-
Notifications
You must be signed in to change notification settings - Fork 0
/
ternisController.java
365 lines (361 loc) · 12.4 KB
/
ternisController.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
355
356
357
358
359
360
361
362
363
364
365
package finalProject;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
public class ternisController{
@FXML
private AnchorPane pane;
@FXML
private Label secondCounter;
public double difficulty;
private KeyCode currentKeyCode=null;
private double duration=1;
private double frequency;
private int changeCounter=0;
private int counter=0;
private int timer=0;
private Timeline time;
private Timeline gameTimeline;
private String[] A=new String[5];
private int[][][] position=new int[10][20][2];
private Color[][] color=new Color[10][20];
private Rectangle[][] element=new Rectangle[10][20];
private Color bgc;
private tetris currentTetris;
ObservableList<Node> children;
public void Initialize(Scene scene) {
difficulty=Math.sqrt(difficulty);
duration=(11-difficulty)*50;
frequency=(1 + difficulty)*2;
A[0]="←";A[1]="→";A[2]="⇊";A[3]="↓";A[4]="↺";
bgc=Color.rgb(0, 31, 62);
secondCounter.setText("You've waist "+this.timer+
" second A:"+A[0]+" D:"+A[1]+" Space:"+A[2]+" S:"+A[3]+" W:"+A[4]);
children=pane.getChildren();
for(int i=0;i<=20;i++){
Line line=new Line(50,50+20*i,250,50+20*i);
line.setStroke(Color.WHITE);
line.setStrokeWidth(0.1);
children.add(line);
}
for(int i=0;i<=10;i++){
Line line=new Line(50+20*i,50,50+20*i,450);
line.setStrokeWidth(0.1);
line.setStroke(Color.WHITE);
children.add(line);
}
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
position[i][j][0]=50+20*i;
position[i][j][1]=50+20*j;
color[i][j]=bgc;
}
}
gameTimeline=new Timeline(new KeyFrame(Duration.millis(1),e->setNextFrame()));
time=new Timeline(
new KeyFrame(Duration.millis(1000),e->{
this.timer++;
if(this.timer%20==0){
frequency*=0.9;
duration*=0.9;
}
changeCounter++;
secondCounter.setText("You've waist "+this.timer+
" second A:"+A[0]+" D:"+A[1]+" Space:"+A[2]+" S:"+A[3]+" W:"+A[4]);
if(changeCounter>=frequency){
changeCounter=0;
Random random=new Random();
for(int i=0;i<5;i++){
String temp;
int t=random.nextInt(5);
temp=A[t];
A[t]=A[0];
A[0]=temp;
}
}
})
);
gameTimeline.setCycleCount(Timeline.INDEFINITE);
time.setCycleCount(Timeline.INDEFINITE);
gameTimeline.play();
time.play();
scene.setOnKeyPressed(e->currentKeyCode=e.getCode());
Random random=new Random();
currentTetris=new tetris(random.nextInt(7));
for(int[] cube:currentTetris.cubes){
if(cube[1]<0)continue;
Rectangle temp=new Rectangle(position[cube[0]][cube[1]][0], position[cube[0]][cube[1]][1], 20, 20);
temp.setFill(currentTetris.cubeColor);
children.add(temp);
element[cube[0]][cube[1]]=temp;
}
}
private void setNextFrame(){
counter++;
if(counter>=duration){
Down();
}
if(currentKeyCode!=null)
switch(currentKeyCode){
case A:
case LEFT:
ternis(0);
currentKeyCode=null;
break;
case D:
case RIGHT:
ternis(1);
currentKeyCode=null;
break;
case S:
case DOWN:
ternis(3);
currentKeyCode=null;
break;
case W:
case UP:
ternis(4);
currentKeyCode=null;
break;
case SPACE:
currentKeyCode=null;
ternis(2);
break;
default:
currentKeyCode=null;
break;
}
}
private void ternis(int cmd) {
if(A[cmd].equals("←")){
left();
}
else if(A[cmd].equals("→")){
right();
}
else if(A[cmd].equals("⇊")){
space();
}
else if(A[cmd].equals("↓")){
down();
}
else if(A[cmd].equals("↺")){
rotate();
}
}
private boolean rotate() {
for(int[] cube:currentTetris.cubes){
if(cube[1]>=0){
children.remove(element[cube[0]][cube[1]]);
color[cube[0]][cube[1]]=bgc;
}
}
tetris checkTetris=new tetris(0);
for(int i=0;i<4;i++){
for(int j=0;j<2;j++){
checkTetris.cubes[i][j]=currentTetris.cubes[i][j];
}
}
checkTetris.cubeColor=currentTetris.cubeColor;
checkTetris.centerCube=currentTetris.centerCube;
for(int[] cube:checkTetris.cubes){
int x=checkTetris.cubes[checkTetris.centerCube][0]+checkTetris.cubes[checkTetris.centerCube][1]-cube[1];
int y=checkTetris.cubes[checkTetris.centerCube][1]+checkTetris.cubes[checkTetris.centerCube][0]-cube[0];
cube[0]=x;cube[1]=y;
}
boolean check=true;int x=0,y=0;
for(int[] cube:checkTetris.cubes){
if(cube[0]<0||cube[0]>9||cube[1]>19){
check=false;
break;
}
if(cube[1]<0)continue;
if(color[cube[0]][cube[1]]!=bgc){
check=false;
break;
}
}
if(!check)
for(int i=-1;i<=1;i++){
for(int j=-1;j<=1;j++){
check=true;x=i;y=j;
for(int[] cube:checkTetris.cubes){
if(cube[0]+i<0||cube[0]+i>9||cube[1]+j>19){
check=false;
break;
}
if(cube[1]+j<0)continue;
if(color[cube[0]+i][cube[1]+j]!=bgc){
check=false;
break;
}
}
if(check)break;
}
if(check)break;
}
if(!check){
for(int[] cube:currentTetris.cubes){
if(cube[1]<0)continue;
Rectangle temp=new Rectangle(position[cube[0]][cube[1]][0], position[cube[0]][cube[1]][1], 20, 20);
temp.setFill(currentTetris.cubeColor);
children.add(temp);
element[cube[0]][cube[1]]=temp;
color[cube[0]][cube[1]]=currentTetris.cubeColor;
}
return false;
}
for(int i=0;i<4;i++){
currentTetris.cubes[i][0]=checkTetris.cubes[i][0]+x;
currentTetris.cubes[i][1]=checkTetris.cubes[i][1]+y;
}
for(int[] cube:currentTetris.cubes){
if(cube[1]<0)continue;
Rectangle temp=new Rectangle(position[cube[0]][cube[1]][0], position[cube[0]][cube[1]][1], 20, 20);
temp.setFill(currentTetris.cubeColor);
children.add(temp);
element[cube[0]][cube[1]]=temp;
color[cube[0]][cube[1]]=currentTetris.cubeColor;
}
return true;
}
private void space() {
while(down());
}
private boolean right() {
List<int[]> checkList=new ArrayList<>();
for(int[] i:currentTetris.cubes){
boolean check=true;
for(int[] j:currentTetris.cubes){
if(i[1]==j[1]&&i[0]+1==j[0]){
check=false;
}
}
if(check)checkList.add(i);
}
for(int[] checkedCube:checkList){
if(checkedCube[1]<0 && checkedCube[0]!=9)continue;
if(checkedCube[0]==9||color[checkedCube[0]+1][checkedCube[1]]!=bgc)
return false;
}
for(int[] cube:currentTetris.cubes){
if(cube[1]>=0){
children.remove(element[cube[0]][cube[1]]);
color[cube[0]][cube[1]]=bgc;
}
cube[0]+=1;
}
for(int[] cube:currentTetris.cubes){
if(cube[1]<0)continue;
Rectangle temp=new Rectangle(position[cube[0]][cube[1]][0], position[cube[0]][cube[1]][1], 20, 20);
temp.setFill(currentTetris.cubeColor);
children.add(temp);
element[cube[0]][cube[1]]=temp;
color[cube[0]][cube[1]]=currentTetris.cubeColor;
}
return true;
}
private boolean left() {
List<int[]> checkList=new ArrayList<>();
for(int[] i:currentTetris.cubes){
boolean check=true;
for(int[] j:currentTetris.cubes){
if(i[1]==j[1]&&i[0]==j[0]+1){
check=false;
}
}
if(check)checkList.add(i);
}
for(int[] checkedCube:checkList){
if(checkedCube[1]<0 && checkedCube[0]!=0)continue;
if(checkedCube[0]==0||color[checkedCube[0]-1][checkedCube[1]]!=bgc)
return false;
}
for(int[] cube:currentTetris.cubes){
if(cube[1]>=0){
children.remove(element[cube[0]][cube[1]]);
color[cube[0]][cube[1]]=bgc;
}
cube[0]-=1;
}
for(int[] cube:currentTetris.cubes){
if(cube[1]<0)continue;
Rectangle temp=new Rectangle(position[cube[0]][cube[1]][0], position[cube[0]][cube[1]][1], 20, 20);
temp.setFill(currentTetris.cubeColor);
children.add(temp);
color[cube[0]][cube[1]]=currentTetris.cubeColor;
element[cube[0]][cube[1]]=temp;
}
return true;
}
private boolean down() {
List<int[]> checkList=new ArrayList<>();
for(int[] i:currentTetris.cubes){
boolean check=true;
for(int[] j:currentTetris.cubes){
if(i[0]==j[0]&&i[1]+1==j[1]){
check=false;
}
}
if(check)checkList.add(i);
}
for(int[] checkedCube:checkList){
if(checkedCube[1]<0)continue;
if(checkedCube[1]==19||color[checkedCube[0]][checkedCube[1]+1]!=bgc)
return false;
}
for(int[] cube:currentTetris.cubes){
if(cube[1]>=0){
children.remove(element[cube[0]][cube[1]]);
color[cube[0]][cube[1]]=bgc;
}
cube[1]+=1;
}
for(int[] cube:currentTetris.cubes){
if(cube[1]<0)continue;
Rectangle temp=new Rectangle(position[cube[0]][cube[1]][0], position[cube[0]][cube[1]][1], 20, 20);
temp.setFill(currentTetris.cubeColor);
children.add(temp);
element[cube[0]][cube[1]]=temp;
color[cube[0]][cube[1]]=currentTetris.cubeColor;
}
return true;
}
private void Down() {
counter=0;
if(!down()){
boolean check=false;
for(int[] cube:currentTetris.cubes){
if(cube[1]<0){
time.stop();
gameTimeline.stop();
check=true;
}
}
if(!check){
Random random=new Random();
currentTetris=new tetris(random.nextInt(7));
for(int[] c:currentTetris.cubes){
if(c[1]<0)continue;
Rectangle temp=new Rectangle(position[c[0]][c[1]][0], position[c[0]][c[1]][1], 20, 20);
temp.setFill(currentTetris.cubeColor);
children.add(temp);
element[c[0]][c[1]]=temp;
}
}
}
}
}