Advertisement
madegoff

Bettelmann

May 13th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4. * The class {@code Bettelmann} simulated the card game 'Bettelmann'. You can construct objects
  5. * either by providing the piles of cards of the two players, or by requesting a shuffled
  6. * distribution of cards.
  7. */
  8. public class Bettelmann {
  9. private Deque<Card> closedPile1;
  10. private Deque<Card> closedPile2;
  11. private int winner = -1;
  12.  
  13. /**
  14. * Constructor which initializes both players with empty piles.
  15. */
  16. public Bettelmann() {
  17. closedPile1 = new LinkedList<>();
  18. closedPile2 = new LinkedList<>();
  19. }
  20.  
  21. /**
  22. * Constructor which initializes both players with the provided piles of cards.
  23. *
  24. * @param pile1 pile of cards of player 1.
  25. * @param pile2 pile of cards of player 2.
  26. */
  27. public Bettelmann(Deque<Card> pile1, Deque<Card> pile2) {
  28. closedPile1 = pile1;
  29. closedPile2 = pile2;
  30. }
  31.  
  32. /**
  33. * Returns the closed pile of player 1 (required for the tests).
  34. *
  35. * @return The closed pile of player 1.
  36. */
  37. public Deque<Card> getClosedPile1() {
  38. return closedPile1;
  39. }
  40.  
  41. /**
  42. * Returns the closed pile of player 2 (required for the tests).
  43. *
  44. * @return The closed pile of player 2.
  45. */
  46. public Deque<Card> getClosedPile2() {
  47. return closedPile2;
  48. }
  49.  
  50. /**
  51. * Play one round of the game. This includes drawing more cards, when both players
  52. * have drawn cards of the same rank. At the end of the round, the player with the
  53. * higher ranked card wins the trick, so all drawn cards from that round are added
  54. * to the bottom of her/his closed pile of cards.
  55. */
  56. public void playRound() {
  57. // TODO implement this method
  58.  
  59. // offener Stapel jeweils
  60. Deque<Card> openPile1 = new LinkedList<>(); //oder getClosePile1 ??
  61. Deque<Card> openPile2 = new LinkedList<>();
  62.  
  63. boolean game = true; //das spiel laeuft
  64.  
  65. while(game) {
  66.  
  67. //Jeweils eine Karte auf einen offenen Stapel legen
  68. Card card1 = closedPile1.pop();
  69. openPile1.push(card1);
  70.  
  71. Card card2 = closedPile2.pop();
  72. openPile2.push(card2);
  73.  
  74. /**
  75. * Compares Card objects based on their value, regardless of their suit.
  76. *
  77. *@param that the other Card, to which this card is compared
  78. * @return results of the comparison (<0 if this card has a lower rank than that card, =0 if equivalent, >0 else)
  79. */
  80. int comp = card1.compareTo(card2); //sagt aus, ob das ergebnis >0 (1 > 2), <0 (1 < 2) oder 0 (1 = 2) ist
  81.  
  82. if (comp > 0) { //die erste Karte hoeheres rangs -> erste person gewinnt
  83. winner = 1;
  84. game = false;
  85. } // die runde ist zu ende, wir haben den gewinner
  86. else if (comp < 0) {
  87. winner = 2;
  88. game = false;
  89. } // else - comb = 0 -> gleiche karten -> neue karten werden gezogen
  90. }
  91.  
  92. //jetzt haben wir die/den Gewinner/in und die Person muss die offenen Karten der anderen person auf
  93. //ihre offene Karten legen -> umdrehen -> unter den verdeckten stappel schieben (addLast?)
  94.  
  95. /*
  96.  
  97. D 8
  98. A A
  99. 1 (open) 2(open)
  100.  
  101. 10 B
  102. 1(closed) 2(closed)
  103.  
  104. 10 ->oberste karte (war schon im closed stappel)
  105. A
  106. D
  107. A
  108. 8
  109. 1(closed)
  110.  
  111. heisst -> 2open.pop (8) + 2open.pop (A) + 1open.pop (D) + 1.open.pop (A) -> 8 A D A (wo 8 die oberste Karte ist)
  112. umdrehen -> A D A 8
  113. // use addFirst() because the last distributed card should be drawn first
  114.  
  115. heisst -> wenn wir was am anfang einfuegen wollen. hier umgekehrt - am ende so das die oberste Karte 10 bleibt
  116. also wenn wir A D A 8 konsequent zum stappel (10) hinzufuegen -> kriegen genau den Stappel mit
  117.  
  118. 10
  119. A
  120. D
  121. A
  122. 8
  123. */
  124.  
  125. if(winner == 1){
  126.  
  127. LinkedList<Card> copyPile = new LinkedList<>();
  128.  
  129. while(!openPile2.isEmpty()){ //solange es noch offene Karten der zweiten Person gibt
  130. Card card = openPile2.removeFirst(); //oberste Karte 2. Stappel
  131. copyPile.addLast(card); //oder addFirst?
  132. }
  133. while(!openPile1.isEmpty()){ //solange es noch offene Karten der ersten Person gibt
  134. Card card = openPile1.removeFirst(); //oberste Karte 1. Stappel
  135. copyPile.addLast(card); //oder addFirst?
  136. }
  137.  
  138. //umdrehen
  139. LinkedList<Card> tempList = new LinkedList<>();
  140. while (!copyPile.isEmpty()) {
  141. tempList.add(copyPile.removeFirst());
  142. }
  143.  
  144. //AM Ende des verdeckten Stappels (1 person) hinzifuegen
  145.  
  146. while (!tempList.isEmpty()){
  147. closedPile1.addLast((Card) tempList.removeLast());
  148. }
  149. }
  150.  
  151. //das gleiche, umgekehrt - wenn die ZWEITE person gewinnt
  152.  
  153. else if(winner == 2){
  154.  
  155. LinkedList<Card> copyPile = new LinkedList<>();
  156.  
  157. while(!openPile1.isEmpty()){ //solange es noch offene Karten der ersten Person gibt
  158. Card card = openPile1.removeFirst(); //oberste Karte 1. Stappel
  159. copyPile.addLast(card); //oder addFirst?
  160. }
  161. while(!openPile2.isEmpty()){ //solange es noch offene Karten der zweiten Person gibt
  162. Card card = openPile2.removeFirst(); //oberste Karte 2. Stappel
  163. copyPile.addLast(card); //oder addFirst?
  164. }
  165.  
  166. //umdrehen
  167. LinkedList<Card> tempList = new LinkedList<>();
  168. while (!copyPile.isEmpty()) {
  169. tempList.add(copyPile.removeFirst());
  170. }
  171.  
  172. //AM Ende des verdeckten Stappels (2 person) hinzifuegen
  173.  
  174. while (!tempList.isEmpty()){
  175. closedPile2.addLast((Card) tempList.removeLast());
  176. }
  177. }
  178. else {
  179. winner = -1;
  180. playRound();
  181. }
  182.  
  183. }
  184.  
  185. /**
  186. * Returns the winner of the game after the end, or -1 during the game.
  187. *
  188. * @return the winner of game (1 or 2), or -1 while the game is ongoing.
  189. */
  190. public int getWinner() {
  191. return winner;
  192. }
  193.  
  194. /**
  195. * Deal the given deck of cards alternately to the two players.
  196. * Side effect: The deck is empty after calling this method.
  197. *
  198. * @param deck The deck of cards that is distributed to the players.
  199. */
  200. public void distributeCards(Stack<Card> deck) {
  201. closedPile1.clear();
  202. closedPile2.clear();
  203. // use addFirst() because the last distributed card should be drawn first
  204. while (!deck.isEmpty()) {
  205. Card card = deck.pop();
  206. closedPile1.addFirst(card);
  207. if (!deck.isEmpty()) {
  208. card = deck.pop();
  209. closedPile2.addFirst(card);
  210. }
  211. }
  212. }
  213.  
  214. /**
  215. * Shuffle a deck of cards and distribute it evenly to the two players.
  216. */
  217. public void distributeCards() {
  218. Stack<Card> deck = new Stack<>();
  219. for (int i = 0; i < Card.nCards; i++){
  220. deck.add(new Card(i));
  221. }
  222. Collections.shuffle(deck);
  223. distributeCards(deck);
  224. }
  225.  
  226. /**
  227. * Returns a String representation of closed piles of cards of the two players.
  228. *
  229. * @return String representation of the state of the game.
  230. */
  231. @Override
  232. public String toString() {
  233. return "Player 1: " + closedPile1 + "\nPlayer 2: " + closedPile2;
  234. }
  235.  
  236. public static void main(String[] args) {
  237. /*
  238. // Game with a complete, shuffled deck
  239. Bettelmann game = new Bettelmann();
  240. game.distributeCards();
  241. */
  242.  
  243. // For testing, you may also use specific distribtions and a small number of cards like this:
  244. int[] deckArray = {28, 30, 6, 23, 17, 14};
  245. Stack<Card> deck = new Stack<>();
  246. for (int id : deckArray) {
  247. deck.push(new Card(id));
  248. }
  249. Bettelmann game = new Bettelmann();
  250. game.distributeCards(deck);
  251.  
  252. // This part is the same for both of the above variants
  253. System.out.println("Initial situation (top card first):\n" + game);
  254. int round = 0;
  255. while (round < 1000000 && game.getWinner()<0) {
  256. round++;
  257. game.playRound();
  258. System.out.println("State after round " + round + ":\n" + game);
  259. }
  260. }
  261. }
  262.  
  263.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement