-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board
294 lines (218 loc) · 7.63 KB
/
Board
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
package Com.Game;
public class Board { // logic untuk memainkan game.
public Tile[][] board;
int grids = 4;
int border = 0;
static int score = 0;
public static void value(int x) {
score = x;
}
public Board() {
board = new Tile[4][4];
for ( int i = 0; i < board.length; i++ ){
for ( int j = 0; j < board.length; j++ ){
board[i][j] = new Tile();
}
}
}
public Tile[][] getBoard(){
return board;
}
public static int getScore() { // untuk mendapatkan skor
return score;
}
public int getHighTile() { // angka paling tinggi di board
int high = board[0][0].getValue();
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
if(board[i][j].getValue() > high) high = board[i][j].getValue();
}
}
return high;
}
public void print() { // output angka
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
String s = board[i][j].toString() + " ";
System.out.print(s);
}
System.out.println("");
}
System.out.println("Score: " + score);
}
public String toString() { // output angka
String s = "";
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
s += board[i][j].toString() + "";
}
s += "\n";
}
return s;
}
public void spawn() { // untuk menampilkan lokasi random milik tile 2 dan tile 4.
boolean empty = true;
while(empty){
int row = (int) (Math.random() * 4);
int col = (int) (Math.random() * 4);
double x = Math.random();
if(board[row][col].getValue() == 0) {
if(x < 0.2) {
board[row][col] = new Tile(4);
empty = false;
}
else {
board[row][col] = new Tile(2);
empty = false;
}
}
}
}
public boolean blackOut() { // untuk cek apakah papan mengalami blackout tapi masih ada angka yang bisa dicombine
int count = 0;
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
if(board[i][j].getValue() > 0) count++;
}
}
if(count == 16) return true;
return false;
}
public boolean gameOver() { // papan sudah penuh dan tidak ada angka yang bisa dicombine.
int count = 0;
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
if ( board[i][j].getValue() > 0 ){
if ( i == 0 && j == 0 ){
if ( board[i][j].getValue() != board[i + 1][j].getValue() && board[i][j].getValue() != board[i][j + 1].getValue() ){
count++;
}
}
else if ( i == 0 && j == 3 ){
if ( board[i][j].getValue() != board[i + 1][j].getValue()&& board[i][j].getValue() != board[i][j - 1].getValue() ){
count++;
}
}
else if ( i == 3 && j == 3 ){
if ( board[i][j].getValue() != board[i - 1][j].getValue() && board[i][j].getValue() != board[i][j - 1].getValue() ){
count++;
}
}
else if ( i == 3 && j == 0 ){
if ( board[i][j].getValue() != board[i - 1][j].getValue() && board[i][j].getValue() != board[i][j + 1].getValue() ){
count++;
}
}
else if ( i == 0 && ( j == 1 || j == 2 )){
if ( board[i][j].getValue() != board[i + 1][j].getValue() && board[i][j].getValue() != board[i][j + 1].getValue()
&& board[i][j].getValue() != board[i][j - 1].getValue() ){
count++;
}
}
else if ( i == 3 && ( j == 1 || j == 2 ) ){
if ( board[i][j].getValue() != board[i - 1][j].getValue() && board[i][j].getValue() != board[i][j + 1].getValue()
&& board[i][j].getValue() != board[i][j - 1].getValue() ){
count++;
}
}
else if ( j == 0 && ( i == 1 || i == 2 ) ){
if ( board[i][j].getValue() != board[i][j + 1].getValue() && board[i][j].getValue() != board[i - 1][j].getValue()
&& board[i][j].getValue() != board[i + 1][j].getValue() ){
count++;
}
}
else if ( j == 3 && ( i == 1 || i == 2 ) ){
if ( board[i][j].getValue() != board[i][j - 1].getValue() && board[i][j].getValue() != board[i - 1][j].getValue()
&& board[i][j].getValue() != board[i + 1][j].getValue() ){
count++;
}
}
else{
if ( board[i][j].getValue() != board[i][j - 1].getValue()&& board[i][j].getValue() != board[i][j + 1].getValue()
&& board[i][j].getValue() != board[i - 1][j].getValue() && board[i][j].getValue() != board[i + 1][j].getValue() ){
count++;
}
}
}
}
}
if ( count == 16 ){
return true;
}
return false;
}
public void right() { // untuk gerak kekanan
for(int i = 0; i < grids; i++) {
border = (grids - 1);
for(int j = grids - 1; j >= 0; j--) {
if(board[i][j].getValue() != 0) {
if(border >= j) horizontalMove(i, j, "right");
}
}
}
}
public void left() { // untuk gerak ke kiri
for(int i = 0; i < grids; i++) {
border = 0;
for(int j = 0; j < grids; j++) {
if(board[i][j].getValue() != 0) {
if(border <= j) horizontalMove(i, j, "left");
}
}
}
}
public void horizontalMove(int row, int col, String direction) { // untuk cek gerak arah horizontal
Tile x = board[row][border];
Tile compare = board[row][col];
if(x.getValue() == 0 || x.getValue() == compare.getValue()) {
if(col > border || (direction.equals("right") && (col < border))) {
int addScore = x.getValue() + compare.getValue();
if(x.getValue() != 0) score += addScore;
x.setValue(addScore);
compare.setValue(0);
}
}
else {
if(direction.equals("right")) border--;
else border++;
horizontalMove(row, col, direction);
}
}
public void up() { // untuk gerak ke atas
for(int i = 0; i < grids; i++) {
border = 0;
for(int j = 0; j < grids; j++) {
if(board[j][i].getValue() != 0) {
if(border <= j) verticalMove(j, i, "up");
}
}
}
}
public void down() { // untuk gerak ke bawah
for(int i = 0; i < grids; i++) {
border = (grids - 1);
for(int j = grids - 1; j >= 0; j--) {
if(board[j][i].getValue() != 0) {
if(border >= j) verticalMove(j, i, "down");
}
}
}
}
public void verticalMove(int row, int col, String direction) { // untuk gerak vertical
Tile x = board[border][col];
Tile compare = board[row][col];
if(x.getValue() == 0 || x.getValue() == compare.getValue()) {
if(row > border || (direction.equals("down") && (row < border))) {
int addScore = x.getValue() + compare.getValue();
if(x.getValue() != 0) score += addScore;
x.setValue(addScore);
compare.setValue(0);
}
}
else {
if(direction.equals("down")) border--;
else border++;
verticalMove(row, col, direction);
}
}
}