-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
168 lines (136 loc) · 4.11 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
import random
import copy
#Player = O
board = [[" "," "," "],[" "," "," "],[" "," "," "]]
game = True
def printBoard(b):
print("\n\n")
print(f"{b[0][0]}|{b[0][1]}|{b[0][2]}")
print("- - -")
print(f"{b[1][0]}|{b[1][1]}|{b[1][2]}")
print("- - -")
print(f"{b[2][0]}|{b[2][1]}|{b[2][2]}")
def makeMove(b, xy, player):
if player == True:
s = "o"
else:
s = "x"
b[xy[0]][xy[1]] = s
printBoard(b)
def getInput():
xy = tuple(int(x.strip()) - 1 for x in input("Please enter x,y coordinates for your move (ex, '2,3'): ").split(','))
return xy
def getScore(b):
for x in range(0, 3):
if b[x][0] == b[x][1] and b[x][1] == b[x][2]:
if b[x][0] == "x":
return 10
if b[x][0] == "o":
return -10
for y in range (0, 3):
if b[0][y] == b[1][y] and b[1][y] == b[2][y]:
if b[0][y] == "x":
return 10
if b[0][y] == "o":
return -10
if b[0][0] == b[1][1] and b[1][1] == b[2][2]:
if b[0][0] == "x":
return 10
if b[0][0] == "o":
return -10
if b[0][2] == b[1][1] and b[1][1] == b[2][0]:
if b[1][1] == "x":
return 10
if b[1][1] == "o":
return -10
return 0
"""
def getMoves(b, isX):
possibleMoves = []
for x in range(0,3):
for y in range(0,3):
if b[x][y] == " ":
tempBoard = copy.deepcopy(b)
if isX:
tempBoard[x][y] = "x"
else:
tempBoard[x][y] = "o"
possibleMoves.append(tempBoard)
return possibleMoves
"""
def movesLeft(b):
for x in range(0,3):
for y in range(0,3):
if b[x][y] == " ":
return True
return False
def minimax(b, depth, isMax):
#Check base case, see if someone won or tie
if movesLeft(b) == False:
return getScore(b)
#printBoard(b)
if isMax:
maxEval = float("-Inf")
for x in range(0,3):
for y in range(0,3):
if b[x][y] == " ":
b[x][y] = "x"
eval = minimax(b, depth + 1, False)
maxEval = max(maxEval, eval) - depth
b[x][y] = " "
return maxEval
else:
minEval = float("Inf")
for x in range(0,3):
for y in range(0,3):
if b[x][y] == " ":
b[x][y] = "o"
eval = minimax(b, depth + 1, True)
minEval = min(minEval, eval) + depth
b[x][y] = " "
return minEval
def getBestMove(b, isMax):
bestMove = None
if isMax == True:
bestScore = float("-Inf")
else:
bestScore = float("Inf")
for x in range(0,3):
for y in range(0,3):
if b[x][y] == " ":
if isMax == True:
b[x][y] = "x"
else:
b[x][y] = "o"
tempScore = minimax(b, 0, not isMax)
b[x][y] = " "
if isMax == True:
if tempScore > bestScore:
bestScore = tempScore
bestMove = (x,y)
else:
if tempScore < bestScore:
bestScore = tempScore
bestMove = (x,y)
return bestMove
def play():
while True:
if getScore(board) == 10:
print("AI Wins!")
break
elif getScore(board) == -10:
print("Player Wins!")
break
elif getScore(board) == 0 and movesLeft(board) == False:
print("Tie!")
break
#Random starting player
#makeMove(board, getBestMove(board, False), True)
makeMove(board, getInput(), True)
makeMove(board, getBestMove(board, True), False)
def test():
board = [["x"," ","o"],
["x","x","o"],
["o"," "," "]]
print(getBestMove(board, True))
play()