-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.java
More file actions
188 lines (167 loc) · 4.47 KB
/
Copy pathCore.java
File metadata and controls
188 lines (167 loc) · 4.47 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
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
import java.util.Random;
public class Core implements Observer
{
private Field [][] array;
private TwoWayLinkedList<Field> snake;
private int direction;
private Frame gui;
public static final int UP = 0;
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int RIGHT = 3;
private int lastDir;
private int score;
private Clock clk;
public Core()
{
array = new Field[25][];
for (int i=0; i<array.length; i++)
{
array[i] = new Field[35];
for (int j=0; j<array[i].length; j++)
{
array[i][j] = new Field(i,j);
}
}
direction = RIGHT;
score = 0;
snake = new TwoWayLinkedList<>();
snake.addTail(array[12][18]);
snake.addTail(array[12][17]);
snake.addTail(array[12][16]);
snake.getHead().setProperty(Field.HEAD);
snake.getTail().setProperty(Field.TAIL);
addFood();
clk = new Clock(100);
clk.register(this);
clk.start();
}
public void move()
{
Field temp;
if (direction == 0)
{
temp = array[(snake.getHead().getxCord()+array.length-1)%array.length][snake.getHead().getyCord()];
}
else if (direction == 1)
{
temp = array[(snake.getHead().getxCord()+1)%array.length][snake.getHead().getyCord()];
}
else if (direction == 2)
{
temp = array[snake.getHead().getxCord()][(snake.getHead().getyCord()+array[0].length-1)%array[0].length];
}
else
{
temp = array[snake.getHead().getxCord()][(snake.getHead().getyCord()+1)%array[0].length];
}
if ((temp.getProperty() == Field.BODY) || (temp.getProperty() == Field.FEDBODY))
{
clk.deregister(this);
try
{
Thread.sleep(3000);
Main.refresh();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
snake.addHead(temp);
//snake.getTail().setProperty(Field.EMPTY);
modifySnake(snake.cutTail());
}
}
private void modifySnake(Field tail)
{
for (Field f : snake)
{
if (f.getProperty() == Field.EATING)
{
f.setProperty(Field.FEDBODY);
}
else if (f.getProperty() == Field.FOOD)
{
f.setProperty(Field.EATING);
addFood();
score ++;
}
else if (f.getProperty() == Field.FEDBODY)
{
}
else
{
f.setProperty(Field.BODY);
}
}
if (snake.getTail().getProperty() == Field.FEDBODY)
{
snake.getTail().setProperty(Field.BODY);
snake.addTail(tail);
snake.getTail().setProperty(Field.TAIL);
}
else
{
tail.setProperty(Field.EMPTY);
}
if (snake.getHead().getProperty() != Field.EATING)
{
snake.getHead().setProperty(Field.HEAD);
}
snake.getTail().setProperty(Field.TAIL);
System.out.println(score);
}
//finished
private void addFood()
{
Random rand = new Random();
Field temp = array[rand.nextInt(25)][rand.nextInt(35)];
if (temp.getProperty() == Field.EMPTY)
{
temp.setProperty(Field.FOOD);
}
else
{
addFood();
}
}
//finished
public void addFrame(Frame frame)
{
gui = frame;
}
//finished
public void changeDirection(char c)
{
int temp;
switch ((int)c)
{
case (int)'w':
temp = 0;
if (lastDir != 1) direction = temp;
break;
case (int)'s':
temp = 1;
if (lastDir != 0) direction = temp;
break;
case (int)'a':
temp = 2;
if (lastDir != 3) direction = temp;
break;
case (int)'d':
temp = 3;
if (lastDir != 2) direction = temp;
break;
}
}
@Override
public void update()
{
move();
gui.display(array);
lastDir = direction;
}
}