-
Notifications
You must be signed in to change notification settings - Fork 3
/
AI_vs_human.py
119 lines (96 loc) · 4.02 KB
/
AI_vs_human.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
from utils import *
def AI_vs_human(heuristic):
board = []
for i in range(24):
board.append("x")
evaluation = evaluate()
print("STAGE 1")
for i in range(9):
printBoard(board)
finished = False
# TAKING USER INPUT FOR STAGE 1
while not finished:
try:
pos = int(input("\nPlace a piece: "))
if board[pos] == 'x':
board[pos] = '1'
if isMill(pos, board):
itemPlaced = False
while not itemPlaced:
try:
pos = int(input("\nRemove a '2' piece: "))
if board[pos] == '2' and not isMill(pos, board) or (isMill(pos, board) and numOfPieces(board, '1') == 3):
board[pos] = 'x'
itemPlaced = True
else:
print("Invalid position. Try again.")
except Exception as e:
print(str(e))
print("Invalid input, Try again.")
finished = True
else:
print("There is a piece there already. Try again.")
except Exception as e:
print(str(e))
print("Try again. Invalid input.")
printBoard(board)
evalBoard = minimax(board, depth, False, alpha, beta, True, heuristic)
if evalBoard.evaluate == float('-inf'):
print("YOU LOST!")
sys.exit()
else:
board = evalBoard.board
print("STAGE 2")
while True:
printBoard(board)
# TAKING USER INPUT FOR STAGE 2
userMoved = False
while not userMoved:
try:
pos = int(input("\nMove a '1' piece: "))
while board[pos] != '1':
print("Invalid. Try again.")
pos = int(input("\nMove a '1' piece: "))
userPlaced = False
while not userPlaced:
newpos = int(input("'1' New Location: "))
if board[newpos] == 'x':
board[pos] = 'x'
board[newpos] = '1'
if isMill(newpos, board):
userRemoved = False
while not userRemoved:
try:
pos = int(
input("\nMill formed. Remove a '2' piece: "))
if board[pos] == "2" and not isMill(pos, board) or (isMill(pos, board) and numOfPieces(board, "1") == 3):
board[pos] = "x"
userRemoved = True
else:
print("Invalid position")
except Exception:
print("Error while accepting input")
userPlaced = True
userMoved = True
else:
print("Invalid Position. Try Again.")
except Exception as e:
print(str(e))
print("Invalid entry. Try Again please.")
if evaluateStage23(board) == float('inf'):
print("YOU WIN!")
exit(0)
printBoard(board)
evaluation = minimax(board, depth, False, alpha,
beta, False, heuristic)
if evaluation.evaluate == float('-inf'):
print("YOU LOST!")
exit(0)
else:
board = evaluation.board
if __name__ == "__main__":
print("WELCOME TO AI VS HUMAN 9 MENS MORRIS")
print()
AI_vs_human(potentialMillsHeuristic)
# You can also use the number of pieces heuristic (worse results)
# AI_vs_human(numPiecesHeuristic)