-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextDriver.java
244 lines (230 loc) · 7.94 KB
/
TextDriver.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package game;
import java.util.*;
/**
* Text driver that enables students to test boards using the terminal.
* @author Kal Pandit
* @author Ishaan Ivaturi
*/
public class TextDriver {
/**
* Enables students to test commands interactively; calls methods to obtain
* methods and play the game.
*
**/
public static void main(String[] args) {
int action = getActionChoice();
switch (action) {
case 1: // Individual methods
testIndividualMethods();
break;
case 2: // Play full game
playFullGame();
break;
default:
StdOut.println("Not a valid option!");
break;
}
System.exit(0);
}
/**
* Tests individual methods.
* @param filename the file name to use for testing
*/
private static void testIndividualMethods() {
String[] methods = { "updateOpenSpaces", "addRandomTile", "swipeLeft", "mergeLeft", "transpose", "flipRows", "makeMove"};
String[] options = { "Test a new input file", "Test another method on the same input file", "Quit" };
int controlChoice = 1;
do {
StdOut.print("Enter a board input file name => ");
String filename = StdIn.readLine();
do {
int method = getMethodChoice(methods);
int[][] boardArray = readBoard(filename);
Board board = new Board(boardArray);
StdOut.println("Base Board:");
board.print();
testMethod(method, board);
controlChoice = getControlChoice(options);
} while (controlChoice == 2);
} while (controlChoice == 1);
}
/**
* Reads board from input file and returns board array.
* @param filename the file name of the board to read
* @return the board array
*/
private static int[][] readBoard(String filename) {
StdIn.setFile(filename);
int[][] boardArray = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
boardArray[i][j] = StdIn.readInt();
}
}
StdIn.resetFile();
return boardArray;
}
/**
* Provides options for testing a new method or quitting.
* @param options the possible options for testing
* @return the result of this action
*/
private static int getControlChoice(String[] options) {
StdOut.println("What would you like to do?");
for (int i = 0; i < 3; i++) {
StdOut.println(i + 1 + ". " + options[i]);
}
StdOut.print("Enter a number => ");
int number = Integer.parseInt(StdIn.readLine());
return number;
}
/**
* Provides options for testing methods or actions
* @param options the possible options for methods or actions
* @return the result of this action
*/
private static int getMethodChoice(String[] methods) {
StdOut.println("What method would you like to test?");
for (int i = 0; i < 7; i++) {
StdOut.println(i + 1 + ". " + methods[i]);
}
StdOut.print("Enter a number => ");
int number = Integer.parseInt(StdIn.readLine());
return number;
}
/**
* Provides options for testing individual methods or the full game
* @param options the possible options for methods or actions
* @return the result of this action
*/
private static int getActionChoice() {
StdOut.println("What would you like to do?");
StdOut.println("1. Test individual methods");
StdOut.println("2. Play full game");
StdOut.print("Enter a number => ");
int number = Integer.parseInt(StdIn.readLine());
return number;
}
/**
* Tests a method given by the user given their board
* @param methodNumber the method number identified through the method list
* @param board the student board
*/
private static void testMethod(int methodNumber, Board board) {
switch (methodNumber) {
case 1:
board.updateOpenSpaces();
StdOut.println("Open Spaces: (** on board)");
board.printOpenSpaces();
break;
case 2:
StdRandom.setSeed(2022);
board.updateOpenSpaces();
board.addRandomTile();
StdOut.println("New Board:");
board.print();
break;
case 3:
board.swipeLeft();
StdOut.println("New Board:");
board.print();
break;
case 4:
board.mergeLeft();
StdOut.println("New Board:");
board.print();
break;
case 5:
board.transpose();
StdOut.println("New Board:");
board.print();
break;
case 6:
board.flipRows();
StdOut.println("New Board:");
board.print();
break;
case 7:
StdOut.println("Actions: w = up, a = left, s = down, d = right");
HashMap<Character, String> keyMap = new HashMap<>() {
{
put('w', "U");
put('a', "L");
put('s', "D");
put('d', "R");
put('W', "U");
put('A', "L");
put('S', "D");
put('D', "R");
}
};
StdOut.print("Enter an action => ");
String toRead = StdIn.readLine();
String action = keyMap.getOrDefault(toRead.charAt(0), "Invalid");
if (!action.equals("Invalid")) {
board.makeMove(action.charAt(0));
}
StdOut.println("New Board:");
board.print();
break;
}
}
/**
* Enables students to play the full game of 2048.
* Calls all methods.
*/
private static void playFullGame() {
StdRandom.setSeed(2022);
Board board = new Board();
board.updateOpenSpaces();
board.addRandomTile();
board.updateOpenSpaces();
board.addRandomTile();
StdOut.println("Base Board:");
board.print();
StdOut.println("Actions: w = up, a = left, s = down, d = right, q = exit (hit ENTER to enter)");
HashMap<Character, String> keyMap = new HashMap<>() {
{
put('w', "U");
put('a', "L");
put('s', "D");
put('d', "R");
put('W', "U");
put('A', "L");
put('S', "D");
put('D', "R");
put('q', "Exit");
}
};
while (true) {
String toRead = StdIn.readLine();
String action = keyMap.getOrDefault(toRead.charAt(0), "Invalid");
if (action.equals("Exit"))
break;
if (!action.equals("Invalid")) {
int[][] oldBoard = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
oldBoard[i][j] = board.getBoard()[i][j];
}
}
board.makeMove(action.charAt(0));
board.updateOpenSpaces();
if (!board.isGameLost() && !boardsMatch(oldBoard, board.getBoard())) {
board.addRandomTile();
}
StdOut.println("New Board: " + action);
board.print();
}
}
}
private static boolean boardsMatch(int[][] board1, int[][] board2) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (board1[i][j] != board2[i][j])
return false;
}
}
return true;
}
}