-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwentyFortyEight.java
73 lines (68 loc) · 1.51 KB
/
TwentyFortyEight.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
import java.util.Scanner;
public class TwentyFortyEight {
public static int[][] grid;
public static boolean[][] alreadyMerged;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
grid = new int[4][4];
alreadyMerged = new boolean[4][4];
String[] in;
for(int i=0;i<4;i++) {
in = scan.nextLine().split(" ");
for(int j=0;j<4;j++) {
grid[i][j] = Integer.parseInt(in[j]);
}
}
int dir = Integer.parseInt(scan.nextLine());
int dr = iff(-1,dir==1) + iff(1,dir==3);
int dc = iff(-1,dir==0) + iff(1,dir==2);
if(dir==0 || dir==1) {
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
if(grid[i][j] != 0)
slide(i,j,dr,dc);
}
}
}
else {
for(int i=3;i>-1;i--) {
for(int j=3;j>-1;j--) {
if(grid[i][j] != 0)
slide(i,j,dr,dc);
}
}
}
for(int i=0;i<4;i++) {
for(int j=0;j<3;j++) {
System.out.print(grid[i][j]+" ");
}
System.out.println(grid[i][3]);
}
}
public static void slide(int r, int c, int dr, int dc) {
int i=1;
while(safeCheck(r+i*dr,c+i*dc)==0)
i++;
if(safeCheck(r+i*dr,c+i*dc)==grid[r][c] && !alreadyMerged[r+i*dr][c+i*dc]){
grid[r][c]=0;
grid[r+i*dr][c+i*dc]*=2;
alreadyMerged[r+i*dr][c+i*dc] = true;
}
else {
if(i!=1) {
grid[r+(i-1)*dr][c+(i-1)*dc] = grid[r][c];
grid[r][c]=0;
}
}
}
public static int safeCheck(int r, int c) {
if(r<0 || r>3 || c<0 || c>3)
return -1;
return grid[r][c];
}
public static int iff(int n, boolean b) {
if(b)
return n;
return 0;
}
}