def PwnMove(playerData): """ Takes in PlayerData config and returns the next move the pawn should make along the shortest path deals with jump rules PwnMove(playerData object) -> move object """ r1=playerData.playerLocations[playerData.playerId-1][0] c1=playerData.playerLocations[playerData.playerId-1][1] path = get_shortest_path(playerData, (r1,c1), Checkrow(BOARD_DIM, playerData.playerId)) neighbors = get_neighbors(playerData, playerData.playerLocations[playerData.playerId-1][0], playerData.playerLocations[playerData.playerId-1][1]) move = PlayerMove(playerData.playerId, True, r1, c1, path[0][0], path[0][1]) if path[0] in playerData.playerLocations: otherPL = get_neighbors(playerData, path[0][0], path[0][1]) validN=deepcopy(otherPL) for cell in otherPL: if (cell[0],cell[1]) in playerData.playerLocations: validN.remove(cell) if len(path) > 1: if [path[1][0],path[1][1]] in otherPL and not(path[1] in playerData.playerLocations): rowj=None colj=None Up = None Left = None if [r1+2,c1] in validN: rowj=get_shortest_path(playerData, (r1+2,c1), Checkrow(BOARD_DIM, playerData.playerId)) Up = False if [r1, c1+2] in validN: colj=get_shortest_path(playerData, (r1,c1+2), Checkrow(BOARD_DIM, playerData.playerId)) Left = False if [r1-2,c1] in validN: rowj=get_shortest_path(playerData, (r1-2,c1), Checkrow(BOARD_DIM, playerData.playerId)) Up = True if [r1, c1-2] in validN: colj=get_shortest_path(playerData, (r1,c1-2), Checkrow(BOARD_DIM, playerData.playerId)) Left = True if colj == None and rowj == None: return PlayerMove(playerData.playerId, True, r1, c1, path[1][0], path[1][1]) if colj != None: if Left: return PlayerMove(playerData.playerId, True, r1, c1, r1, c1-2) else: return PlayerMove(playerData.playerId, True, r1, c1, r1, c1+2) if rowj != None: if Up: return PlayerMove(playerData.playerId, True, r1, c1, r1-2, c1) else: return PlayerMove(playerData.playerId, True, r1, c1, r1+2, c1) if validN != []: min=None for tile in validN: current = get_shortest_path(playerData, (tile[0], tile[1]), Checkrow(BOARD_DIM, playerData.playerId)) if min == None or len(current) < len(min): min = current move = PlayerMove(playerData.playerId, True, r1, c1, min[0][0], min[0][1]) elif validN == [] and playerData.numWalls[playerData.playerId-1] <= 0: move = PlayerMove(playerData.playerId, True, r1, c1, r1, c1) elif validN == []: move = wallMove(playerData) return move
def move(playerData): """ Parts 2 - 4 The engine calls this function at each moment when it is this player's turn to make a move. This function decides what kind of move, wall placement or piece move, to make. Parameters playerData: this player's data, originally built by this module in init() returns: the move chosen, in the form of an instance of PlayerMove """ # This function is called when it's your turn to move # Here you'll figure out what kind of move to make and then return that # move. We recommend that you don't update your data structures here, # but rather in last_move. If you do it here, you'll need a special case # check in last_move to make sure you don't update your data structures # twice. # In part 3, any legal move is acceptable. In part 4, you will want to # implement a strategy # Placeholder, fill in these values yourself move = PlayerMove(-1, False, -1, -1, -1, -1) return move
def move(playerData): """The engine calls this function when it wants you to make a move. Parameters: playerData - your player data, which contains whatever you need to keep track of You return: playerData - your player data, which contains whatever you need to keep track of playerMove - your next move """ playerData.logger.write("move() called") # generate next move thisMove = MakeMove(playerData) # Populate these values playerId = playerData.playerId # 0-5 accesses ID stored with playerData position = thisMove.move['position'] # (row, column) tileName = thisMove.move['tileName'] # a-j rotation = thisMove.move[ 'rotation'] # 0-3 (0 = north, 1 = east, 2 = south, 3 = west) playerMove = PlayerMove(playerId, position, tileName, rotation) # setTile() for my move playerData.tiles[playerMove.position].setTile(playerMove.tileName, playerMove.rotation) return playerData, playerMove
def wallMove(playerData): """ Returns a move object given the playerData object """ paths=[] iD=1 for key in playerData.playerLocations: if key == False: paths.append(9999999) elif key == playerData.playerLocations[playerData.playerId-1]: paths.append(9999999) else: paths.append(len(get_shortest_path(playerData, key, Checkrow(BOARD_DIM, iD)))) iD+=1 lowId=paths.index(min(paths)) AttackP=get_shortest_path(playerData, playerData.playerLocations[lowId], Checkrow(BOARD_DIM, lowId+1)) r1=AttackP[0][0] c1=AttackP[0][1] moveL=[] i=0 while i < 3: moveL.append(PlayerMove(playerData.playerId, False, r1, c1, r1+2, c1)) moveL.append(PlayerMove(playerData.playerId, False, r1, c1, r1, c1+2)) moveL.append(PlayerMove(playerData.playerId, False, r1, c1, r1-2, c1)) moveL.append(PlayerMove(playerData.playerId, False, r1, c1, r1, c1-2)) if i == 0: r1-=1 if i == 1: c1+=1 if i == 2: r1+=1 i+=1 moves={} for move in moveL: if ValidWall(playerData, move.r1, move.c1, move.r2, move.c2): if not(isBlock(playerData, move)): data=PlayerData(playerData.logger,deepcopy(playerData.playerId), deepcopy(playerData.playerLocations), BOARD_DIM, deepcopy(playerData.numWalls)) data.walls=deepcopy(playerData.walls) data.board=deepcopy(playerData.board) data.movesmade=deepcopy(playerData.movesmade) data.numWalls=deepcopy(playerData.numWalls) last_move(data, move) score=0 iD=1 for key in playerData.playerLocations: if key == False: pass elif key == playerData.playerLocations[playerData.playerId-1]: pass else: score+=len(get_shortest_path(data, key, Checkrow(BOARD_DIM, iD))) iD+=1 moves[score] = move best=None for key in moves: if best == None or key > best: best = key if best == None: return PwnMove(playerData) data=PlayerData(playerData.logger,deepcopy(playerData.playerId), deepcopy(playerData.playerLocations), BOARD_DIM, deepcopy(playerData.numWalls)) data.walls=deepcopy(playerData.walls) data.board=deepcopy(playerData.board) data.movesmade=deepcopy(playerData.movesmade) data.numWalls=deepcopy(playerData.numWalls) last_move(data, moves[best]) befor = len(AttackP) after = len(get_shortest_path(data, playerData.playerLocations[lowId], Checkrow(BOARD_DIM, lowId+1))) if after <= befor: return PwnMove(playerData) return moves[best]