-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path2048.java
More file actions
64 lines (48 loc) · 1.13 KB
/
2048.java
File metadata and controls
64 lines (48 loc) · 1.13 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
import java.util.*;
public class 2048 {
public static void move(int[] nums) {
boolean[] merge = new boolean[4];
for (int i = 0; i < nums.length; i++)
for (int j = i; j > 0 && !merge[j - 1]; j--)
{
if (nums[j - 1] == 0)
{
nums[j - 1] = nums[j];
nums[j] = 0;
}
else if (nums[j - 1] == nums[j])
{
nums[j - 1] *= 2;
nums[j] = 0;
merge[j - 1] = true;
break;
}
else
break;
}
}
public static int[][] rotate(int[][] nums) {
int[][] rot = new int[4][4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
rot[3 - j][i] = nums[i][j];
return rot;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] nums = new int[4][4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
nums[i][j] = scan.nextInt();
int move = scan.nextInt();
for (int i = 0; i < move; i++)
nums = rotate(nums);
for (int i = 0; i < 4; i++)
move(nums[i]);
for (int i = 0; i < 4 - move; i++)
nums = rotate(nums);
for (int i = 0; i < 4; i++)
System.out.println(Arrays.toString(nums[i]).substring(1 , Arrays.toString(nums[i]).length() - 1).replace("," , ""));
scan.close();
}
}