-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
232 lines (157 loc) · 5.79 KB
/
main.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 AlphaBeta import *
from BoardLogic import *
from heuristics import *
import time
alpha = float('-inf')
beta = float('inf')
depth = 3
ai_depth = 4
def boardOutput(board):
print(board[0]+"(00)----------------------"+board[1]+"(01)----------------------"+board[2]+"(02)");
print("| | |");
print("| "+board[8]+"(08)--------------"+board[9]+"(09)--------------"+board[10]+"(10) |");
print("| | | | |");
print("| | | | |");
print("| | "+board[16]+"(16)-----"+board[17]+"(17)-----"+board[18]+"(18) | |");
print("| | | | | |");
print("| | | | | |");
print(board[3]+"(03)---"+board[11]+"(11)----"+board[19]+"(19) "+board[20]+"(20)----"+board[12]+"(12)---"+board[4]+"(04)");
print("| | | | | |");
print("| | | | | |");
print("| | "+board[21]+"(21)-----"+board[22]+"(22)-----"+board[23]+"(23) | |");
print("| | | | |");
print("| | | | |");
print("| "+board[13]+"(13)--------------"+board[14]+"(14)--------------"+board[15]+"(15) |");
print("| | |");
print("| | |");
print(board[5]+"(05)----------------------"+board[6]+"(06)----------------------"+board[7]+"(07)");
def AI_VS_AI(heuristic1, heuristic2):
board = []
for i in range(24):
board.append("X")
evaluation = evaluator()
print("Stage 1")
for i in range(9):
boardOutput(board)
evalBoard = alphaBetaPruning(board, ai_depth, True, alpha, beta, True, heuristic1)
if evalBoard.evaluator == float('inf'):
print("AI Bot 1 has won!")
exit(0)
else:
board = evalBoard.board
boardOutput(board)
evalBoard = alphaBetaPruning(board, ai_depth, False, alpha, beta, True, heuristic2)
if evalBoard.evaluator == float('-inf'):
print("AI Bot 2 has won!")
exit(0)
else:
board = evalBoard.board
print("Stage 2")
while True:
boardOutput(board)
evalBoard = alphaBetaPruning(board, ai_depth, True, alpha, beta, False, heuristic1)
if evalBoard.evaluator == float('inf'):
print("AI Bot 1 has won!")
exit(0)
else:
board = evalBoard.board
boardOutput(board)
evaluation = alphaBetaPruning(board, ai_depth, False, alpha, beta, False, heuristic2)
if evaluation.evaluator == float('-inf'):
print("AI Bot 2 has won")
exit(0)
else:
board = evaluation.board
def HUMAN_VS_AI(heuristic_stage1, heuristic_stage23):
board = []
for i in range(24):
board.append("X")
evaluation = evaluator()
for i in range(9):
boardOutput(board)
finished = False
while not finished:
try:
pos = int(input("\nPlace '1' piece: "))
if board[pos] == "X":
board[pos] = '1'
if isCloseMill(pos, board):
itemPlaced = False
while not itemPlaced:
try:
pos = int(input("\nRemove '2' piece: "))
if board[pos] == "2" and not isCloseMill(pos, board) or (isCloseMill(pos, board) and getNumberOfPieces(board, "1") == 3):
board[pos] = "X"
itemPlaced = True
else:
print("Invalid position")
except Exception:
print("Input was either out of bounds or wasn't an integer")
finished = True
else:
print("There is already a piece there")
except Exception:
print("Couldn't get the input value")
boardOutput(board)
evalBoard = alphaBetaPruning(board, depth, False, alpha, beta, True, heuristic_stage1)
if evalBoard.evaluator == float('-inf'):
print("You Lost")
exit(0)
else:
board = evalBoard.board
endStagesFinished = False
while not endStagesFinished:
boardOutput(board)
#Get the users next move
userHasMoved = False
while not userHasMoved:
try:
pos = int(input("\nMove '1' piece: "))
while board[pos] != '1':
pos = int(input("\nMove '1' piece: "))
userHasPlaced = False
while not userHasPlaced:
newPos = int(input("'1' New Location: "))
if board[newPos] == "X":
board[pos] = 'X'
board[newPos] = '1'
if isCloseMill(newPos, board):
userHasRemoved = False
while not userHasRemoved:
try:
pos = int(input("\nRemove '2' piece: "))
if board[pos] == "2" and not isCloseMill(pos, board) or (isCloseMill(pos, board) and getNumberOfPieces(board, "1") == 3):
board[pos] = "X"
userHasRemoved = True
else:
print("Invalid position")
except Exception:
print("Error while accepting input")
userHasPlaced = True
userHasMoved = True
else:
print("You cannot move there")
except Exception:
print("You cannot move there")
if getEvaluationStage23(board) == float('inf'):
print("You Win!")
exit(0)
boardOutput(board)
evaluation = alphaBetaPruning(board, depth, False, alpha, beta, False, heuristic_stage23)
if evaluation.evaluator == float('-inf'):
print("You Lost")
exit(0)
else:
board = evaluation.board
if __name__ == "__main__":
print("Welcome to Nine Mens Morris")
print("==========================")
print("1. Is Human vs AI")
print("2. Is AI vs AI")
gametype = input("Please enter 1 or 2: ")
while gametype != "1" and gametype != "2":
gametype = input("Please enter 1 or 2")
if gametype == "1":
HUMAN_VS_AI(numberOfPiecesHeuristic, AdvancedHeuristic)
elif gametype == "2":
AI_VS_AI(numberOfMoveablePiecesHeuristic, AdvancedHeuristic)