-
Notifications
You must be signed in to change notification settings - Fork 0
/
T048.java
332 lines (301 loc) · 9.36 KB
/
T048.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*====================================
class T048 -- implements the game 2048
implements the Game interface
uses Board as data type
====================================*/
import cs1.Keyboard;
public class T048 implements Game{
//instance variables
private Board _board;
//constructor
public T048() {
_board = new Board(4);
_board.populate(0);
addNewTile();
addNewTile();
}
/*====================================
adds a new tile to the board in a random location
1/10 chance of the tile being a 4, 9/10 of a 2
====================================*/
public void addNewTile() {
int valToAdd;
if (Math.random() * 10 < 1) valToAdd = 4;
else valToAdd = 2;
int r = (int)(Math.random() * 4);
int c = (int)(Math.random() * 4);
//making sure its not overwriting an existing value
while ( _board.get(r,c) != 0 ) {
r = (int)(Math.random() * 4);
c = (int)(Math.random() * 4);
}
_board.set(r,c, valToAdd);
}
/*====================================
swipe left: swipes left, combining necessary tiles
returns true if a swipe was able to happen and did happen,
false if you cannot swipe left
====================================*/
public boolean swipeL() {
//to compare to new one after a swipe
String old = _board.toString();
//for each row
for (int r = 0; r < 4; r++){
//first to combine similar tiles
//for the first 3 tiles
for (int c = 0; c < 3; c++){
//if you find a meaningful element . . .
if ( !_board.get(r, c).equals(0)){
//for the rest of the tiles . . .
for (int n = c + 1; n < 4; n++){
if (_board.get(r, c).equals( _board.get(r, n) )){
_board.set(r, c, 2 * (Integer)_board.get(r, c) );
_board.set(r, n, 0 );
break;
}
else if (!_board.get(r, n).equals(0)) break;
}
}
}
//then to get rid of 0s and move everything over
//for the first 3 tiles again
for (int c = 0; c < 3; c++){
//if you find a 0
if (_board.get(r, c).equals(0)) {
//for the rest of the tiles . . .
for (int n = c + 1; n < 4; n++){
if (!_board.get(r, n).equals(0)){
_board.set(r, c, _board.get(r, n) );
_board.set(r, n, 0 );
break;
}
}
}
}
}
if (old.equals(_board.toString())) return false;
return true;
}
/*====================================
swipe right: swipes right, combining necessary tiles
returns true if a swipe was able to happen and did happen,
false if you cannot swipe right
====================================*/
public boolean swipeR() {
//to compare to new one after a swipe
String old = _board.toString();
//for each row
for (int r = 0; r < 4; r++){
//first to combine similar tiles
//for the first 3 tiles
for (int c = 3; c > 0; c--){
//if you find a meaningful element . . .
if ( !_board.get(r, c).equals(0)){
//for the rest of the tiles . . .
for (int n = c - 1; n >= 0; n--){
if (_board.get(r, c).equals( _board.get(r, n) )){
_board.set(r, c, 2 * (Integer)_board.get(r, c) );
_board.set(r, n, 0 );
break;
}
else if (!_board.get(r, n).equals(0)) break;
}
}
}
//then to get rid of 0s and move everything over
//for the first 3 tiles again
for (int c = 3; c > 0; c--){
//if you find a 0
if (_board.get(r, c).equals(0)) {
//for the rest of the tiles . . .
for (int n = c - 1; n >= 0; n--){
if (!_board.get(r, n).equals(0)){
_board.set(r, c, _board.get(r, n) );
_board.set(r, n, 0 );
break;
}
}
}
}
}
if (old.equals(_board.toString())) return false;
return true;
}
/*====================================
swipe down: swipes down, combining necessary tiles
returns true if a swipe was able to happen and did happen,
false if you cannot swipe down
====================================*/
public boolean swipeD() {
//to compare to new one after a swipe
String old = _board.toString();
//for each column
for (int c = 0; c < 4; c++){
//first to combine similar tiles
//for the first 3 tiles
for (int r = 3; r > 0; r--){
//if you find a meaningful element . . .
if ( !_board.get(r, c).equals(0)){
//for the rest of the tiles . . .
for (int n = r - 1; n >=0; n--){
if (_board.get(r, c).equals( _board.get(n, c) )){
_board.set(r, c, 2 * (Integer)_board.get(r, c) );
_board.set(n, c, 0 );
break;
}
else if (!_board.get(n, c).equals(0)) break;
}
}
}
//then to get rid of 0s and move everything over
//for the first 3 tiles again
for (int r = 3; r > 0; r--){
//if you find a 0
if (_board.get(r, c).equals(0)) {
//for the rest of the tiles . . .
for (int n = r - 1; n >=0; n--){
if (!_board.get(n, c).equals(0)){
_board.set(r, c, _board.get(n, c) );
_board.set(n, c, 0 );
break;
}
}
}
}
}
if (old.equals(_board.toString())) return false;
return true;
}
/*====================================
swipe up: swipes up, combining necessary tiles
returns true if a swipe was able to happen and did happen,
false if you cannot swipe up
====================================*/
public boolean swipeU() {
//to compare to new one after a swipe
String old = _board.toString();
//for each column
for (int c = 0; c < 4; c++){
//first to combine similar tiles
//for the first 3 tiles
for (int r = 0; r < 3; r++){
//if you find a meaningful element . . .
if ( !_board.get(r, c).equals(0)){
//for the rest of the tiles . . .
for (int n = r + 1; n < 4; n++){
if (_board.get(r, c).equals( _board.get(n, c) )){
_board.set(r, c, 2 * (Integer)_board.get(r, c) );
_board.set(n, c, 0 );
break;
}
else if (!_board.get(n, c).equals(0)) break;
}
}
}
//then to get rid of 0s and move everything over
//for the first 3 tiles again
for (int r = 0; r < 3; r++){
//if you find a 0
if (_board.get(r, c).equals(0)) {
//for the rest of the tiles . . .
for (int n = r + 1; n < 4; n++){
if (!_board.get(n, c).equals(0)){
_board.set(r, c, _board.get(n, c) );
_board.set(n, c, 0 );
break;
}
}
}
}
}
if (old.equals(_board.toString())) return false;
return true;
}
/*====================================
displays the instructions at the start of the game, so the user will be able to play
====================================*/
public void displayInstructions(){
String s;
s = "This is a game of 2048. The objective is to get a tile with the face 2048, by combining tiles in a smart way.\n";
System.out.println(s);
s = "To play, you can swipe up, down, left, or right. The keys are as follows:\n";
s+="\t* w : up\n";
s+="\t* a : left\n";
s+="\t* s : down\n";
s+="\t* d : right\n";
System.out.println(s);
s = "When you swipe, if two tiles with the same face are push against eachother, they will combine to a tile with twice the value!\n";
System.out.println(s);
s = "REMEMBER: you must hit enter after every keystroke for it to proccess\n";
System.out.println(s);
s = "Good luck! :)";
System.out.println(s);
}
/*====================================
plays one turn of 2048
====================================*/
public void playTurn(){
String s = Keyboard.readString();
if ( s.equals("w") && swipeU() ) addNewTile();
else if ( s.equals("a") && swipeL() ) addNewTile();
else if ( s.equals("s") && swipeD() ) addNewTile();
else if ( s.equals("d") && swipeR() ) addNewTile();
else return;//if the input is not w a s or d, do nothing
//System.out.println("\n" + _board);
System.out.println(_board);
}
public boolean isGameOver() {
return _board.contains(2048) || noMoves();
}
public boolean noMoves() {
T048 copy = new T048();
for (int r = 0; r < _board.size(); r++) {
for (int c = 0; c < _board.size(); c++) {
copy._board.set( r,c, _board.get(r,c) );
}
}
return !( copy.swipeU() || copy.swipeD() || copy.swipeL() || copy.swipeR() );
}
/*===================================
playGame -- play a game of 2048 with an EXISTING T048 object created
====================================*/
public int playGame() {
displayInstructions();
System.out.println(_board);
while ( ! isGameOver() )
playTurn();
if ( _board.contains(2048) ) return 3;
else if ( _board.contains(1024) ) return 2;
else if ( _board.contains(512) ) return 1;
else return 0;
}
/*====================================
plays an entire game of 2048, from STATIC
therefore will be called as T048.play()
====================================*/
public static int play(){
T048 t = new T048();
return t.playGame();
}
/*===================================
main() (for testing)
====================================*/
public static void main(String[] args) {
int creds = play();
System.out.println("You earned " + creds + " credits!");
/*
T048 t = new T048();
t._board.set(3,0,1024);
t._board.set(3,1,512);
t._board.set(3,2,256);
t._board.set(3,3,128);
t._board.set(2,3,64);
t._board.set(2,2,32);
t._board.set(2,1,16);
t._board.set(2,0,8);
t._board.set(1,0,4);
t._board.set(1,1,2);
t._board.set(1,2,2);
t.playGame();*/
}//end main
}//end class T048