-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.java
More file actions
281 lines (248 loc) · 6.82 KB
/
Copy pathGameState.java
File metadata and controls
281 lines (248 loc) · 6.82 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
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
/**This file is used to define a GameState class and implement some method
to set the board and move the tiles on the board.
Name: Huaner Wang Email: huw005@ucsd.edu
*/
import java.util.Random;
/*This class is used to set the board, move the tiles and get the Score
*/
public class GameState{
private Random rng;
private int[][] board;
private int score;
Random rand=new Random();
// REQUIRED - put this in your GameState.java
public String toString () {
StringBuilder outputString = new StringBuilder();
outputString.append(String.format("Score: %d\n", getScore()));
for (int row = 0; row < getBoard().length; row++) {
for (int column = 0; column < getBoard()[0].length; column++) {
outputString.append(getBoard()[row][column] == 0 ? " -" :
String.format("%5d", getBoard()[row][column]));
}
outputString.append("\n");
}
return outputString.toString();
}
/*constructor that create new object*/
public GameState (int numRows, int numCols){
this.board=new int[numRows][numCols];
this.score=0;
}
/*get the board we set with numbers*/
public int[][] getBoard (){
int[][] newboard=new int[board.length][board[0].length];
for(int row=0; row<board.length; row++){
for(int col=0; col<board[row].length; col++){
newboard[row][col]=this.board[row][col];
}
}return newboard;
}
/*set the board with numbers*/
public void setBoard (int[][] newBoard){
if(newBoard==null){
return;
}
this.board=new int[newBoard.length][newBoard[0].length];
for(int row=0; row<newBoard.length;row++){
for(int col=0; col<newBoard[0].length;col++){
this.board[row][col]=newBoard[row][col];
}
}
}
/*get the score*/
public int getScore (){
return this.score;
}
/*set score with new score*/
public void setScore (int newScore){
this.score=newScore;
}
/*return a radom number within the input range*/
protected int rollRNG (int bound){
return rand.nextInt(bound);
}
/*return 2 with 70% and 4 with 30%*/
protected int randomTile(){
boolean value = rand.nextInt(100) < Config.TWO_PROB;
if(value==true){
return 2;
}else{
return 4;
}
}
/*count tile that equals to zero on the board*/
protected int countEmptyTiles (){
int count=0;
for(int row=0;row<this.board.length;row++){
for(int col=0;col<this.board[row].length; col++){
if (this.board[row][col]==0){
count++;
}
}
}return count;
}
/*add a new tile on the board when it's possible*/
protected int addTile (){
if(countEmptyTiles()==0){
return 0;
}
int boardlength=this.board.length;
int boardwidth=this.board[0].length;
int roll1=rollRNG(boardlength);
int roll2=rollRNG(boardwidth);
while(this.board[roll1][roll2]!=0){
roll1=rollRNG(boardlength);
roll2=rollRNG(boardwidth);
}
this.board[roll1][roll2]=randomTile();
return this.board[roll1][roll2];
}
/*rotate the board rotateCounterClockwise once*/
protected void rotateCounterClockwise (){
int[][] copy=new int[this.board.length][this.board[0].length];
for(int row=0; row<this.board.length;row++){
for(int col=0; col<this.board[row].length;col++){
copy[row][col]=this.board[row][col];
}
}
for(int row=0; row<this.board.length;row++){
for(int col=0; col<this.board[row].length;col++){
this.board[row][col]=copy[col][this.board[0].length-1-row];
}
}
}
/*check if the tile can slide down*/
protected boolean canSlideDown (){
for(int column=0;column<this.board[0].length;column++){
for(int row=0;row<this.board.length-1;row++){
if(this.board[row][column]!=0){
if(this.board[row][column]==this.board[row+1][column]||this.board[row+1][column]==0){
return true;
}
}
}
}return false;
}
/*check if the tile can move anymore*/
public boolean isGameOver (){
boolean gameover=true;
if(canSlideDown()){
gameover=false;
return gameover;
}
rotateCounterClockwise();
if(canSlideDown()){
gameover=false;
return gameover;
}
if(this.canSlideDown()){
gameover=false;
return gameover;
}
this.rotateCounterClockwise();
if(this.canSlideDown()){
gameover=false;
return gameover;
}
return gameover;
}
/*slide the tile down and coun the score*/
protected boolean slideDown(){
boolean flag=false;
for(int col=0;col<board[0].length;col++){
int scale=board.length-1;
for(int row=board.length-2; row>=0;row--){
if(canSlideDown()){
int moveposition=row;//switch column to the top and row to the bottom
if(board[row][col]!=0){
int index=row+1;
while(index<=board.length-1){
if(board[index][col]==0){
moveposition=index;
}
index+=1;
}
if(moveposition!=row){
board[moveposition][col]=board[row][col];
board[row][col]=0;
}
int belowitem=moveposition+1;
if(belowitem<=scale){
// System.out.println("y-1 "+board[moveposition][j]);
//System.out.println("y "+board[y][col]);
if(board[moveposition][col]==board[belowitem][col]){
board[belowitem][col]*=2;
board[moveposition][col]=0;
scale=moveposition;
System.out.println(score);
// System.out.println("moveposition"+moveposition);
// System.out.println("scale"+scale);
score+=board[belowitem][col];
}
}
}
flag=true;
}
}
}System.out.println("return is "+flag);
return flag;
}
/*move the tile in different direction*/
public boolean move (Direction dir){
if(dir==null){
return false;
}
if(dir.getRotationCount()==0){
slideDown();
addTile();
return true;//down
}else if(dir.getRotationCount()==1){
//left
rotateCounterClockwise();
slideDown();
rotateCounterClockwise();
rotateCounterClockwise();
rotateCounterClockwise();
addTile();
return true;
}else if(dir.getRotationCount()==2){
rotateCounterClockwise();
rotateCounterClockwise();
slideDown();
rotateCounterClockwise();
rotateCounterClockwise();
addTile();
return true;
}else if(dir.getRotationCount()==3){
//right
rotateCounterClockwise();
rotateCounterClockwise();
rotateCounterClockwise();
slideDown();
rotateCounterClockwise();
addTile();
return true;
}else{
return false;
}
//return false;
}
public static void main(String[] args){
GameState gm=new GameState(4,4);
int[][] hh=new int[][]{
{4,0,0,0},
{4,0,0,0},
{8,0,0,0},
{8,0,0,0}
};
gm.setBoard(hh);
boolean x=gm.slideDown();
//System.out.print(x);
for(int i=0;i<gm.board.length;i++){
for(int j=0;j<gm.board[i].length;j++){
System.out.print(gm.board[i][j]);
// System.out.print(x);
}System.out.println("\n");
}
}
}