Advertisement
Guest User

bruh

a guest
Oct 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. public class MemoryLane
  2. {
  3. private Domino[] board;
  4.  
  5. /**
  6. * Constructs a Memory Lane game with max^2 + max Dominoes.
  7. *
  8. * Example: max <-- 2
  9. * [1] [1] [1] [1] [2] [2]
  10. * [1] [1] [2] [2] [2] [2]
  11. *
  12. * Example: max <-- 3
  13. * [1] [1] [1] [1] [1] [1] [2] [2] [2] [2] [3] [3]
  14. * [1] [1] [2] [2] [3] [3] [2] [2] [3] [3] [3] [3]
  15. *
  16. * Postcondition: board is random
  17. *
  18. * @param max the largest number of pips on the Dominoes
  19. */
  20. public MemoryLane(int max)
  21. {
  22. this.board = new Domino[(int)(Math.pow(max, 2) + max)];
  23. int index = 0;
  24. for (int i = 1; i <= max; i++)
  25. {
  26. board[index++] = new Domino(i,i);
  27. for (int j = 1; j <= max; j++)
  28. {
  29. board[index++] = new Domino(i,j);
  30. }
  31. }
  32. shuffle();
  33. }
  34.  
  35. /**
  36. * Shuffles board
  37. * Precondition: board does not contain null elements
  38. *
  39. * @return
  40. */
  41. private void shuffle()
  42. {
  43. Domino[] tempBoard = new Domino[board.length]; //creates new array and reinputs board
  44. for (int i = 0; i < board.length; i++)
  45. {
  46. int randy = (int) (Math.random()* board.length);
  47. while(board[randy] == null)
  48. randy = (int) (Math.random()* board.length);
  49. tempBoard[i] = board[randy];
  50. board[randy] = null;
  51. }
  52. board = tempBoard;
  53.  
  54. }
  55.  
  56. /**
  57. * Reveals the Dominoes at index i and k if they match
  58. *
  59. * @param i
  60. * @param k
  61. * @return true if the Dominoes at i and k match; false otherwise
  62. */
  63. public boolean guess(int i, int k)
  64. {
  65. if (board[i].equals(board[k]))
  66. {
  67. board[i].setReveal(true);
  68. board[k].setReveal(true);
  69. }
  70. return board[i].equals(board[k]);
  71. }
  72.  
  73. /**
  74.  
  75. *
  76. * Precondition: the elements at the specified indexes are not null
  77. *
  78. * @param indexes variable arguments; any number of indexes;
  79. * indexes if effectively an int[]
  80. * @return
  81. */
  82. public String peek(int... indexes)
  83. {
  84. String whitHubbard = ""; //basically str or rtn
  85. for (int index: indexes)
  86. whitHubbard += "[" + board[index].getTop() + "]";
  87. whitHubbard += "\n";
  88. for (int index : indexes)
  89. whitHubbard += "[" + board[index].getBottom() + "]";
  90. return whitHubbard;
  91.  
  92. }
  93.  
  94. /**
  95. * @return true if the number of revealed Dominoes is equal to the
  96. * length of the board
  97. */
  98. public boolean gameOver()
  99. {
  100. int counter = 0;
  101. for (Domino domino : board)
  102. if (domino.isReveal())
  103. counter++;
  104. return counter == board.length;
  105. }
  106.  
  107. /**
  108. * Returns a String representation of the board in the following format:
  109. *
  110. * 0 1 2 3 4 5
  111. * [top] [ ] [top] [ ] [top] [top]
  112. * [bot] [ ] [bot] [ ] [bot] [bot]
  113. *
  114. * Note: the top and bottom numbers of an unrevealed Domino are represented with
  115. * an empty space
  116. *
  117. * @return
  118. */
  119. public String toString()
  120. {
  121. String no = " ";
  122. int bruh = board.length;
  123. for (int i = 0; i < bruh; i++)
  124. no += i + " ";
  125. no += "\n";
  126. for (int i = 0; i < bruh; i++)
  127. {
  128. if(board[i].isReveal())
  129. no += "[" + board[i].getTop() + "]";
  130. else
  131. no += "[ ]";
  132. }
  133. no += "\n";
  134. for (int i = 0; i < bruh; i++)
  135. {
  136. if(board[i].isReveal())
  137. no += "[" + board[i].getBottom() + "]";
  138. else
  139. no += "[ ]";
  140. }
  141. return no;
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement