-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
175 lines (113 loc) · 4.45 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
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 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("Human vs AI")
HUMAN_VS_AI(numberOfPiecesHeuristic, AdvancedHeuristic)