def scoreBlowUp(board: Board, pos): board = board.copy() before = enemiesVsPlayerHeuristic(board) board.update_explosion(pos) after = enemiesVsPlayerHeuristic(board) #times 100 to ensure it is above movement moves return (after-before)*100
def all_possible_moves(board: Board): moves = [] for pos in board: if (board.at(pos).isMyToken): if(board.nearby_enemy(pos)): moves.append((None, pos, None)) for pos in board: if (board.at(pos).isMyToken): moves.extend(possible_moves(board.at(pos).numTokens, pos, board)) return moves
def blowUps(board: Board): pieces = myPieces(board) moves = [] for pos in pieces: if(board.nearby_enemy(pos)): moves.append((None, pos, None)) return moves
def bestMoves(board: Board): bmap = createbmap(board) pieces = myPieces(board) moves = [] for pos in pieces: if(board.nearby_enemy(pos)): moves.append((scoreBlowUp(board, pos), (None, pos, None))) for move in possible_moves(board[pos].numTokens, pos, board): res = rankPiece(board.amountOfTokensAtDestination(move), move[2], bmap) moves.append((res, move)) moves.sort(key=lambda x:x[0], reverse=True) m = [] for move in moves: m.append(move[1]) return m
def BlowUp(board: Board, isMax: bool): moveBoard = board.copy() moves = blowUps(moveBoard) #blow up all pieces next to enemies while (len(moves) > 0): move = moves[0] moveBoard.update_board(move[1], move[2], move[0]) moves = blowUps(moveBoard) #if min then flip so we rank the board #relative to max if (not isMax): moveBoard.flipPlayer() bestRes = rankBoard(moveBoard) return bestRes
class MainPlayer: def __init__(self, colour): board = {"white": _WHITE_START_SQUARES, "black": _BLACK_START_SQUARES} self.board = Board(board, colour) self.colour = colour self.visted = {} self.totalTime = 0 def action(self): start = time.time() depth = None if (self.totalTime >= 50): depth = 1 bhash = self.board.to_hashable() #if previously visiten chosen the second action to avoid #repeated states if (bhash in self.visted): actions = self.visted[bhash] if (len(actions) > 1): action = actions[1][1] else: action = actions[0][1] #else use AI to determine Move else: actions = determineMove(self.board.copy(), depth) action = actions[0][1] self.visted[bhash] = actions end = time.time() duration = end - start self.totalTime += duration if (action == None): return ("BOOM", ()) if (action[0] == None): return ("BOOM", (action[1])) else: return ("MOVE", action[0], action[1], action[2]) def update(self, colour, action): if (action[0] == "BOOM"): self.board.update_board(action[1], None, None) else: self.board.update_board(action[2], action[3], action[1])
def breakTiesWithRankPieceHeuristic(board: Board, moves): bmap = createbmap(board) topSame = [] notTop = [] topRes = moves[0][0] for (res, move) in moves: if(topRes == res): if(move[0]==None): newres = scoreBlowUp(board, move[1]) else: newres = rankPiece(board.amountOfTokensAtDestination(move), move[2], bmap) topSame.append(((res, move), newres)) else: notTop.append((res,move)) topSame.sort(key=lambda x: x[1], reverse = True) result = [] for move in topSame: result.append(move[0]) result.extend(notTop) return result
def __init__(self, colour): board = {"white": _WHITE_START_SQUARES, "black": _BLACK_START_SQUARES} self.board = Board(board, colour) self.colour = colour self.visted = {} self.totalTime = 0
def cane_make_move(pos: tuple, board:Board): return isInsideBoard(pos) and board.can_move_here(pos)