-
Notifications
You must be signed in to change notification settings - Fork 0
/
snuk.pde
186 lines (170 loc) · 3.45 KB
/
snuk.pde
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
class snuk{
int x=int(w/2);
int y=int(h/2);
int fx,fy;
int dir=0;
int pdir=0;
int l=3;
ArrayList<PVector> trail = new ArrayList<PVector>();
int add=0;
nNet brain=new nNet(brainDim[0],brainDim[1],brainDim[2]);
boolean dead=false;
int fitness=1;
float pDist;
snuk(){
for(int i=0; i<l+2; i++){
trail.add(new PVector(x,y));
}
pickFood();
pDist=dist(x,y,fx,fy);
}
void core(){
if(dead==false){
decide();
move();
checkFood();
checkDead();
trail();
}
}
void pickFood(){
boolean placed=false;
while(placed==false){
fx=int(random(0,w-1));
fy=int(random(0,h-1));
placed=true;
for(int i=trail.size()-l; i<trail.size(); i++){
if(fx==trail.get(i).x && fy==trail.get(i).y){
placed=false;
}
}
}
pDist=dist(x,y,fx,fy);
}
void move(){
if(dir==0){
y++;
}else if(dir==1){
x++;
}else if(dir==2){
y--;
}else if(dir==3){
x--;
}
pdir=dir;
if(dist(x,y,fx,fy)>pDist){
if(fitness>1){
fitness--;
}
}else{
fitness++;
}
pDist=dist(x,y,fx,fy);
}
void checkFood(){
if(x==fx && y==fy){
fitness+=prize;
add+=growth;
pickFood();
}
}
void checkDead(){
if(x==-1||x==w||y==-1||y==h){
dead=true;
}
for(int i=trail.size()-l; i<trail.size(); i++){
if(x==trail.get(i).x && y==trail.get(i).y){
dead=true;
}
}
}
void trail(){
if(add>0){
l++;
add--;
}
trail.add(new PVector(x,y));
if(trail.size()>l+5){
trail.remove(0);
}
}
void decide(){
float[] in=new float[brainDim[0]];
float dx=fx-x;
float dy=fy-y;
boolean[] dir_=new boolean[4];
boolean[] danger=new boolean[4];
if(dy>0){
dir_[0]=true;
}else if(dy<0){
dir_[2]=true;
}
if(dx>0){
dir_[1]=true;
}else if(dx<0){
dir_[3]=true;
}
if(y+1>=h){
danger[0]=true;
}
if(x+1>=w){
danger[1]=true;
}
if(y-1<0){
danger[2]=true;
}
if(x-1<0){
danger[3]=true;
}
for(int i=trail.size()-1; i>=trail.size()-l; i--){
if(trail.get(i).x==x){
if(trail.get(i).y==y+1){
danger[0]=true;
}
if(trail.get(i).y==y-1){
danger[2]=true;
}
}
if(trail.get(i).y==y){
if(trail.get(i).x==x+1){
danger[1]=true;
}
if(trail.get(i).x==x-1){
danger[3]=true;
}
}
}
for(int i=0; i<3; i++){
if(dir_[((((dir-1+i)%4)+4)%4)]==true){
in[i]=1.0;
}else{
in[i]=0.0;
}
if(danger[((((dir-1+i)%4)+4)%4)]==true){
in[3+i]=1.0;
}else{
in[3+i]=0.0;
}
}
float[] ans=brain.feed(in);
float max=max(ans);
for(int i=0; i<ans.length; i++){
if(ans[i]==max){
input(i);
}
}
}
void input(int dir_){
dir=(((dir+dir_-1)%4)+4)%4;
}
void display(){
//float t=0;
for(int i=trail.size()-1; i>=trail.size()-l; i--){
//t+=0.5;
//fill(0,0,255-noise(t)*155);
fill(255,alpha);
noStroke();
rect(trail.get(i).x*size,trail.get(i).y*size,size,size);
}
}
}