Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Game:
- def __init__(self, boardSize):
- self.boardSize = boardSize # Holds the size of the board
- self.marks = np.empty((boardSize, boardSize),dtype='str') # Holds the mark for each position
- self.marks[:,:] = ' '
- # Prints the game board using current marks
- def printBoard(self):
- # Prthe column numbers
- print(' ',end='')
- for j in range(self.boardSize):
- print(" "+str(j+1), end='')
- # Prthe rows with marks
- print("")
- for i in range(self.boardSize):
- # Prthe line separating the row
- print(" ",end='')
- for j in range(self.boardSize):
- print("--",end='')
- print("-")
- # Prthe row number
- print(i+1,end='')
- # Prthe marks on self row
- for j in range(self.boardSize):
- print("|"+self.marks[i][j],end='')
- print("|")
- # Prthe line separating the last row
- print(" ",end='')
- for j in range(self.boardSize):
- print("--",end='')
- print("-")
- # Attempts to make a move given the row,col and mark
- # If move cannot be made, returns False and prints a message if mark is 'X'
- # Otherwise, returns True
- def makeMove(self, row, col, mark):
- if(0<=row<self.boardSize and 0<=col<self.boardSize and self.marks[row][col] == ' '):
- self.marks[row][col] = mark
- return True
- else:
- return False
- def checkWin(self, mark):
- for i in range(self.boardSize):
- hasWon = True
- for j in range(self.boardSize):
- if(self.marks[i][j] != mark):
- hasWon = False
- if(hasWon):
- return True
- hasWon = True
- for j in range(self.boardSize):
- if(self.marks[j][i] != mark):
- hasWon = False
- if(hasWon):
- return True
- hasWon = True
- for i in range(self.boardSize):
- if self.marks[i][i] != mark:
- hasWon = False
- if(hasWon):
- return True
- hasWon = True
- for i in range(self.boardSize):
- if(self.marks[self.boardSize-1-i][i] != mark):
- hasWon = False
- if(hasWon):
- return True
- def noMoreMoves(self):
- return not(game.marks == ' ').any()
- # This method should run minimax to determine the value of each move
- # Then make best move for the computer by placing the mark in the best spot
- def makeCompMove(self):
- # Depth limit of 5
- v, best_action = self.max_value(5)
- (row,col)=best_action
- self.makeMove(row, col, 'O')
- # return utility to computer player
- def utility(self):
- if self.checkWin('O'):
- return 1000
- elif self.checkWin('X'):
- return -1000
- #simplified version for now:
- else:
- return 0
- #return possible moves
- def possibleMoves(self):
- possible=[]
- for i in range(self.boardSize):
- for j in range(self.boardSize):
- if (self.marks[i][j]==' '):
- possible.append((i,j))
- return possible
- def max_value(self, depth):
- # if current board is in game ending state, return the utility to computer player
- if self.checkWin('O'):
- return 999, None
- elif self.checkWin('X'):
- return -999, None
- elif self.noMoreMoves():
- return 0,None
- if depth<0:
- return self.utility(), None
- v = -math.inf
- best_action = None
- # go through all possible marks that can be made (i.e. move = possible coordinate of mark)
- for (row,col) in self.possibleMoves():
- #It’s easiest to use the backtracking method. That is, instead of generating
- #hypothetical states, just apply the moves to the game board,
- #compute the utility, and then backtrack the move. This requires
- #that you save the current board configuration before trying each move,
- #so you can backtrack it later
- #save current board
- savedboard=self.marks.copy()
- # make current move (mark) on board
- self.makeMove(row, col, 'O')
- min_val, act = self.min_value(depth-1)
- if (min_val > v):
- v = min_val
- best_action = (row,col)
- # backtrack to prior state: make ' ' mark at same coordinate
- self.marks=savedboard
- return v, best_action
- def min_value(self, alpha, beta, depth):
- # if current board is in game ending state, return the utility to computer player
- if self.checkWin('O'):
- return 999, None
- elif self.checkWin('X'):
- return -999, None
- elif self.noMoreMoves():
- return 0,None
- if depth<0:
- return self.utility(), None
- v = math.inf
- best_action = None
- # go through all possible marks that can be made (i.e. move = possible coordinate of mark)
- for (row,col) in self.possibleMoves():
- #It’s easiest to use the backtracking method. That is, instead of generating
- #hypothetical states, just apply the moves to the game board,
- #compute the utility, and then backtrack the move. This requires
- #that you save the current board configuration before trying each move,
- #so you can backtrack it later
- #save current board
- savedboard=self.marks.copy()
- # make current move (mark) on board
- self.makeMove(row, col, 'X')
- # update alpha beta
- max_val, act = self.max_value(alpha, beta,depth-1)
- if (max_val < v):
- v = max_val
- best_action = (row,col)
- # backtrack to prior state: make ' ' mark at same coordinate
- self.marks=savedboard
- # break early
- if v <= alpha:
- return v, best_action
- beta = min(beta, v)
- return v, best_action
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement