-
Notifications
You must be signed in to change notification settings - Fork 9
/
GameManager.java
executable file
·244 lines (186 loc) · 6.75 KB
/
GameManager.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
/*
* Name: Daniel Huang
* PID:A12440549
* Login: cs8bwacy
* Date: 2/3/2016
* File: GameManager.java
* Sources of Help:PIAZZA
* This program is the game manager of 2048.
*
* */
//------------------------------------------------------------------//
// GameManager.java //
// //
// Game Manager for 2048 //
// //
// Author: W16-CSE8B-TA group //
// Date: 1/17/16 //
//------------------------------------------------------------------//
import java.util.*;
import java.io.*;
/*
* Name:GameManager Class
* Purpose: It manages the start,save,loading,and closing of
* the game.
* Parameters: None
* Return: void
*/
public class GameManager {
// Instance variables
private Board board; // The actual 2048 board
private String outputFileName; // File to save the board to when exiting
/*
* Name: GameManager(the constructor)
* Purpose: GameManager Constructor
It generates a new game
* Parameters: int boardSize:specify the size of the grid
* String outputBoard:specify where to save the game
* Random random: a random generator
* Return: void
*
*/
public GameManager(int boardSize, String outputBoard, Random random) {
System.out.println("Generating a New Board");
if(boardSize>=2){
this.board=new Board(boardSize,random);}
//account for the case when the boardsize value is less than 2
else
{System.out.println("Use the default board size:4.");
this.board=new Board(4,random);}
this.outputFileName=outputBoard;
}
/*
* Name: GameManager(the constructor)
* Purpose: GameManager Constructor
It loads a saved game
* Parameters: String inputBoard:specify where to load the previously saved
* game from
* String outputBoard:specify where to save the game
* Random random: a random generator
* Return: void
*
*/
public GameManager(String inputBoard, String outputBoard, Random random)
throws IOException {
System.out.println("Loading Board from " + inputBoard);
this.board=new Board(inputBoard,random);
this.outputFileName=outputBoard;
}
/*
* Name: play
* Purpose:
// Main play loop
// Takes in input from the user to specify moves to execute
// valid moves are:
// k - Move up
// j - Move Down
// h - Move Left
// l - Move Right
// q - Quit and Save Board
//
// If an invalid command is received then print the controls
// to remind the user of the valid moves.
//
// Once the player decides to quit or the game is over,
// save the game board to a file based on the outputFileName
// string that was set in the constructor and then return
//
// If the game is over print "Game Over!" to the terminal
* Parameters: none
* Return: void
*
*/
public void play() throws IOException {
//print the controls
this.printControls();
//print out the current state of the 2048 board to the console
System.out.println(board.toString());
while(board.isGameOver()==false) {
Scanner userInput=new Scanner(System.in);
System.out.println("Enter the control key to move the tiles");
String userIn=userInput.next();
//check the user input's validility
while ((!userIn.equals("k"))&&(!userIn.equals("j"))&&
(!userIn.equals("h"))&&(!userIn.equals("l"))
&&(!userIn.equals("q")))
{ this.printControls();
System.out.println("Invalid control input,please re-enter");
userIn=userInput.next();
}
Direction dir=null;
if(userIn.equals("k"))
{dir=Direction.UP;}
if(userIn.equals("j"))
{dir=Direction.DOWN;}
if(userIn.equals("h"))
{dir=Direction.LEFT;}
if(userIn.equals("l"))
{dir=Direction.RIGHT;}
if(userIn.equals("q"))
{ //save the board to the outputBoard and quit the game
this.board.saveBoard(outputFileName);
//exit the method
return;
}
//check if the move is valid
while(this.board.canMove(dir)==false)
{ //if the board cannot move,print the controls and
//the current board again and prompt the user to re-enter
System.out.println("Cannot move in this direction");
this.printControls();
System.out.println(board.toString());
userIn=userInput.next();
while ((!userIn.equals("k"))&&(!userIn.equals("j"))&&
(!userIn.equals("h"))&&(!userIn.equals("l"))&&
(!userIn.equals("q")))
//if the input is not valid, print out the countrols again
//and prompt the user to enter another command
{ this.printControls();
System.out.println("Invalid control input,please re-enter");
userIn=userInput.next();
}
if(userIn.equals("k"))
{dir=Direction.UP;}
if(userIn.equals("j"))
{dir=Direction.DOWN;}
if(userIn.equals("h"))
{dir=Direction.LEFT;}
if(userIn.equals("l"))
{dir=Direction.RIGHT;}
if(userIn.equals("q"))
{ //save the board to the outputBoard and quit the game
this.board.saveBoard(outputFileName);
//exit the method
return;
}
}
//finally,if the input is valid,move the tile and add a random
//new tile
this.board.move(dir);
this.board.addRandomTile();
//print the updated board
System.out.println(board.toString()); }
//outside the while loop==>means the game is over
System.out.println("Game Over!");
//save the game and quit
this.board.saveBoard(outputFileName);
//exit the method
return;
}
/*
* Name: printControls
* Purpose: Print the Controls keys for the Game
* Parameters:none
* Return: void
*
*/
private void printControls() {
System.out.println(" Controls:");
System.out.println(" k - Move Up");
System.out.println(" j - Move Down");
System.out.println(" h - Move Left");
System.out.println(" l - Move Right");
System.out.println(" q - Quit and Save Board");
System.out.println();
}
}