Memory Game [Java]


SUBMITTED BY: Guest

DATE: Nov. 29, 2013, 3:08 a.m.

FORMAT: Java

SIZE: 4.3 kB

HITS: 1007

  1. /* A simple memory game. Generates a random SIZE x SIZE board.
  2. Pick the coordianates of the card you want to flip over.
  3. If two cards match, they stay face up.
  4. Program ends when you match all the cards.
  5. Yes, the code is extremely messy. I'm new to programming :P
  6. */
  7. import java.util.Scanner;
  8. public class Memory {
  9. public static final int SIZE = 4;
  10. public static void main(String[] args)
  11. {
  12. Scanner keyboard = new Scanner(System.in);
  13. Memory[][] board = generateBoard();
  14. printBoard(board);
  15. int x, y, a, b;
  16. do
  17. {
  18. System.out.println("Which card do you want to turn over?");
  19. x = keyboard.nextInt() - 1;
  20. y = keyboard.nextInt() - 1;
  21. if(x >= SIZE || y >= SIZE)
  22. {
  23. System.out.println("The card doesn't exist!");
  24. continue;
  25. }
  26. if(board[x][y].matched)
  27. {
  28. System.out.println("You've already matched this one!");
  29. continue;
  30. }
  31. turn(board, x, y);
  32. System.out.println("Pick another one.");
  33. a = keyboard.nextInt() - 1;
  34. b = keyboard.nextInt() - 1;
  35. if (a == x && b == y)
  36. {
  37. System.out.println("That's the same card!");
  38. continue;
  39. }
  40. if(a >= SIZE || b >= SIZE)
  41. {
  42. System.out.println("The card doesn't exist!");
  43. turnBack(board, x, y);
  44. continue;
  45. }
  46. if(board[a][b].matched)
  47. {
  48. System.out.println("You've already matched this one!");
  49. continue;
  50. }
  51. turn(board, a, b);
  52. if(board[x][y].getFace() == board[a][b].getFace())
  53. {
  54. board[x][y].correct(board);
  55. board[a][b].correct(board);
  56. System.out.println("It's a match!");
  57. turnBack(board, x, y);
  58. turnBack(board, a, b);
  59. printBoard(board);
  60. }
  61. else
  62. {
  63. System.out.println("Try again.");
  64. printBoard(board);
  65. turnBack(board, x, y);
  66. turnBack(board, a, b);
  67. }
  68. }while(true);
  69. }
  70. private boolean faceUp;
  71. private boolean matched;
  72. private char face;
  73. public Memory(char letter)
  74. {
  75. matched = false;
  76. faceUp = false;
  77. face = letter;
  78. }
  79. public static void turn(Memory[][] board, int x, int y)
  80. {
  81. board[x][y].faceUp = true;
  82. printBoard(board);
  83. }
  84. public static void turnBack(Memory[][] board, int x, int y)
  85. {
  86. board[x][y].faceUp = false;
  87. }
  88. public void correct(Memory[][] board)
  89. {
  90. matched = true;
  91. boolean finished = false;
  92. for(Memory[] element : board)
  93. {
  94. for(Memory element2 : element)
  95. {
  96. if(!element2.matched)
  97. return;
  98. else
  99. finished = true;
  100. }
  101. }
  102. if(finished)
  103. System.out.println("Victory!");
  104. System.exit(0);
  105. }
  106. public char getFace()
  107. {
  108. return face;
  109. }
  110. public static Memory[][] generateBoard()
  111. {
  112. Memory[][] board = new Memory[SIZE][SIZE];
  113. boolean[][] available = new boolean[SIZE][SIZE];
  114. int i1, i2;
  115. for(i1 = 0; i1 < SIZE; i1++)
  116. {
  117. for(i2 = 0; i2 < SIZE; i2++)
  118. available[i1][i2] = true;
  119. }
  120. int freeSpaces = SIZE * SIZE;
  121. char letter = 'a';
  122. int numberUsed = 0;
  123. int rand1, rand2;
  124. do
  125. {
  126. rand1 = (int)(Math.random() * SIZE);
  127. rand2 = (int)(Math.random() * SIZE);
  128. if(available[rand1][rand2])
  129. {
  130. available[rand1][rand2] = false;
  131. board[rand1][rand2] = new Memory(letter);
  132. freeSpaces--;
  133. numberUsed++;
  134. }
  135. if(numberUsed == 2)
  136. {
  137. numberUsed = 0;
  138. letter++;
  139. }
  140. }while(freeSpaces > 0);
  141. return board;
  142. }
  143. public static void showBoard(Memory[][] board)
  144. {
  145. for(Memory[] element : board)
  146. {
  147. for (Memory element2 : element)
  148. System.out.print(element2.face + " ");
  149. System.out.println();
  150. }
  151. }
  152. public static void printBoard(Memory[][] board)
  153. {
  154. for(Memory[] element : board)
  155. {
  156. for(Memory element2 : element)
  157. {
  158. if(element2.faceUp || element2.matched)
  159. System.out.print(element2.face + " ");
  160. else
  161. System.out.print('x' + " ");
  162. }
  163. System.out.println();
  164. }
  165. }
  166. }

comments powered by Disqus