-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuttt
221 lines (201 loc) · 8.87 KB
/
uttt
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
from time import sleep
from math import inf
from random import randint
class ultimateTicTacToe:
def __init__(self):
"""
Initialization of the game.
"""
self.board=[['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_'],
['_','_','_','_','_','_','_','_','_']]
self.maxPlayer='X'
self.minPlayer='O'
self.maxDepth=3
#The start indexes of each local board
self.globalIdx=[(0,0),(0,3),(0,6),(3,0),(3,3),(3,6),(6,0),(6,3),(6,6)]
#Start local board index for reflex agent playing
self.startBoardIdx=4
#self.startBoardIdx=randint(0,8)
#utility value for reflex offensive and reflex defensive agents
self.winnerMaxUtility=10000
self.twoInARowMaxUtility=500
self.preventThreeInARowMaxUtility=100
self.cornerMaxUtility=30
self.winnerMinUtility=-10000
self.twoInARowMinUtility=-100
self.preventThreeInARowMinUtility=-500
self.cornerMinUtility=-30
self.expandedNodes=0
self.currPlayer=True
def printGameBoard(self):
"""
This function prints the current game board.
"""
print('\n'.join([' '.join([str(cell) for cell in row]) for row in self.board[:3]])+'\n')
print('\n'.join([' '.join([str(cell) for cell in row]) for row in self.board[3:6]])+'\n')
print('\n'.join([' '.join([str(cell) for cell in row]) for row in self.board[6:9]])+'\n')
def evaluatePredifined(self, isMax):
"""
This function implements the evaluation function for ultimate tic tac toe for predifined agent.
input args:
isMax(bool): boolean variable indicates whether it's maxPlayer or minPlayer.
True for maxPlayer, False for minPlayer
output:
score(float): estimated utility score for maxPlayer or minPlayer
"""
#YOUR CODE HERE
score=0
if(isMax == True):
return score
def evaluateDesigned(self, isMax):
"""
This function implements the evaluation function for ultimate tic tac toe for your own agent.
input args:
isMax(bool): boolean variable indicates whether it's maxPlayer or minPlayer.
True for maxPlayer, False for minPlayer
output:
score(float): estimated utility score for maxPlayer or minPlayer
"""
#YOUR CODE HERE
score=0
return score
def checkMovesLeft(self):
"""
This function checks whether any legal move remains on the board.
output:
movesLeft(bool): boolean variable indicates whether any legal move remains
on the board.
"""
#YOUR CODE HERE
movesLeft=True
return movesLeft
def checkWinner(self):
#Return termimnal node status for maximizer player 1-win,0-tie,-1-lose
"""
This function checks whether there is a winner on the board.
output:
winner(int): Return 0 if there is no winner.
Return 1 if maxPlayer is the winner.
Return -1 if miniPlayer is the winner.
"""
#YOUR CODE HERE
winner=0
for i in self.globalIdx:
for j in range(3):
if(self.board[i[0]+j][i[1]]+self.board[i[0]+j][i[1]+1]+self.board[i[0]+j][i[1]+2]=="XXX"):
winner = 1
return winner
elif(self.board[i[0]+j][i[1]]+self.board[i[0]+j][i[1]+1]+self.board[i[0]+j][i[1]+2]=="OOO"):
winner = -1
return winner
elif(self.board[i[0]][i[1]+j]+self.board[i[0]+1][i[1]+j]+self.board[i[0]+2][i[1]+j]=="XXX"):
winner = 1
return winner
elif(self.board[i[0]][i[1]+j]+self.board[i[0]+1][i[1]+j]+self.board[i[0]+2][i[1]+j]=="OOO"):
winner = -1
return winner
if(self.board[i[0]][i[1]]+self.board[i[0]+1][i[1]+1]+self.board[i[0]+2][i[1]+2]=="XXX"or self.board[i[0]+2][i[1]]+self.board[i[0]+1][i[1]+1]+self.board[i[0]][i[1]+2]=="XXX"):
winner = 1
return winner
elif(self.board[i[0]][i[1]]+self.board[i[0]+1][i[1]+1]+self.board[i[0]+2][i[1]+2]=="OOO"or self.board[i[0]+2][i[1]]+self.board[i[0]+1][i[1]+1]+self.board[i[0]][i[1]+2]=="OOO"):
winner = -1
return winner
return 0
def alphabeta(self,depth,currBoardIdx,alpha,beta,isMax):
"""
This function implements alpha-beta algorithm for ultimate tic-tac-toe game.
input args:
depth(int): current depth level
currBoardIdx(int): current local board index
alpha(float): alpha value
beta(float): beta value
isMax(bool):boolean variable indicates whether it's maxPlayer or minPlayer.
True for maxPlayer, False for minPlayer
output:
bestValue(float):the bestValue that current player may have
"""
#YOUR CODE HERE
bestValue=0.0
return bestValue
def minimax(self, depth, currBoardIdx, isMax):
"""
This function implements minimax algorithm for ultimate tic-tac-toe game.
input args:
depth(int): current depth level
currBoardIdx(int): current local board index
alpha(float): alpha value
beta(float): beta value
isMax(bool):boolean variable indicates whether it's maxPlayer or minPlayer.
True for maxPlayer, False for minPlayer
output:
bestValue(float):the bestValue that current player may have
"""
#YOUR CODE HERE
bestValue=0.0
return bestValue
def playGamePredifinedAgent(self,maxFirst,isMinimaxOffensive,isMinimaxDefensive):
"""
This function implements the processes of the game of predifined offensive agent vs defensive agent.
input args:
maxFirst(bool): boolean variable indicates whether maxPlayer or minPlayer plays first.
True for maxPlayer plays first, and False for minPlayer plays first.
isMinimaxOffensive(bool):boolean variable indicates whether it's using minimax or alpha-beta pruning algorithm for offensive agent.
True is minimax and False is alpha-beta.
isMinimaxOffensive(bool):boolean variable indicates whether it's using minimax or alpha-beta pruning algorithm for defensive agent.
True is minimax and False is alpha-beta.
output:
bestMove(list of tuple): list of bestMove coordinates at each step
bestValue(list of float): list of bestValue at each move
expandedNodes(list of int): list of expanded nodes at each move
gameBoards(list of 2d lists): list of game board positions at each move
winner(int): 1 for maxPlayer is the winner, -1 for minPlayer is the winner, and 0 for tie.
"""
#YOUR CODE HERE
bestMove=[]
bestValue=[]
gameBoards=[]
winner=0
return gameBoards, bestMove, expandedNodes, bestValue, winner
def playGameYourAgent(self):
"""
This function implements the processes of the game of your own agent vs predifined offensive agent.
input args:
output:
bestMove(list of tuple): list of bestMove coordinates at each step
gameBoards(list of 2d lists): list of game board positions at each move
winner(int): 1 for maxPlayer is the winner, -1 for minPlayer is the winner, and 0 for tie.
"""
#YOUR CODE HERE
bestMove=[]
gameBoards=[]
winner=0
return gameBoards, bestMove, winner
def playGameHuman(self):
"""
This function implements the processes of the game of your own agent vs a human.
output:
bestMove(list of tuple): list of bestMove coordinates at each step
gameBoards(list of 2d lists): list of game board positions at each move
winner(int): 1 for maxPlayer is the winner, -1 for minPlayer is the winner, and 0 for tie.
"""
#YOUR CODE HERE
bestMove=[]
gameBoards=[]
winner=0
return gameBoards, bestMove, winner
if __name__=="__main__":
uttt=ultimateTicTacToe()
gameBoards, bestMove, expandedNodes, bestValue, winner=uttt.playGamePredifinedAgent(True,False,False)
if winner == 1:
print("The winner is maxPlayer!!!")
elif winner == -1:
print("The winner is minPlayer!!!")
else:
print("Tie. No winner:(")