-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.java
354 lines (298 loc) · 8.94 KB
/
Model.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import java.io.IOException;
/**
* Copyright (C) 2020 Intern Labs O!
* <p>
*
* Sokoban is a logical puzzle game in which the player moves boxes
* through a maze shown as a plan in order to put all the boxes
* in the specified final positions. Only one box can be moved at a time,
* and the hero of the game — the "storekeeper" — can only push the boxes,
* but not pull them. Since the game is quite difficult to recreate physically,
* it is usually implemented as a computer game.
*
* @author Argen Kasymov
*/
/**
* The Model class manages data and program logic.
* In the class, the Model is initialized by the Viewer to send data directly to the Viewer
* Allows you to load new levels
*/
public class Model {
private Viewer viewer;
private int[][] desktop;
private Levels levels;
private int indexX;
private int indexY;
private int[][] arrayIndexes;
Model(Viewer viewer) throws IOException {
this.viewer = viewer;
levels = new Levels();
desktop = levels.nextLevel();
indexX = 4;
indexY = 3;
initialization();
}
public void initialization() {
int counterFour = 0;
for (int i = 0; i < desktop.length; i++) {
for (int j = 0; j < desktop[i].length; j++) {
if (desktop[i][j] == 4) {
counterFour = counterFour + 1;
}
}
}
arrayIndexes = new int[2][counterFour];
int count = 0;
for (int i = 0; i < desktop.length; i++) {
for (int j = 0; j < desktop[i].length; j++) {
if (desktop[i][j] == 4) {
arrayIndexes[0][count] = i;
arrayIndexes[1][count] = j;
count++;
}
}
}
for (int i = 0; i < arrayIndexes.length; i++) {
for (int j = 0; j < arrayIndexes[i].length; j++) {
System.out.print(arrayIndexes[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println();
}
/**
* returns a map of the game
*/
public int[][] getDesktop() {
return desktop;
}
/**
* This function listens for driving directions from the controller
* Then updates the viewer and checks if the player has won
*
*/
public void move(String direction) {
System.out.println(direction);
switch (direction) {
case "Left":
moveLeft();
break;
case "Up":
moveUp();
break;
case "Right":
moveRight();
break;
case "Down":
moveDown();
break;
default:
return;
}
print();
check();
viewer.update();
won();
}
/**
* Step to the left
*/
private void moveLeft() {
if (desktop[indexX][indexY - 1] == 3) {
if (desktop[indexX][indexY - 2] == 0 || desktop[indexX][indexY - 2] == 4) {
desktop[indexX][indexY - 1] = 0;
desktop[indexX][indexY - 2] = 3;
}
}
if (desktop[indexX][indexY - 1] == 0 || desktop[indexX][indexY - 1] == 4) {
desktop[indexX][indexY] = 0;
indexY = indexY - 1;
desktop[indexX][indexY] = 1;
}
}
/**
* Step to the up
*/
private void moveUp() {
if (desktop[indexX - 1][indexY] == 3) {
if (desktop[indexX - 2][indexY] == 0 || desktop[indexX - 2][indexY] == 4) {
desktop[indexX - 1][indexY] = 0;
desktop[indexX - 2][indexY] = 3;
}
}
if (desktop[indexX - 1][indexY] == 0 || desktop[indexX - 1][indexY] == 4) {
desktop[indexX][indexY] = 0;
indexX = indexX - 1;
desktop[indexX][indexY] = 1;
}
}
/**
* Step to the right
*/
private void moveRight() {
if (desktop[indexX][indexY + 1] == 3) {
if (desktop[indexX][indexY + 2] == 0 || desktop[indexX][indexY + 2] == 4) {
desktop[indexX][indexY + 1] = 0;
desktop[indexX][indexY + 2] = 3;
}
}
if (desktop[indexX][indexY + 1] == 0 || desktop[indexX][indexY + 1] == 4) {
desktop[indexX][indexY] = 0;
indexY = indexY + 1;
desktop[indexX][indexY] = 1;
}
}
/**
* Step to the down
*/
private void moveDown() {
if (desktop[indexX + 1][indexY] == 3) {
if (desktop[indexX + 2][indexY] == 0 || desktop[indexX + 2][indexY] == 4) {
desktop[indexX + 1][indexY] = 0;
desktop[indexX + 2][indexY] = 3;
}
}
if (desktop[indexX + 1][indexY] == 0 || desktop[indexX + 1][indexY] == 4) {
desktop[indexX][indexY] = 0;
indexX = indexX + 1;
desktop[indexX][indexY] = 1;
}
}
/**
* Checks whether the player has won
*/
private void check() {
int t = 0;
for (int j = 0; j < arrayIndexes[0].length; j++) {
int x = arrayIndexes[0][t];
int y = arrayIndexes[1][t];
if (desktop[x][y] == 0) {
desktop[x][y] = 4;
break;
}
t++;
}
}
/**
* Checks whether the player has won
* If you win, it takes the player to the next level
*/
private void won() {
boolean flag = true;
int t = 0;
for (int i = 0; i < arrayIndexes[0].length; i++) {
int x = arrayIndexes[0][t];
int y = arrayIndexes[1][t];
if (desktop[x][y] != 3) {
flag = false;
break;
}
t++;
}
if (flag) {
javax.swing.JOptionPane.showMessageDialog(viewer.getMyFrame(), "You won!");
desktop = levels.nextLevel();
indexX = 4;
indexY = 3;
initialization();
viewer.update();
}
}
private void print() {
for (int i = 0; i < desktop.length; i++) {
for (int j = 0; j < desktop[i].length; j++) {
System.out.print(desktop[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println();
}
/**
* doAction
* Basic logic of the program
* The controller sends a command,
* the model examines it, and when detected,
* performs this action,
* after which the break statement is called
* @param choice stores the command name
*/
public void menuAction(String choice) {
if (choice.equals("restart game")) {
desktop = null;
desktop = levels.restartGame();
indexX = 4;
indexY = 3;
initialization();
viewer.update();
} else if (choice.equals("level 1")) {
desktop = null;
desktop = levels.firstLevel();
indexX = 4;
indexY = 3;
initialization();
viewer.update();
} else if (choice.equals("level 2")) {
desktop = null;
desktop = levels.secondLevel();
indexX = 4;
indexY = 3;
initialization();
viewer.update();
} else if (choice.equals("level 3")) {
desktop = null;
desktop = levels.thirdLevel();
indexX = 4;
indexY = 3;
initialization();
viewer.update();
} else if (choice.equals("level 4")) {
desktop = null;
desktop = levels.fourthLevel();
indexX = 4;
indexY = 3;
initialization();
viewer.update();
} else if (choice.equals("add level")) {
//desktop = null;
desktop = levels.readLevel(viewer.openFileChooser());
indexX = 4;
indexY = 3;
initialization();
viewer.update();
} else if (choice.equals("fifth")) {
desktop = null;
desktop = levels.readLevelFromServer(5);
indexX = 4;
indexY = 3;
initialization();
viewer.update();
}
else if (choice.equals("sixth")) {
desktop = null;
desktop = levels.readLevelFromServer(6);
indexX = 4;
indexY = 3;
initialization();
viewer.update();
}
else if (choice.equals("seventh")) {
desktop = null;
desktop = levels.readLevelFromServer(7);
indexX = 4;
indexY = 3;
initialization();
viewer.update();
}
else if (choice.equals("quit")) {
exit();
}
}
/**
* Exit the program
*/
private void exit() {
System.exit(0);
}
}