/
snake.cpp
executable file
·350 lines (320 loc) · 7.26 KB
/
snake.cpp
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
#include "snake.h"
#include <curses.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
Snake::Snake(int x, int y)
{
head = new Node;
head->x = x;
head->y = y;
head->prevNode = NULL;
tail = head;
}
Snake::Snake(int x, int y, Direction direction, int targetX, int targetY) {
// Create snake's head at the specified position
head = new Node;
head->x = x;
head->y = y;
head->prevNode = NULL;
tail = head;
dir = direction;
}/*
Snake::Snake(int x, int y, Direction direction) {
// Create snake's head at the specified position
head = new Node;
head->x = x;
head->y = y;
head->prevNode = NULL;
tail = head;
dir = direction;
}*/
Snake::~Snake() {
while(tail != NULL) {
Node* temp = tail;
tail = tail->prevNode;
delete temp;
}
}
void Node::operator=(Node* n)
{
//Node* tmp=new Node;
this->x=n->x;
this->y=n->y;
this->survTime=n->survTime;
//return tmp;
}
void Snake::operator=(Snake* s)
{
dir=s->dir;
Node* index = s->tail;
Node* t=new Node;
t->operator=(index);
t->prevNode=NULL;
this->InsertAtHead(t);
this->tail=t;
this->head=t;
while(index->prevNode!=NULL)
{
Node* n = new Node;
n->operator=(index);
n->prevNode=NULL;
this->InsertAtHead(n);
index=index->prevNode;
}
Node* n= new Node;
n->operator=(index);
this->InsertAtHead(n);
}
void Snake::Draw() {
Node* temp = tail;
while(temp != NULL) {
mvaddch(temp->y, temp->x, '*');
temp = temp->prevNode;
}
//mvaddch(temp->y, temp->x, '@');
}
void Snake::RemoveAtTail() {
if(this->tail->prevNode!=NULL)
{
Node* temp = this->tail;
this->tail = this->tail->prevNode;
//delete temp;
}
}
void Snake::InsertAtHead(Node* newHead) {
head->prevNode = newHead;
head = newHead;
}
void Snake::SetDir(Direction new_dir) {
if (!(new_dir == UP && dir == DOWN) && !(new_dir == DOWN && dir == UP) &&
!(new_dir == LEFT && dir == RIGHT) && !(new_dir == RIGHT && dir == LEFT)) {
dir = new_dir;
}
}
bool Snake::SetTarget(int x, int y){
targetX=x;
targetY=y;
Node* n = new Node;
n=this->tail;
while(n->prevNode!=NULL)
{
if(n->x==x && n->y==y)
return false;
n=n->prevNode;
}
if(n->x==x && n->y==y)
return false;
else return true;
}
void Snake::Move() {
Node* new_head = new Node;
*new_head = *head; // copy the current head
// Compute position of the new head
switch(dir) {
case UP:
new_head->y--;
break;
case DOWN:
new_head->y++;
break;
case LEFT:
new_head->x--;
break;
case RIGHT:
new_head->x++;
break;
}
// Draw the new head
InsertAtHead(new_head);
mvaddch(new_head->y, new_head->x, '*');
// Clear the tail if we haven't reached the target
if (targetX != new_head->x || targetY != new_head->y ) {
mvaddch(tail->y, tail->x, ' ');
RemoveAtTail();
}
}
void Snake::ghostMove(queue<int>* sim) {
int dir;
queue<int>* s=sim;
for(int i=0;i<s->size();i++)
{
dir=s->front();
Node* new_head = new Node;
new_head->x=head->x;
new_head->y=head->y;
//new_head->operator=(this->head);
switch(dir) {
case 259://UP
this->SetDir(UP);
new_head->y--;
break;
case 258://DOWN
this->SetDir(DOWN);
new_head->y++;
break;
case 260://LEFT
this->SetDir(LEFT);
new_head->x--;
break;
case 261://RIGHT
this->SetDir(RIGHT);
new_head->x++;
break;
}
this->InsertAtHead(new_head);
this->RemoveAtTail();
s->pop();
}
}
bool Snake::ghostMove(int dir ,int maxX,int maxY) {
//Snake* tmp = new Snake;
//tmp->operator=(this);
//避免回頭問題
if(this->dir==UP && dir==258){
return false;
}
else if(this->dir==DOWN && dir==259){
return false;
}
else if(this->dir==RIGHT && dir==260){
return false;
}
else if(this->dir==LEFT && dir==261){
return false;
}
else{
Node* new_head = new Node;
new_head->x=head->x;
new_head->y=head->y;
new_head->prevNode=NULL;
//new_head->operator=(this->head);
switch(dir) {
case 259://UP
this->SetDir(UP);
new_head->y--;
break;
case 258://DOWN
this->SetDir(DOWN);
new_head->y++;
break;
case 260://LEFT
this->SetDir(LEFT);
new_head->x--;
break;
case 261://RIGHT
this->SetDir(RIGHT);
new_head->x++;
break;
}
this->InsertAtHead(new_head);
this->RemoveAtTail();
if(this->CurX()<0||this->CurY()<0||this->CurY()>=maxY||this->CurX()>=maxX|| this->checkSelf()==false)
{
//this->operator=(tmp);
return false;
}
else
return true;
/*
if(new_head->x<0||new_head->y<0||new_head->y>=maxY||new_head->x>=maxX|| this->checkSelf()==false)
{
//this->operator=(tmp);
return false;
}
else
return true;*/
}
}
void Snake::calSurvTime(){
int a=1;
Node* temp = this->tail;
while(temp->prevNode!=NULL)
{
temp->survTime=a;
a++;
}
}
int Snake::getTarX(){
return this->targetX;
}
int Snake::getTarY(){
return this->targetY;
}
int Snake::getLength(){
int x=1;
Node* n =this->tail;
while(n->prevNode!=NULL)
{
x=x+1;
n=n->prevNode;
}
return x;
}
void Snake::Save(const char* file_name) {
ofstream file(file_name);
if (!file) return;
// Save moving direction
file << dir << endl;
// Save snake body locations
Node* temp = tail;
while(temp != NULL) {
file << temp->x << ' ' << temp->y << endl;
temp = temp->prevNode;
}
file.close();
}
Snake* Snake::Load(const char* file_name) {
Snake* snake = NULL;
ifstream file(file_name);
if (!file) return NULL;
int dir, x, y;
// Create a new snake
snake = new Snake;
snake->head = snake->tail = NULL;
// Load moving direction
file >> dir;
snake->dir = (Direction) dir;
// Load the body
while(file >> x >> y) {
Node* node = new Node;
node->x = x;
node->y = y;
node->prevNode = NULL;
mvaddch(node->y, node->x, '*');
if (snake->head == NULL) {
snake->head = node;
snake->tail = node;
} else {
snake->InsertAtHead(node);
}
}
return snake;
}
/*bool Snake::isInBody(Node* n)
{
Node* index=this->tail;
while(index->prevNode!=NULL)
{
}
}*/
bool Snake::checkSelf()
{
if(this->head!=this->tail){
this->head->prevNode=NULL;
int x =this->head->x;
int y =this->head->y;
Node*n =this->tail;
while(n->prevNode!=NULL)
{
if(x==n->x && y==n->y)
return false;
n=n->prevNode;
}
}
else if (this->head->x==this->tail->x && this->head->y==this->tail->y&& this->getLength()>3)
return false;
return true;
}