def PeiceAdvantage(state, colour, oppo_colour): eval=0 for col in range (state.getMinColSize(),state.getMaxColSize()+1): for row in range (state.getMinRowSize(),state.getMaxRowSize()+1): if (Board.comaprePieceat(state,col,row, colour)): eval+=1 if (Board.comaprePieceat(state,col,row, oppo_colour)): eval-=1 return(eval)
def countTotalMoves(board,colour): moves = 0 for row in range(Board.getMinRowSize(board), Board.getMaxRowSize(board)+1): for col in range(Board.getMinColSize(board), Board.getMaxColSize(board)+1): if not(Board.comaprePieceat(board,col,row, "E")): if not(Board.comaprePieceat(board,col,row, colour)): moves += countMoves(board,row, col) return moves
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 evalFromCentre(state,colour,oppo_colour): evaluation=0 totPeices=1 for col in range(state.getMinColSize(),state.getMaxColSize()+1): for row in range(state.getMinRowSize(),state.getMaxRowSize()+1): if(Board.comaprePieceat(state,col,row,colour)): totPeices+=1 evaluation-=abs(distance.euclidean((col,row),(3.5,3.5))) if(Board.comaprePieceat(state,col,row,oppo_colour)): totPeices+=1 evaluation+=abs(distance.euclidean((col,row),(3.5,3.5))) return (evaluation/(totPeices*(state.getMaxColSize()-2)))
def evalPartnerWarriorDiag(state,colour,oppo_colour): evaluation=0 numberOfPeices=1 for col in range(state.getMinColSize(),state.getMaxColSize()+1): for row in range(state.getMinRowSize(),state.getMaxRowSize()+1): if(Board.comaprePieceat(state,col,row,colour)): numberOfPeices+=4 evaluation+=Evaluation.EvalDiag(state, col, row, colour) if(Board.comaprePieceat(state,col,row,oppo_colour)): numberOfPeices+=4 evaluation-=Evaluation.EvalDiag(state, col, row, oppo_colour) return (evaluation/numberOfPeices)
def placingPhaseMoves(board, colour): moves = 0 if (colour == "W"): for col in range(0, 8): for row in range(0, 6): if (Board.comaprePieceat(board, col, row, "E")): moves += 1 if (colour == "B"): for col in range(0, 8): for row in range(2, 8): if (Board.comaprePieceat(board, col, row, "E")): moves += 1 return moves
def piecesLeft(state, colour): count = 0 for col in range(0, 8): for row in range(0, 8): if (Board.comaprePieceat(state, col, row, colour)): count += 1 return count
def placing_phase(board, colour): if (colour == "W"): for col in range(0, 8): for row in range(0, 6): if (Board.comaprePieceat(board, col, row, "E")): Board.placePiece(board, col, row, "W") return (col, row) if (colour == "B"): for col in range(0, 8): for row in reversed(range(2, 8)): if (Board.comaprePieceat(board, col, row, "E")): Board.placePiece(board, col, row, "B") return (col, row) return ()
def evalPartnerWarriorDiag(state, colour, oppo_colour): evaluation = 0 numberOfPeices = 1 if (colour == "W"): for col in range(0, 8): for row in range(0, 6): if (Board.comaprePieceat(state, col, row, "W")): numberOfPeices += 1 evaluation += Evaluation.EvalDiag( state, col, row, colour) if (colour == "B"): for col in range(0, 8): for row in range(2, 8): if (Board.comaprePieceat(state, col, row, "B")): numberOfPeices += 1 evaluation += Evaluation.EvalDiag( state, col, row, colour) return (evaluation / numberOfPeices)
def deathTrap(state, colour, oppo_colour): evaluation = 0 for col in range(0, 8): for row in range(0, 8): if (Board.comaprePieceat(state, col, row, "E")): evaluation += Evaluation.evalDeathTrap( state, col, row, colour) evaluation -= Evaluation.evalDeathTrap( state, col, row, oppo_colour) return evaluation
def deathTrap(state,colour,oppo_colour): evaluation=0 count=1 for col in range(state.getMinColSize(),state.getMaxColSize()+1): for row in range(state.getMinRowSize(),state.getMaxRowSize()+1): if(Board.comaprePieceat(state,col,row,"E")): count+=1 evaluation+=Evaluation.evalDeathTrap(state, col,row, colour) evaluation-=Evaluation.evalDeathTrap(state, col,row, oppo_colour) return (evaluation/count)
def evalFromCentre(state, colour, oppo_colour): evaluation = 0 totPeices = 0 if (colour == "W"): for col in range(0, 8): for row in range(0, 6): if (Board.comaprePieceat(state, col, row, "W")): totPeices += 1 evaluation += distance.euclidean((col, row), (3.5, 3.5)) if (colour == "B"): for col in range(0, 8): for row in range(2, 8): if (Board.comaprePieceat(state, col, row, "W")): totPeices += 1 evaluation += distance.euclidean((col, row), (3.5, 3.5)) return evaluation
def distanceLine(state, colour, oppo_colour): numberOfPeices = 1 eval = 0 if (colour == "W"): for col in range(0, 8): for row in range(0, 6): if (Board.comaprePieceat(state, col, row, colour)): numberOfPeices += 1 if (row > 1): eval += (1 / (row - 1)) else: eval += 0.75 if (colour == "B"): for col in range(0, 8): for row in range(2, 8): if (Board.comaprePieceat(state, col, row, colour)): numberOfPeices += 1 if (row < 6): eval += (1 / (6 - row)) else: eval += 0.75 return (eval / numberOfPeices)
def distanceLine(state, colour, oppo_colour): numberOfPeices=1 eval=0 if(colour=="W"): for col in range(state.getMinColSize(),state.getMaxColSize()+1): for row in range(state.getMinRowSize(),state.getMaxRowSize()+1): if (Board.comaprePieceat(state,col,row, colour)): numberOfPeices+=1 if(row>1): eval+=(1/(row-1)) else: eval+=0.75 if (Board.comaprePieceat(state,col,row, oppo_colour)): numberOfPeices+=1 if(row<6): eval-=(1/(6-row)) else: eval-=0.75 if(colour=="B"): for col in range(state.getMinColSize(),state.getMaxColSize()+1): for row in range(state.getMinRowSize(),state.getMaxRowSize()+1): if (Board.comaprePieceat(state,col,row, colour)): numberOfPeices+=1 if(row<6): eval+=(1/(6-row)) else: eval+=0.75 if (Board.comaprePieceat(state,col,row, oppo_colour)): numberOfPeices+=1 if(row>1): eval-=(1/(row-1)) else: eval-=0.75 return (eval/numberOfPeices)
def mainFourSquares(state,colour, oppo_colour): eval=0 if(Board.comaprePieceat(state,3,3,colour)): eval+=1 if(Board.comaprePieceat(state,3,4,colour)): eval+=1 if(Board.comaprePieceat(state,4,3,colour)): eval+=1 if(Board.comaprePieceat(state,4,4,colour)): eval+=1 if(Board.comaprePieceat(state,3,3,oppo_colour)): eval-=1 if(Board.comaprePieceat(state,3,4,oppo_colour)): eval-=1 if(Board.comaprePieceat(state,4,3,oppo_colour)): eval-=1 if(Board.comaprePieceat(state,4,4,oppo_colour)): eval-=1 return(eval/4)
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 checkMove(board, col, row): possibleMoves = [] # Check up if its free above and in the board range if (Board.validSquare(board,col,row-1) and Board.comaprePieceat(board, col,row - 1, "E")): possibleMoves.append([col,row-1]) # Check up for a jump over another piece above elif (Board.validSquare(board,col,row-2) and not (Board.comaprePieceat(board, col,row - 1, "E")) and Board.comaprePieceat(board,col,row-2,"E")): possibleMoves.append([col,row-2]) # Check down if its free below and in the board range if (Board.validSquare(board,col,row+1) and Board.comaprePieceat(board,col, row + 1, "E")): possibleMoves.append([col,row+1]) # Check down for a jump below over another piece elif (((Board.validSquare(board,col,row+2)) and not(Board.comaprePieceat( board,col,row - 1, "E"))) and (Board.comaprePieceat(board,col,row + 2, "E"))): possibleMoves.append([col,row+2]) # check left if its free to the left and in the board range if (Board.validSquare(board,col-1,row) and Board.comaprePieceat(board, col-1, row, "E")): possibleMoves.append([col-1,row]) # check left for a jump over another piece to the left elif ((Board.validSquare(board,col-2,row) and not(Board.comaprePieceat(board, col-1,row, "E"))) and (Board.comaprePieceat(board,col-2,row, "E"))): possibleMoves.append([col-2,row]) # check right if its free to the right and in the board range if (Board.validSquare(board,col+1,row) and (Board.comaprePieceat(board, col+1,row, "E"))): possibleMoves.append([col+1,row]) # check right for a jump over another piece to the right elif ((Board.validSquare(board,col+2,row) and not(Board.comaprePieceat(board, col+1,row, "E"))) and (Board.comaprePieceat(board,col+2,row, "E"))): possibleMoves.append([col+2,row]) #return the total number of moves available for that piece return possibleMoves
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
def checkDiagRightDown(board,col,row, colour): if (Board.validSquare(board,col+1,row+1) and (Board.comaprePieceat( board,col+1,row+1, colour))): return 1 return 0
def checkDiagLeftUp(board,col,row, colour): if (Board.validSquare(board,col-1,row-1) and (Board.comaprePieceat( board,col-1,row-1, colour))): return 1 return 0