def update(self, action): #update number of turns self.no_turns=self.no_turns+1 self.our_turns+=1 #shrink board when number of turns achived if(self.no_turns==128): Game.shrinkBoard(self.board) if(self.no_turns==192): Game.shrinkBoard(self.board) if(self.our_turns==24): self.placingPhase=False #remove kill before anything Game.removeKills(self.board, self.team_colour, self.oppo_colour) if (action != None): #if its the placing phase if(self.placingPhase): #after placing the peice check for kills Board.placePiece(self.board,action[0],action[1],self.oppo_colour) Game.removeKillsAfterAction(self.board, action[0], action[1], self.team_colour, self.oppo_colour) else: #after moivng check for kills Board.movePiece(self.board,action[0][0], action[0][1], action[1][0],action[1][1]) Game.removeKillsAfterAction(self.board, action[1][0], action[1][1], self.team_colour, self.oppo_colour)
def minimax(newBoard, colour, oppo_colour, type): beta = math.inf bestVal = -math.inf bestState = None cord = Player.getSuccessors(newBoard, colour, "bs", type) count = 0 MaxCount = 0 #for every state call minimax on them for board in Player.getSuccessors(newBoard, colour, "boards", type): value = Player.min_value(board, 2, colour, oppo_colour, bestVal, beta, type) if (value > bestVal): MaxCount = count bestVal = value bestState = board count += 1 #if its the placing phase place the piece on the best possible move if (type == "PP"): Board.placePiece(newBoard, cord[MaxCount][0], cord[MaxCount][1], colour) #if its the moving phase move the piece to the best possible position if (type == "MP"): Board.movePiece(newBoard, cord[MaxCount][0], cord[MaxCount][1], cord[MaxCount][2], cord[MaxCount][3]) return cord[MaxCount]
def getSuccessors(board, colour, needCord, type, oppo_colour): Successors=[] cord=[] #if its the placing phase then get states for that phase if (type=="PP"): #if you want the board as the output if(colour == "W"): for col in range (0,8): for row in range (0,6): if (Board.comaprePieceat(board,col,row, "E")): #make a copy of the board that will represent a new state tempBoard=copy.deepcopy(board) Board.placePiece(tempBoard,col,row,"W") #check if after placing the piece there is something # that can be eliminated from the board and remove it #from the board Game.removeKills(tempBoard, colour, oppo_colour) Successors.append(tempBoard) if (needCord): cord.append((col, row)) if(colour == "B"): for col in range (0,8): for row in range (2,8): if (Board.comaprePieceat(board,col,row, "E")): #make a copy of the board that will represent a new state tempBoard=copy.deepcopy(board) Board.placePiece(tempBoard,col,row,"B") # check if after placing the piece there is something # that can be eliminated from the board and remove it #from the board Game.removeKills(tempBoard, colour, oppo_colour) Successors.append(tempBoard) if (needCord): cord.append((col, row)) # if its in the moving phase then get states for that phase if (type=="MP"): #check all the possible moves peices of our colour can make and return states for row in range(Board.getMinRowSize(board), Board.getMaxRowSize(board)+1): for col in range(Board.getMinColSize(board), Board.getMaxColSize(board)+1): if (Board.comaprePieceat(board,col,row, colour )): temp=checkMove(board,col, row) for pos in temp: #make a copy of the board that will represent a new state tempBoard=copy.deepcopy(board) #move the peice to the new location Board.movePiece(tempBoard,col,row,pos[0],pos[1]) #remove kills Game.removeKills(tempBoard, colour,oppo_colour ) Successors.append(tempBoard) if (needCord): cord.append((col,row,pos[0],pos[1])) return (cord,Successors)
def update(self, action): Board.printBoard(self.board) self.no_turns = self.no_turns + 1 self.our_turns += 1 if (self.no_turns == 128): Game.shrinkBoard(self.board) if (self.no_turns == 192): Game.shrinkBoard(self.board) if (self.our_turns == 24): self.placingPhase = False print("CHnaged to mving phase") Game.removeKills(self.board) print(self.no_turns) print(action) if (self.placingPhase): Board.placePiece(self.board, action[0], action[1], self.oppo_colour) else: Board.movePiece(self.board, action[0][0], action[0][1], action[1][0], action[1][1])
def minimax(newBoard,colour,oppo_colour, type, evalWeights, cutOff): #initialize beta and other needed variables beta=math.inf bestVal=-math.inf bestState=None #get the first set of cordinates so we could return when we get the state #cord=getSuccessors(newBoard, colour,"bs",type, oppo_colour) count=0 MaxCount=0 #for every state call min_value on them #note:we are running 2 depth here itseldf cord, bds = getSuccessors(newBoard, colour,True, type, oppo_colour) for board in bds: #if we want depth 1 if (cutOff==1): value=Evaluation.evalFinal(board,colour,oppo_colour, evalWeights) else: value = min_value(board, 2, colour, oppo_colour , bestVal, beta, type, evalWeights, cutOff) #get the max value (max val) if (value > bestVal): MaxCount=count bestVal = value bestState = board count+=1 #if its the placing phase place the piece on the best possible move if (type=="PP"): Board.placePiece(newBoard,cord[MaxCount][0], cord[MaxCount][1],colour) #if its the moving phase move the piece to the best possible position if (type=="MP"): if(len(cord)==0): return None Board.movePiece(newBoard,cord[MaxCount][0],cord[MaxCount][1], cord[MaxCount][2],cord[MaxCount][3]) return cord[MaxCount]
def moving(board, colour): for col in range(0, 8): for row in range(0, 8): if (Board.comaprePieceat(board, col, row, colour)): if Game.freeUp(board, col, row): Board.movePiece(board, col, row, col, row - 1) return ((col, row), (col, row - 1)) if Game.freeDown(board, col, row): Board.movePiece(board, col, row, col, row + 1) return ((col, row), (col, row + 1)) if Game.freeRight(board, col, row): Board.movePiece(board, col, row, col + 1, row) return ((col, row), (col + 1, row)) if Game.freeLeft(board, col, row): Board.movePiece(board, col, row, col - 1, row) return ((col, row), (col - 1, row)) return ()
def getSuccessors(board, colour, output, type): Successors = [] #if its in the placing phase then get states for that phase if (type == "PP"): #if you want the board as the output if (output == "boards"): if (colour == "W"): for col in range(0, 8): for row in range(0, 6): if (Board.comaprePieceat(board, col, row, "E")): tempBoard = copy.deepcopy( board ) #make a copy of the board that will represent a new state Board.placePiece(tempBoard, col, row, "W") Game.removeKills( tempBoard ) # check if after placing the piece there is something # that can be eliminated from the board and remove it from the board Successors.append(tempBoard) if (colour == "B"): for col in range(0, 8): for row in range(2, 8): if (Board.comaprePieceat(board, col, row, "E")): tempBoard = copy.deepcopy( board ) #make a copy of the board that will represent a new state Board.placePiece(tempBoard, col, row, "B") Game.removeKills( tempBoard ) # check if after placing the piece there is something # that can be eliminated from the board and remove it from the board Successors.append(tempBoard) #if you want the output as coordinates else: if (colour == "W"): for col in range(0, 8): for row in range(0, 6): if (Board.comaprePieceat(board, col, row, "E")): Successors.append((col, row)) if (colour == "B"): for col in range(0, 8): for row in range(2, 8): if (Board.comaprePieceat(board, col, row, "E")): Successors.append((col, row)) # if its in the moving phase then get states for that phase if (type == "MP"): if (output == "boards"): for row in range(Board.getMinRowSize(board), Board.getMaxRowSize(board) + 1): for col in range(Board.getMinColSize(board), Board.getMaxColSize(board) + 1): if (Board.comaprePieceat(board, col, row, colour)): temp = Player.checkMove(board, col, row) for pos in temp: tempBoard = copy.deepcopy( board ) #make a copy of the board that will represent a new state Board.movePiece(tempBoard, col, row, pos[0], pos[1]) Game.removeKills(tempBoard) Successors.append(tempBoard) else: for row in range(Board.getMinRowSize(board), Board.getMaxRowSize(board) + 1): for col in range(Board.getMinColSize(board), Board.getMaxColSize(board) + 1): if (Board.comaprePieceat(board, col, row, colour)): temp = Player.checkMove(board, col, row) for pos in temp: Successors.append((col, row, pos[0], pos[1])) return Successors