Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * The class {@code Bettelmann} simulated the card game 'Bettelmann'. You can construct objects
- * either by providing the piles of cards of the two players, or by requesting a shuffled
- * distribution of cards.
- */
- public class Bettelmann {
- private Deque<Card> closedPile1;
- private Deque<Card> closedPile2;
- private int winner = -1;
- /**
- * Constructor which initializes both players with empty piles.
- */
- public Bettelmann() {
- closedPile1 = new LinkedList<>();
- closedPile2 = new LinkedList<>();
- }
- /**
- * Constructor which initializes both players with the provided piles of cards.
- *
- * @param pile1 pile of cards of player 1.
- * @param pile2 pile of cards of player 2.
- */
- public Bettelmann(Deque<Card> pile1, Deque<Card> pile2) {
- closedPile1 = pile1;
- closedPile2 = pile2;
- }
- /**
- * Returns the closed pile of player 1 (required for the tests).
- *
- * @return The closed pile of player 1.
- */
- public Deque<Card> getClosedPile1() {
- return closedPile1;
- }
- /**
- * Returns the closed pile of player 2 (required for the tests).
- *
- * @return The closed pile of player 2.
- */
- public Deque<Card> getClosedPile2() {
- return closedPile2;
- }
- /**
- * Play one round of the game. This includes drawing more cards, when both players
- * have drawn cards of the same rank. At the end of the round, the player with the
- * higher ranked card wins the trick, so all drawn cards from that round are added
- * to the bottom of her/his closed pile of cards.
- */
- public void playRound() {
- // TODO implement this method
- // offener Stapel jeweils
- Deque<Card> openPile1 = new LinkedList<>(); //oder getClosePile1 ??
- Deque<Card> openPile2 = new LinkedList<>();
- boolean game = true; //das spiel laeuft
- while(game) {
- //Jeweils eine Karte auf einen offenen Stapel legen
- Card card1 = closedPile1.pop();
- openPile1.push(card1);
- Card card2 = closedPile2.pop();
- openPile2.push(card2);
- /**
- * Compares Card objects based on their value, regardless of their suit.
- *
- *@param that the other Card, to which this card is compared
- * @return results of the comparison (<0 if this card has a lower rank than that card, =0 if equivalent, >0 else)
- */
- int comp = card1.compareTo(card2); //sagt aus, ob das ergebnis >0 (1 > 2), <0 (1 < 2) oder 0 (1 = 2) ist
- if (comp > 0) { //die erste Karte hoeheres rangs -> erste person gewinnt
- winner = 1;
- game = false;
- } // die runde ist zu ende, wir haben den gewinner
- else if (comp < 0) {
- winner = 2;
- game = false;
- } // else - comb = 0 -> gleiche karten -> neue karten werden gezogen
- }
- //jetzt haben wir die/den Gewinner/in und die Person muss die offenen Karten der anderen person auf
- //ihre offene Karten legen -> umdrehen -> unter den verdeckten stappel schieben (addLast?)
- /*
- D 8
- A A
- 1 (open) 2(open)
- 10 B
- 1(closed) 2(closed)
- 10 ->oberste karte (war schon im closed stappel)
- A
- D
- A
- 8
- 1(closed)
- heisst -> 2open.pop (8) + 2open.pop (A) + 1open.pop (D) + 1.open.pop (A) -> 8 A D A (wo 8 die oberste Karte ist)
- umdrehen -> A D A 8
- // use addFirst() because the last distributed card should be drawn first
- heisst -> wenn wir was am anfang einfuegen wollen. hier umgekehrt - am ende so das die oberste Karte 10 bleibt
- also wenn wir A D A 8 konsequent zum stappel (10) hinzufuegen -> kriegen genau den Stappel mit
- 10
- A
- D
- A
- 8
- */
- if(winner == 1){
- LinkedList<Card> copyPile = new LinkedList<>();
- while(!openPile2.isEmpty()){ //solange es noch offene Karten der zweiten Person gibt
- Card card = openPile2.removeFirst(); //oberste Karte 2. Stappel
- copyPile.addLast(card); //oder addFirst?
- }
- while(!openPile1.isEmpty()){ //solange es noch offene Karten der ersten Person gibt
- Card card = openPile1.removeFirst(); //oberste Karte 1. Stappel
- copyPile.addLast(card); //oder addFirst?
- }
- //umdrehen
- LinkedList<Card> tempList = new LinkedList<>();
- while (!copyPile.isEmpty()) {
- tempList.add(copyPile.removeFirst());
- }
- //AM Ende des verdeckten Stappels (1 person) hinzifuegen
- while (!tempList.isEmpty()){
- closedPile1.addLast((Card) tempList.removeLast());
- }
- }
- //das gleiche, umgekehrt - wenn die ZWEITE person gewinnt
- else if(winner == 2){
- LinkedList<Card> copyPile = new LinkedList<>();
- while(!openPile1.isEmpty()){ //solange es noch offene Karten der ersten Person gibt
- Card card = openPile1.removeFirst(); //oberste Karte 1. Stappel
- copyPile.addLast(card); //oder addFirst?
- }
- while(!openPile2.isEmpty()){ //solange es noch offene Karten der zweiten Person gibt
- Card card = openPile2.removeFirst(); //oberste Karte 2. Stappel
- copyPile.addLast(card); //oder addFirst?
- }
- //umdrehen
- LinkedList<Card> tempList = new LinkedList<>();
- while (!copyPile.isEmpty()) {
- tempList.add(copyPile.removeFirst());
- }
- //AM Ende des verdeckten Stappels (2 person) hinzifuegen
- while (!tempList.isEmpty()){
- closedPile2.addLast((Card) tempList.removeLast());
- }
- }
- else {
- winner = -1;
- playRound();
- }
- }
- /**
- * Returns the winner of the game after the end, or -1 during the game.
- *
- * @return the winner of game (1 or 2), or -1 while the game is ongoing.
- */
- public int getWinner() {
- return winner;
- }
- /**
- * Deal the given deck of cards alternately to the two players.
- * Side effect: The deck is empty after calling this method.
- *
- * @param deck The deck of cards that is distributed to the players.
- */
- public void distributeCards(Stack<Card> deck) {
- closedPile1.clear();
- closedPile2.clear();
- // use addFirst() because the last distributed card should be drawn first
- while (!deck.isEmpty()) {
- Card card = deck.pop();
- closedPile1.addFirst(card);
- if (!deck.isEmpty()) {
- card = deck.pop();
- closedPile2.addFirst(card);
- }
- }
- }
- /**
- * Shuffle a deck of cards and distribute it evenly to the two players.
- */
- public void distributeCards() {
- Stack<Card> deck = new Stack<>();
- for (int i = 0; i < Card.nCards; i++){
- deck.add(new Card(i));
- }
- Collections.shuffle(deck);
- distributeCards(deck);
- }
- /**
- * Returns a String representation of closed piles of cards of the two players.
- *
- * @return String representation of the state of the game.
- */
- @Override
- public String toString() {
- return "Player 1: " + closedPile1 + "\nPlayer 2: " + closedPile2;
- }
- public static void main(String[] args) {
- /*
- // Game with a complete, shuffled deck
- Bettelmann game = new Bettelmann();
- game.distributeCards();
- */
- // For testing, you may also use specific distribtions and a small number of cards like this:
- int[] deckArray = {28, 30, 6, 23, 17, 14};
- Stack<Card> deck = new Stack<>();
- for (int id : deckArray) {
- deck.push(new Card(id));
- }
- Bettelmann game = new Bettelmann();
- game.distributeCards(deck);
- // This part is the same for both of the above variants
- System.out.println("Initial situation (top card first):\n" + game);
- int round = 0;
- while (round < 1000000 && game.getWinner()<0) {
- round++;
- game.playRound();
- System.out.println("State after round " + round + ":\n" + game);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement