-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.java
203 lines (188 loc) · 6.9 KB
/
Worker.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
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
public class Worker {
private String name = "";
int positionX = 0;
int positionY = 0;
public Worker(String name, int positionX, int positionY) { // constructor
this.setName(name);
this.positionX = positionX;
this.positionY = positionY;
}
// to initilize the position of the worker at the start of the game
//changed
public void placeWorker(int positionX, int positionY) {
this.positionX = positionX;
this.positionY = positionY;
}
//change the position of the worker and return true if possible, otherwise return false.
public boolean move(int newPositionX, int newPositionY, SantoriniGame game) {
boolean doMove = true;
if (positionY == 0) { // checks if the worker is in the first row
if (positionX == 0) { // check if worker is in the upper left corner
if (newPositionX == (positionX + 1) && newPositionY == 0) {
doMove = true;
} else if (newPositionX == 0 && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY + 1)) {
doMove = true;
} else {
doMove = false;
}
}
if (positionX == 4) { // checks if worker is in the upper right corner
if (newPositionX == (positionX - 1) && newPositionY == 0) {
doMove = true;
} else if (newPositionX == 4 && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY + 1)) {
doMove = true;
} else {
doMove = false;
}
} else {
if (newPositionX == (positionX + 1) && newPositionY == 0) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == 0) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX) && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY + 1)) {
doMove = true;
} else {
doMove = false;
}
}
} else if (positionY == 4) { // check if worker is in the last row
if (positionX == 0) { // check if worker is in the lower left corner
if (newPositionX == (positionX + 1) && newPositionY == 4) {
doMove = true;
} else if (newPositionX == 0 && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else {
doMove = false;
}
}
if (positionX == 4) { // checks if worker is in the lower right corner
if (newPositionX == (positionX - 1) && newPositionY == 4) {
doMove = true;
} else if (newPositionX == 4 && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else {
doMove = false;
}
} else {
if (newPositionX == (positionX + 1) && newPositionY == 4) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == 4) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else {
doMove = false;
}
}
}
if (positionX == 0) { // checks if worker is in the first column
if (positionY != 0 || positionY != 4) {
if (newPositionX == (positionX) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX) && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY)) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY + 1)) {
doMove = true;
}
}
} else if (positionX == 4) { // checks if worker is in the last column
if (positionY != 0 || positionY != 4) {
if (newPositionX == (positionX) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX) && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY + 1)) {
doMove = true;
}
}
}
if ((positionX != 0 || positionX != 4)
&& (positionY != 0 || positionY != 4)) {
// possible cells if worker is in the middle
if (newPositionX == (positionX + 1) && newPositionY == (positionY)) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY - 1)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY)) {
doMove = true;
} else if (newPositionX == (positionX - 1) && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX) && newPositionY == (positionY + 1)) {
doMove = true;
} else if (newPositionX == (positionX + 1) && newPositionY == (positionY + 1)) {
doMove = true;
} else {
doMove = false;
}
}
//to check if adjacent cell can be climbed or jumped on them
if (game.board[positionY][positionX].equals("") ) {
switch (game.board[newPositionY][newPositionX]) {
case "": break;
case "B": break;
default: doMove = false;
}
} else if (game.board[positionY][positionX].equals("B")) {
switch (game.board[newPositionY][newPositionX]) {
case "": break;
case "BB": break;
default: doMove = false;
}
} else if (game.board[positionY][positionX].equals("BB")) {
switch (game.board[newPositionY][newPositionX]) {
case "": break;
case "B": break;
case "BBB": break;
default: doMove = false;
}
}
//this is the last checking of conditions
//to check if the cell to be moved to is not occupied by another worker
for (int i=0; i<4; i++) {
if (newPositionX == game.workers[i].positionX &&
newPositionY == game.workers[i].positionY) {
doMove = false;
}
if (i==3) {
positionX = newPositionX;
positionY = newPositionY;
doMove = true;
}
}
return doMove ;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}