-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCryptica.java
148 lines (134 loc) · 4.72 KB
/
Cryptica.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
public class Cryptica {
private Stone[][] board;
public Cryptica(int numRows, int numCols) {
board = new Stone[numRows][numCols];
}
public Cryptica(Cryptica crypt) {
board = new Stone[crypt.board.length][crypt.board[0].length];
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
board[r][c] = crypt.board[r][c];
}
}
}
public boolean solved() {
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board[r].length; c++)
if (board[r][c] != null && board[r][c].isTempleStone())
if (!board[r][c].hasDestination(r, c))
return false;
return true;
}
public void addTempleStone(int row, int col, int destRow, int destCol) {
board[row][col] = new Stone(destRow, destCol);
}
public void addMovingStone(int row, int col) {
board[row][col] = Stone.MOVING_STONE;
}
public void addBlockingStone(int row, int col) {
board[row][col] = Stone.BLOCKING_STONE;
}
public Cryptica moveRight() {
for (int r = 0; r < board.length; r++) {
for (int c = board[r].length - 1; c > 0; c--) {
if (board[r][c] == null && board[r][c-1] != null &&
!board[r][c-1].isBlockingStone()) {
board[r][c] = board[r][c-1];
board[r][c-1] = null;
}
}
}
return this;
}
public Cryptica moveLeft() {
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length - 1; c++) {
if (board[r][c] == null && board[r][c+1] != null &&
!board[r][c+1].isBlockingStone()) {
board[r][c] = board[r][c+1];
board[r][c+1] = null;
}
}
}
return this;
}
public Cryptica moveDown() {
for (int r = board.length - 1; r > 0; r--) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == null && board[r-1][c] != null &&
!board[r-1][c].isBlockingStone()) {
board[r][c] = board[r-1][c];
board[r-1][c] = null;
}
}
}
return this;
}
public Cryptica moveUp() {
for (int r = 0; r < board.length - 1; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == null && board[r+1][c] != null &&
!board[r+1][c].isBlockingStone()) {
board[r][c] = board[r+1][c];
board[r+1][c] = null;
}
}
}
return this;
}
@Override
public int hashCode() {
int ret = 0;
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == null || board[r][c].isBlockingStone())
continue;
int val;
if (board[r][c].isMovingStone()) {
val = c * board.length + r;
val *= 36;
} else {
val = c * board.length + r;
val *= 36;
val += ((board[r][c].getDestCol()) * board.length + board[r][c].getDestRow() + 1);
}
ret *= 36 * 36;
ret += val;
}
}
return ret;
}
@Override
public boolean equals(Object o) {
Cryptica other = (Cryptica)o;
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] != null && other.board[r][c] == null)
return false;
if (board[r][c] == null && other.board[r][c] != null)
return false;
if (board[r][c] != null && other.board[r][c] != null && !board[r][c].equals(other.board[r][c]))
return false;
}
}
return true;
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == null)
str.append('.');
else if (board[r][c].isBlockingStone())
str.append('#');
else if (board[r][c].isMovingStone())
str.append('@');
else
str.append('*');
}
str.append('\n');
}
return str.toString().trim();
}
}