-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakeAI.py
More file actions
139 lines (123 loc) · 3.46 KB
/
Copy pathsnakeAI.py
File metadata and controls
139 lines (123 loc) · 3.46 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
# coding: utf-8
import curses
from random import randint
#map and score
HEIGHT=10
WIDTH =30
score=0
pointsum = HEIGHT*WIDTH
board=[0]*pointsum
BLANKMARK= HEIGHT*WIDTH+5
#foodinf
FOODMARK=0
foodindex=5*WIDTH+5
#snakeinf
HEAD=0
size=1
SNAKEMARK=pointsum*2 +1
snake = [0] * (pointsum +1)
snake[HEAD] = 1*WIDTH+1
#direction
LEFT=-1
RIGHT=1
UP=-WIDTH
DOWN=WIDTH
direction = [LEFT,RIGHT,UP,DOWN]
key = 0
nextstep=LEFT
ERROR=-5
def board_reset(snake,foodindex,board):
global size,pointsum
for i in xrange(pointsum):
if i in snake[:size]:
board[i] = SNAKEMARK
elif i==foodindex:
board[i] = FOODMARK
else:
board[i] = BLANKMARK
def canmove(initidx,dir):
flag=False
if dir==LEFT:
flag = True if initidx%WIDTH >1 else False
elif dir==RIGHT:
flag = True if initidx%WIDTH <WIDTH - 2 else False
elif dir==UP:
flag = True if initidx/WIDTH >1 else False
elif dir==DOWN:
flag = True if initidx/WIDTH <HEIGHT - 2 else False
return flag
def Breadth_First_Search(snake,foodindex,board):
global direction,pointsum
fakeboard = [0] *pointsum
searchpoion=[]
searchpoion.append(foodindex)
flag = False
while len(searchpoion) != 0:
idx = searchpoion.pop()
fakeboard[idx] = 1
for i in xrange(4):
if canmove(idx,direction[i]):
if idx + direction[i] == snake[HEAD]:
flag = True
if board[idx+direction[i]] != SNAKEMARK:
if board[idx+direction[i]]>board[idx]+1:
board[idx+direction[i]] = board[idx]+1
if fakeboard[idx+direction[i]] == 0:
searchpoion.append(idx+direction[i])
return flag
def find_short_way(snake,board):
global direction
short = SNAKEMARK
finaldirection = ERROR
for i in xrange(4):
if canmove(snake[HEAD],direction[i]) and board[snake[HEAD]+direction[i]]<short:
short = board[snake[HEAD]+direction[i]]
finaldirection = direction[i]
return finaldirection
def new_food():
global WIDTH,HEIGHT,foodindex,snake,board
flag = True
while flag:
w = randint(1, WIDTH-2)
h = randint(1, HEIGHT-2)
foodindex = h * WIDTH + w
if not (foodindex in snake[:size]):
flag = False
win.addch(foodindex/WIDTH, foodindex%WIDTH, '.')
def moveto_nextstep(nextstep):
global snake,board,size,pointsum,score
for i in xrange(pointsum,0,-1):
snake[i] = snake[i-1]
snake[HEAD] += nextstep
win.addch(snake[HEAD]/WIDTH, snake[HEAD]%WIDTH, '0')
if board[snake[HEAD+1]+nextstep] == FOODMARK:
board[foodindex] = SNAKEMARK
size += 1
score +=1
if size < pointsum :
new_food()
else:
board[snake[HEAD]] = SNAKEMARK
board[snake[size]] = BLANKMARK
win.addch(snake[size]/WIDTH, snake[size]%WIDTH, ' ')
snake[size] = 0
curses.initscr()
win = curses.newwin(HEIGHT, WIDTH, 0, 0)
win.keypad(1)
curses.noecho()
curses.curs_set(0)
win.border(0)
win.nodelay(1)
win.addch(foodindex/WIDTH, foodindex%WIDTH, '.')
while key != 27:
win.border(0)
win.addstr(0, 2, 'S:' + str(score) + ' ')
win.timeout(10)
win.getch()
board_reset(snake,foodindex,board)
if Breadth_First_Search(snake,foodindex,board):
nextstep=find_short_way(snake,board)
else:
print ERROR
moveto_nextstep(nextstep)
curses.endwin()