Table.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Table.java
  3. *
  4. * This is the Class with the data structure that represents everything that is
  5. * currently in the game including: players, player hands, the dominoes on the
  6. * board.
  7. *
  8. */
  9. import java.util.ArrayList;
  10. import java.util.Random;
  11. public class Table
  12. {
  13. //fields
  14. //keeps track of dominoes on table
  15. private ArrayList<Domino> boneYard;
  16. private ArrayList<Domino> lineOfPlay;
  17. private final int NUM_PLAYERS = 2;
  18. public Player[] players = new Player[NUM_PLAYERS];
  19. // stores most recently placed domino
  20. private Domino placed;
  21. //keeps track of player who places next
  22. private int whoseTurn;
  23. //number of dominos distributed to each player when game begins
  24. private final int HAND_SIZE = 10;
  25. private boolean areThereAnyWinner = false;
  26. //constructor
  27. public Table()
  28. {
  29. //make the dominoes
  30. boneYard = createDominoes();
  31. lineOfPlay = new ArrayList<Domino>();
  32. //create players
  33. for (int i = 0; i < NUM_PLAYERS; i++)
  34. {
  35. players[i] = new Player( makeStartHand() );
  36. }
  37. //remaining dominoes stay in boneYard
  38. }
  39. //Make a players starting hand
  40. private ArrayList<Domino> makeStartHand()
  41. {
  42. ArrayList<Domino> startHand = new ArrayList<Domino>();
  43. for (int i = 0; i < HAND_SIZE; i++){
  44. startHand.add( boneYard.remove(0) );
  45. }
  46. return startHand;
  47. }
  48. //Method to determine which player has the highest sum of domino dots.
  49. public int highestDomino()
  50. {
  51. int sumPlayer1 = getSumOfDots(players[0]);
  52. int sumPlayer2 = getSumOfDots(players[1]);
  53. //System.out.println("player1: " + sumPlayer1);
  54. //System.out.println("player2: " + sumPlayer2);
  55. if (sumPlayer1 > sumPlayer2)
  56. {
  57. System.out.println("Player 1 has the heaviest hand so"
  58. + " they will place first");
  59. return (int)0;
  60. }
  61. else if (sumPlayer1 == sumPlayer2)
  62. {
  63. System.out.println("Both players are tied for number of dots!");
  64. System.out.println("Chosing at random...");
  65. Random randomObject = new Random();
  66. int randomPlayerNumber = randomObject.nextInt(2);
  67. System.out.println("Player " + randomPlayerNumber + " goes first!");
  68. return randomPlayerNumber;
  69. }
  70. else
  71. {
  72. System.out.println("Player 2 has the heaviest hand so they will place first");
  73. return (int)1;
  74. }
  75. }
  76. //Returns the sum of dots on dominoes in hand. Also saves it in a field.
  77. public int getSumOfDots(Player aPlayer)
  78. {
  79. int sumOfDots = 0;
  80. for (int i = 0; i < aPlayer.getHandSize(); i++)
  81. {
  82. int temp = aPlayer.getHand().get(i).getLeftSquare()
  83. + aPlayer.getHand().get(i).getRightSquare();
  84. sumOfDots += temp;
  85. }
  86. if ( aPlayer.getHandSize() == 0 )
  87. {
  88. aPlayer.setNumberOfDots( (int) 0 );
  89. return (int) 0;
  90. }
  91. aPlayer.setNumberOfDots(sumOfDots);
  92. return sumOfDots;
  93. }
  94. //getter method returns most recently placed domino
  95. public Domino getPlaced(){
  96. return placed;
  97. }
  98. //getter method returns whos turn it is to place a domino
  99. public int getTurn(){
  100. return whoseTurn;
  101. }
  102. //Method for playing the game. Returns the number of winning player.
  103. //If the game is blocked, returns -1 if player1 blocked, and -2 if blocked by player2.
  104. public int playGame()
  105. {
  106. //System.out.println("Entering playGame");
  107. while ( !isGameOver() )
  108. {
  109. while ( !isMatchInHand() )
  110. {
  111. if ( isBoneyardEmpty() )
  112. {
  113. //count dots with getSumOfDots to save sums to fields.
  114. //return -1 to indicate tie.
  115. getSumOfDots(players[0]);
  116. getSumOfDots(players[1]);
  117. System.out.println("Player " + (whoseTurn + 1) + " is unable to place a domino!");
  118. return -1 - whoseTurn;
  119. }
  120. drawFromBoneyard();
  121. }
  122. //System.out.println("Entering play a piece part");
  123. System.out.println("Next player is looking for a piece to place...");
  124. //play
  125. placeAPiece();
  126. //switch turns
  127. updateTurn();
  128. }
  129. //System.out.println("Entering winner check loop");
  130. System.out.println("Looks like we have a winner...");
  131. return whoWon();
  132. }
  133. //method determines if player has needed domino and places it
  134. private void placeAPiece()
  135. {
  136. int right = getRightmostSquareInLine();
  137. int left = getLeftmostSquareInLine();
  138. for (int i = 0; i < players[whoseTurn].getHandSize(); i++)
  139. {
  140. int thisRight = players[whoseTurn].getHand().get(i).getRightSquare();
  141. int thisLeft = players[whoseTurn].getHand().get(i).getLeftSquare();
  142. if (left == thisRight)
  143. {
  144. System.out.println("\nPlayer " + (whoseTurn + 1) + " places: " + players[whoseTurn].getHand().get(i) );
  145. addToLeftOfLine( players[whoseTurn].removeFromHand(i) );
  146. break;
  147. }
  148. else if (left == thisLeft)
  149. {
  150. System.out.println("\nPlayer " + (whoseTurn + 1) + " places: " + players[whoseTurn].getHand().get(i) );
  151. addToLeftOfLine( Domino.rotateDomino( players[whoseTurn].removeFromHand(i) ) );
  152. break;
  153. }
  154. else if (right == thisRight)
  155. {
  156. System.out.println("\nPlayer " + (whoseTurn + 1) + " places: " + players[whoseTurn].getHand().get(i) );
  157. addToRightOfLine( Domino.rotateDomino( players[whoseTurn].removeFromHand(i) ) );
  158. break;
  159. }
  160. else if (right == thisLeft)
  161. {
  162. System.out.println("\nPlayer " + (whoseTurn + 1) + " places: " + players[whoseTurn].getHand().get(i) );
  163. addToRightOfLine( players[whoseTurn].removeFromHand(i) );
  164. break;
  165. }
  166. else
  167. {
  168. System.out.println("Player " + (whoseTurn + 1) + " could not place domino number " + i);
  169. }
  170. }
  171. }
  172. //Checks if the game is over
  173. private boolean isGameOver()
  174. {
  175. for (int i = 0; i < NUM_PLAYERS; i++)
  176. {
  177. if ( players[i].checkAndSetIfWinner() )
  178. {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. //Returns the winner. Call only if game was not blocked.
  185. //Returns 0 for player1, 1 for player 2, and -99 for error.
  186. //Also, updates dot totals.
  187. private int whoWon()
  188. {
  189. for (int i = 0; i < NUM_PLAYERS; i++)
  190. {
  191. if ( players[i].checkAndSetIfWinner() )
  192. {
  193. return i;
  194. }
  195. }
  196. return -99;
  197. }
  198. //Adds a domino from the boneyard to the current player's hand.
  199. private void drawFromBoneyard()
  200. {
  201. //need to put a domino in there!
  202. players[whoseTurn].addToHand( this.boneYard.remove(0) );
  203. }
  204. //checks if there is a match in hand to one of the ends of the lineOfPlay
  205. private boolean isMatchInHand()
  206. {
  207. int right = getRightmostSquareInLine();
  208. int left = getLeftmostSquareInLine();
  209. for (int i = 0; i < players[whoseTurn].getHandSize(); i++)
  210. {
  211. int thisRight = players[whoseTurn].getHand().get(i).getRightSquare();
  212. int thisLeft = players[whoseTurn].getHand().get(i).getLeftSquare();
  213. if ( (left == thisRight) || ( left == thisLeft) || (right == thisRight) || (right == thisLeft) )
  214. {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. //method to place first domino onto table from players hand
  221. public void placeFirstDomino(int playerToTakeFrom)
  222. {
  223. Random randomObject = new Random();
  224. if (playerToTakeFrom==1)
  225. {
  226. updateTurn();
  227. }
  228. int dominoIndex = randomObject.nextInt(players[playerToTakeFrom].getHandSize());
  229. System.out.print("Player " + (playerToTakeFrom + 1) + " places:" + players[playerToTakeFrom].getHand().get(dominoIndex));
  230. placed = players[playerToTakeFrom].removeFromHand(dominoIndex);
  231. addToRightOfLine(placed);
  232. updateTurn();
  233. }
  234. //Will print the current state of the table.
  235. public String toString()
  236. {
  237. //Update dot totals.
  238. for (int i = 0; i < NUM_PLAYERS; i++)
  239. {
  240. getSumOfDots(players[i]);
  241. }
  242. return "Player1: " + players[0]
  243. + "\nPlayer2: " + players[1]
  244. + "\nBoneyard: " + printBoneyard()
  245. + "\nLine of play: " + printLineOfPlay()
  246. + "\n\n";
  247. }
  248. //code creates the correct number and combinations do dominoes
  249. private ArrayList<Domino> createUnsortedDominoes()
  250. {
  251. //ArrayList stores created dominoes
  252. ArrayList<Domino> unsortedDominoes = new ArrayList<Domino>();
  253. //creates 28 domino set
  254. unsortedDominoes.add(new Domino(0,0));
  255. unsortedDominoes.add(new Domino(1,0));
  256. unsortedDominoes.add(new Domino(2,0));
  257. unsortedDominoes.add(new Domino(3,0));
  258. unsortedDominoes.add(new Domino(4,0));
  259. unsortedDominoes.add(new Domino(5,0));
  260. unsortedDominoes.add(new Domino(6,0));
  261. unsortedDominoes.add(new Domino(1,1));
  262. unsortedDominoes.add(new Domino(2,1));
  263. unsortedDominoes.add(new Domino(3,1));
  264. unsortedDominoes.add(new Domino(4,1));
  265. unsortedDominoes.add(new Domino(5,1));
  266. unsortedDominoes.add(new Domino(6,1));
  267. unsortedDominoes.add(new Domino(2,2));
  268. unsortedDominoes.add(new Domino(3,2));
  269. unsortedDominoes.add(new Domino(4,2));
  270. unsortedDominoes.add(new Domino(5,2));
  271. unsortedDominoes.add(new Domino(6,2));
  272. unsortedDominoes.add(new Domino(3,3));
  273. unsortedDominoes.add(new Domino(4,3));
  274. unsortedDominoes.add(new Domino(5,3));
  275. unsortedDominoes.add(new Domino(6,3));
  276. unsortedDominoes.add(new Domino(4,4));
  277. unsortedDominoes.add(new Domino(5,4));
  278. unsortedDominoes.add(new Domino(6,4));
  279. unsortedDominoes.add(new Domino(5,5));
  280. unsortedDominoes.add(new Domino(6,5));
  281. unsortedDominoes.add(new Domino(6,6));
  282. return unsortedDominoes;
  283. }
  284. //method invoked shuffleDominoes method
  285. private ArrayList<Domino> sortDominoes(ArrayList<Domino> dominoesToSort)
  286. {
  287. return Shuffle.shuffleDominoes(dominoesToSort);
  288. }
  289. //method invokes createUnsortedDominoes() method
  290. private ArrayList<Domino> createDominoes()
  291. {
  292. ArrayList<Domino> someDominoes = createUnsortedDominoes();
  293. return sortDominoes(someDominoes);
  294. }
  295. //might need/want to make static later?
  296. //change to iterate through printing punctuation and toString of each domino.
  297. private String printBoneyard()
  298. {
  299. return boneYard.toString(); //change later
  300. }
  301. //Returns the contents of the line of play as a string.
  302. public String printLineOfPlay()
  303. {
  304. return lineOfPlay.toString();
  305. }
  306. //Adds to the left of the line
  307. private void addToLeftOfLine(Domino dominoToAdd)
  308. {
  309. lineOfPlay.add(0, dominoToAdd);
  310. }
  311. //Adds to the right of the line.
  312. private void addToRightOfLine(Domino dominoToAdd)
  313. {
  314. lineOfPlay.add(dominoToAdd);
  315. }
  316. //Updates the current turn
  317. private void updateTurn()
  318. {
  319. whoseTurn = (whoseTurn + 1) % NUM_PLAYERS;
  320. }
  321. //Checks if there are any winners on the table.
  322. //Returns -1 if none, or the number of the player if they are a winner.
  323. public int checkAreThereAnyWinners()
  324. {
  325. for (int i = 0; i < NUM_PLAYERS; i++)
  326. {
  327. if (this.players[i].checkAndSetIfWinner() )
  328. {
  329. return i;
  330. }
  331. }
  332. return -1;
  333. }
  334. //Checks if boneyard is empty
  335. public boolean isBoneyardEmpty()
  336. {
  337. if ( boneYard.size() <= 0 )
  338. {
  339. return true;
  340. }
  341. return false;
  342. }
  343. //Gets the value of the rightmost square on the lineOfPlay
  344. private int getLeftmostSquareInLine()
  345. {
  346. return lineOfPlay.get(0).getLeftSquare();
  347. }
  348. //Get the value of the leftmost square on the lineOfPlay
  349. private int getRightmostSquareInLine()
  350. {
  351. return lineOfPlay.get( lineOfPlay.size() - 1 ).getRightSquare();
  352. }
  353. }