-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryGame.java
More file actions
119 lines (94 loc) · 4.08 KB
/
Copy pathMemoryGame.java
File metadata and controls
119 lines (94 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.Scanner;
public class MemoryGame {
public static void main(String[] args){
// initializing objects
Scanner scan = new Scanner(System.in);
int diff = 0; // choice of difficulty
int size = 2*diff; // creates grid of min 4, max 20
boolean playGame = true;
while (!(diff >= 1 && diff <= 5)){ // ensures valid int when int is inputted
System.out.println("Input your difficulty (1-5): ");
diff = scan.nextInt();
}
size = 2*diff;
// initializing boards
int[][] hiddenBoard = new int[size][size];
int[][] gameBoard = new int[size][size];
// intializing numbers for boards
int[] numList = new int[size*size];
int filler = 1;
for(int i = 0; i < numList.length; i+=2){
numList[i] = filler;
numList[i+1] = filler;
filler++;
}
// for(int i = 0; i < numList.length; i++){
// System.out.print(numList[i] + ", ");
// }
System.out.println();
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
int index = (int)(Math.random() * (numList.length));
int number = numList[index];
while (number == 0){
index = (int)(Math.random() * (numList.length));
number = numList[index];
}
gameBoard[i][j] = numList[index];
numList[index] = 0;
hiddenBoard[i][j] = 0;
// gameBoard[i][j] = (index != 0) ? index :
}
}
// for(int i = 0; i < length; i++){
// for(int j = 0; j < size; j++){
// System.out.print(gameBoard[i][j] + " ");
// }
// System.out.println();
// }
displayBoard(gameBoard);
System.out.println();
displayBoard(hiddenBoard);
System.out.print(String.format("\033[2J")); // Attributed to StackOverflow, user LabOctoCat
while(playGame){
displayBoard(hiddenBoard);
System.out.println("Enter the coordinates of the first cell with the row (1-" + size + ") first and column (1-" + size + ") second. Enter -1 to quit. The board is " + size + " by " + size + ".");
int row1 = scan.nextInt() - 1;
int col1 = scan.nextInt() - 1;
if(row1 == -1 || col1 == -1) playGame = false;
displayTempBoard(hiddenBoard, gameBoard, row1, col1);
System.out.println("Enter the coordinates of the second cell with the row (1-" + size + ") first and column (1-" + size + ") second. Enter -1 to quit. The board is " + size + " by " + size + ".");
int row2 = scan.nextInt() - 1;
int col2 = scan.nextInt() - 1;
if(row2 == -1 || col2 == -1) playGame = false;
displayTempBoard(hiddenBoard, gameBoard, row2, col2);
checkCell(hiddenBoard, gameBoard, row1, col1, row2, col2);
}
System.out.println("Thanks for playing!");
}
// display board method,
private static void displayBoard(int[][] board){
for(int[] row : board){
for(int cell : row){
if(cell == 0){
System.out.print("* ");
} else {
System.out.print(cell + " "); // implement if statement
}
}
System.out.println();
}
}
private static void displayTempBoard(int[][]hiddenBoard, int[][]gameBoard, int r1, int c1){
hiddenBoard[r1][c1] = gameBoard[r1][c1];
displayBoard(hiddenBoard);
}
private static void checkCell(int[][]hiddenBoard, int[][] gameBoard, int r1, int c1, int r2, int c2){
if(gameBoard[r1][c1] == gameBoard[r2][c2]){
System.out.println("You found a match!");
} else {
hiddenBoard[r1][c1] = 0;
hiddenBoard[r2][c2] = 0;
}
}
}