-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_4pt.py
232 lines (212 loc) · 8.02 KB
/
game_4pt.py
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
from search_4pt import *
import sys
import math
import time
import random
import pygame
class game:
def __init__(self):
pygame.init()
self.size = int(560/8)
self.board = 0
self.clock = pygame.time.Clock()
self.status = 0
self.turn = 1
self.x = 0
self.y = 0
self.newX = 0
self.newY = 0
self.totalNode1 = 0
self.totalNode2 = 0
self.totalTime1 = 0
self.totalTime2 = 0
self.totalStep1 = 0
self.totalStep2 = 0
self.eaten = 0
self.matrix = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]]
#run the game
def run(self):
self.clock.tick(60)
self.status = 5
if self.status == 5:
# Black
if self.turn == 1:
start = time.clock()
self.move(2, 4)
self.totalTime1 += (time.clock() - start)
self.totalStep1 += 1
for i in self.matrix:
print(i)
print('\n')
elif self.turn == 2:
start = time.clock()
self.move(2, 2)
self.totalTime2 += (time.clock() - start)
self.totalStep2 += 1
for i in self.matrix:
print(i)
print('\n')
for event in pygame.event.get():
# Quit if close the windows
if event.type == pygame.QUIT:
exit()
elif self.status == 0:
x, y = event.pos
coor_y = math.floor(x / self.size)
coor_x = math.floor(y / self.size)
if self.matrix[coor_x][coor_y] == self.turn:
self.status = 1
self.y = math.floor(x / self.size)
self.x = math.floor(y / self.size)
# check whether the selected chess can move, otherwise select other chess
elif self.status == 1:
x, y = event.pos
self.newY = math.floor(x / self.size)
self.newX = math.floor(y / self.size)
if self.isabletomove():
self.movechess()
if (self.newX == 4 and self.matrix[self.newX][self.newY] == 1) or (self.newX == 0 and self.matrix[self.newX][self.newY] == 2):
self.status = 3
elif self.matrix[self.newX][self.newY] == self.matrix[self.x][self.y]:
self.x = self.newX
self.y = self.newY
self.display()
#get the result in terminal
def display(self):
if self.status == 3:
if self.turn == 1:
print("White Win!")
print( 'Black: \n'
'total steps =', self.totalStep1, '\n'
'total expaned nodes =', self.totalNode1, '\n'
'average expaned nodes per move =', self.totalNode1 / self.totalStep1, '\n'
'average time per move =', self.totalTime1 / self.totalStep1, '\n'
'number of captured workers =', self.eaten)
print( 'White: \n'
'total steps =', self.totalStep2, '\n'
'total expaned nodes =', self.totalNode2, '\n'
'average expaned nodes per move =', self.totalNode2 / self.totalStep2, '\n'
'average time per move =', self.totalTime2 / self.totalStep2, '\n'
'number of captured workers =', self.eaten)
for i in self.matrix:
print(i)
else:
print("Black Win!")
print( 'White: \n'
'total steps =', self.totalStep2, '\n'
'total expaned nodes =', self.totalNode2, '\n'
'average expaned nodes per move =', self.totalNode2 / self.totalStep2, '\n'
'average time per move =', self.totalTime2 / self.totalStep2, '\n'
'number of captured workers =', self.eaten)
print( 'Black: \n'
'total steps =', self.totalStep1, '\n'
'total expaned nodes =', self.totalNode1, '\n'
'average expaned nodes per move =', self.totalNode1 / self.totalStep1, '\n'
'average time per move =', self.totalTime1 / self.totalStep1, '\n'
'number of captured workers =', self.eaten)
for i in self.matrix:
print(i)
sys.exit()
if self.status == 1:
# only downward is acceptable
if self.matrix[self.x][self.y] == 1:
x1 = self.x + 1
y1 = self.y - 1
x2 = self.x + 1
y2 = self.y + 1
x3 = self.x + 1
y3 = self.y
# left down
if self.matrix[self.x][self.y] == 2:
x1 = self.x - 1
y1 = self.y - 1
x2 = self.x - 1
y2 = self.y + 1
x3 = self.x - 1
y3 = self.y
#change the action in the screen
def movechess(self):
self.matrix[self.newX][self.newY] = self.matrix[self.x][self.y]
self.matrix[self.x][self.y] = 0
if self.turn == 1:
self.turn = 2
elif self.turn == 2:
self.turn = 1
self.status = 0
#check the state whether is availble
def isabletomove(self):
if checkMove() == True:
return 1
return 0
def checkMove(self):
if (self.matrix[self.x][self.y] == 1
and self.matrix[self.newX][self.newY] != 1
and self.newX - self.x == 1
and self.y - 1 <= self.newY <= self.y + 1
and not (self.y == self.newY and self.matrix[self.newX][self.newY] == 2)) \
or (self.matrix[self.x][self.y] == 2
and self.matrix[self.newX][self.newY] != 2
and self.x - self.newX == 1
and self.y - 1 <= self.newY <= self.y + 1
and not (self.y == self.newY and self.matrix[self.newX][self.newY] == 1)):
return True
return False
def move(self, searchtype, evaluation):
if searchtype == 1:
return self.move_minimax(evaluation)
elif searchtype == 2:
return self.move_alphabeta(evaluation)
#call the minimax/alphabeta search
def move_minimax(self, function_type):
board, nodes, piece = search(self.matrix, self.turn, 3, function_type).minimax()
self.moveNode(board,nodes,piece)
def move_alphabeta(self, function_type):
board, nodes, piece = search(self.matrix, self.turn, 5, function_type).alphabet()
self.moveNode(board,nodes,piece)
#move the nodes
def moveNode(self,board,nodes,piece):
self.matrix = board.getmatrix()
if self.turn == 1:
self.totalNode1 += nodes
self.turn = 2
elif self.turn == 2:
self.totalNode2 += nodes
self.turn = 1
self.eaten = 20-piece
if self.done():
self.status = 3
#check whether it is done
def done(self):
count = 0
for i in self.matrix[0]:
if i == 2:
count += 1
if count == 3:
return True
count = 0
for i in self.matrix[4]:
if i == 1:
count += 1
if count == 3:
return True
count1 = 0
count2 = 0
for line in self.matrix:
for i in line:
if i == 1:
count1 += 1
elif i == 2:
count2 += 1
if count1 <= 2 or count2 <= 2:
return True
return False
def main():
Game = game()
while True:
Game.run()
if __name__ == '__main__':
main()