-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.py
More file actions
173 lines (138 loc) · 4.99 KB
/
AI.py
File metadata and controls
173 lines (138 loc) · 4.99 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
from copy import deepcopy
class Checkers:
def __init__(self, player, coord, isKing):
self.player = player
self.isKing = isKing
self.coord = coord
def _isKing(self):
return self.isKing
def makeKing(self):
self.isKing = True
def updateCoords(self, x, y):
if self.player == 1 and y == 7:
self.makeKing()
if self.player == 2 and y == 0:
self.makeKing()
self.coord = [x, y]
class State:
def __init__(self, player1, player2):
self.player1 = player1
self.player2 = player2
def game_over(self):
if len(self.player1) == 0 or len(self.player2) == 0:
return True
return False
def evaluateState(self, turn):
ans = 0
if turn == 1:
ans = 10 * (len(self.player1) - len(self.player2))
for x in self.player1:
if x._isKing():
ans += 5
for x in self.player2:
if x._isKing():
ans -= 5
else:
ans = 10 * (len(self.player2) - len(self.player1))
for x in self.player2:
if x._isKing():
ans += 5
for x in self.player1:
if x._isKing():
ans -= 5
return ans
def move(self, oldCoord, newCoord):
check = self.getCheckerObject(oldCoord[0], oldCoord[1])
check.updateCoords(newCoord[0], newCoord[1])
def getCheckerObject(self, x, y):
for i in self.player1:
if i.coord == [x, y]:
return i
for i in self.player2:
if i.coord == [x, y]:
return i
def occupiedStatus(self, x, y):
if x >= 8 or x < 0 or y >= 8 or y < 0:
return -1
obj_ = self.getCheckerObject(x, y)
if not obj_:
return 0
else:
return obj_.player
def delete(self, x, y):
for i in range(len(self.player1)):
if self.player1[i].coord == [x, y]:
del self.player1[i]
break
for i in range(len(self.player2)):
if self.player2[i].coord == [x, y]:
del self.player2[i]
break
def solve(player1, player2, turn, depth):
player1_, player2_ = [], []
for x in player1:
player1_.append(Checkers(x.player, x.coord, x._isKing()))
for x in player2:
player2_.append(Checkers(x.player, x.coord, x._isKing()))
currState = State(player1_, player2_)
return MiniMax(currState, turn, depth, True, turn)
def MiniMax(state, turn, depth, max_player, starter):
if depth == 0 or state.game_over():
return [state.evaluateState(starter), None]
if max_player:
best = -10000000
bestMove = None
for [newState, moves] in make_move(state, turn) + make_jump_move(state, turn):
stateVal = MiniMax(newState, flipTurn(turn), depth - 1, False, starter)[0]
if stateVal >= best:
best = stateVal
bestMove = moves
return [best, bestMove]
else:
worst = 10000000
bestMove = None
for [newState, moves] in make_move(state, turn):
stateVal = MiniMax(newState, flipTurn(turn), depth - 1, True, starter)[0]
if stateVal <= worst:
worst = stateVal
bestMove = moves
return [worst, bestMove]
def flipTurn(turn):
return 1 if turn == 2 else 2
def make_move(state, turn):
ans = []
checkers = state.player1 if turn == 1 else state.player2
for piece in checkers:
moves = []
if piece.player == 1 or piece._isKing():
moves.append([-1, 1])
moves.append([1, 1])
if piece.player == 2 or piece._isKing():
moves.append([1, -1])
moves.append([-1, -1])
[x, y] = piece.coord
for i in moves:
if state.occupiedStatus(i[0] + x, i[1] + y) == 0:
state1 = deepcopy(state)
state1.move([x, y], [i[0] + x, i[1] + y])
ans.append([state1, [[x, y], [i[0] + x, i[1] + y]]])
return ans
def make_jump_move(state, turn):
ans = []
checkers = state.player1 if turn == 1 else state.player2
for piece in checkers:
moves = []
if piece.player == 1 or piece._isKing():
moves.append([-1, 1])
moves.append([1, 1])
if piece.player == 2 or piece._isKing():
moves.append([1, -1])
moves.append([-1, -1])
[x, y] = piece.coord
for i in moves:
if state.occupiedStatus(2 * i[0] + x, 2 * i[1] + y) == 0 and state.occupiedStatus(i[0] + x, i[1] + y) != piece.player and state.occupiedStatus(i[0] + x, i[1] + y) > 0:
state1 = deepcopy(state)
state1.move([x, y], [2*i[0] + x, 2*i[1] + y])
state1.delete(x+i[0], y+i[1])
ans.append([state1, [[x, y], [2*i[0] + x, 2*i[1] + y]]])
return ans