def move(playerName: str, destination: tuple, game: Dungeon): """ Updates the game to reflect the movement of a player if they can move to the given destination in the game. Returns an unmodified game if the player cannot move based on game rules. :param playerName: str :param destination: tuple :param game: Dungeon """ currLevel: Level = game.levels[game.currLevel] currBoardNum = whichBoardInLevel(currLevel, destination) currBoard: Board = currLevel.boards[currBoardNum] player = currBoard.players[playerName] numMoves = 2 if playerCanMoveTo(destination, player, currLevel, numMoves): updatedLevel = moveEntity(currLevel, playerName, currBoardNum, destination, isPlayer=True) game.levels[game.currLevel] = updatedLevel updatedGame = interact(playerName, destination, game) return updatedGame else: return game
def move(entityName: str, destination, game: Dungeon, isPlayer=True): """ Updates the game to reflect the movement of a player if they can move to the given destination in the game or skip a turn when destination is None. Returns an unmodified game if the player cannot move based on game rules. :param entityName: str :param destination: tuple | None :param game: Dungeon """ currLevel: Level = game.levels[game.currLevel] # Player move is skipped if destination is None: # only increment player turn updatedPlayerTurn = currLevel.playerTurn + 1 if updatedPlayerTurn == len(game.players) - 1: currLevel.playerTurn = 0 else: currLevel.playerTurn = updatedPlayerTurn return game entity = getPlayer(currLevel, entityName) if isPlayer else getEnemy( currLevel, entityName) currBoardNum = whichBoardInLevel(currLevel, entity.location) newBoardNum = whichBoardInLevel(currLevel, destination) numMoves = 2 if isPlayer and playerCanMoveTo(destination, entity, currLevel, numMoves): updatedLevel = moveEntity(currLevel, entityName, currBoardNum, newBoardNum, destination, isPlayer=True) if updatedLevel.playerTurn == len(game.players) - 1: updatedLevel.playerTurn = 0 updatedLevel.enemyTurn = updatedLevel.enemyTurn + 1 else: updatedLevel.playerTurn = updatedLevel.playerTurn + 1 updatedLevel.enemyTurn = updatedLevel.enemyTurn + 1 game.levels[game.currLevel] = updatedLevel updatedGame = interact(entityName, destination, game) return updatedGame elif not isPlayer: updatedLevel = moveEntity(currLevel, entityName, currBoardNum, newBoardNum, destination, isPlayer=False) game.levels[game.currLevel] = updatedLevel updatedGame = enemyInteract(entityName, destination, game) return updatedGame else: return game
def executeTurns(game: Dungeon, maxNumTurns: int, jsonActorMoveListList: list, jsonNameList: list, jsonLevel: dict): # Send initial player updates to all players in game managerTrace = getPlayersUpdates(game, []) i = 0 while i < maxNumTurns: # 0. Exit checking if anyOutOfMoves(jsonActorMoveListList) or isCurrentLevelOver(game): break # 1. Run all moves in turn for j in range(len(jsonActorMoveListList) ): # execute each move for a player in a turn playerMoveList = jsonActorMoveListList[j] validMove = False while not validMove and len(playerMoveList) > 0: # do first move in playerMoveList, if valid, set flag move = playerMoveList[0] playerName = jsonNameList[j] if not isPlayerInGame(playerName, game): break currLevel: Level = game.levels[game.currLevel] player = getPlayer(currLevel, playerName) # pot bug?? dest = move["to"] # apply if not dest: # skipped turn validMove = True managerTrace.append([playerName, move, "OK"]) # pot bug adding?? managerTrace = getPlayersUpdates(game, managerTrace) elif playerCanMoveTo(intifyTuple(dest), player, currLevel, 2): GameManager.move(playerName, intifyTuple(dest), game) # pot bug not mutating?? managerTrace.append( getMoveStatus(currLevel, playerName, move)) # pot bug wrong return types?? managerTrace = getPlayersUpdates(game, managerTrace) validMove = True else: managerTrace.append([playerName, move, "Invalid"]) validMove = False del playerMoveList[0] i += 1 return managerTrace, game
def testPlayerCanMoveToPlayer(): dest = (11, 1) result = playerCanMoveTo(dest, player2, exampleLevel2, 2) assert not result
def testPlayerCanMoveToVoid(): dest = (5000, 1000) result = playerCanMoveTo(dest, player1, exampleLevel2, 2) assert not result
def testPlayerCanMoveToValid(): dest = (12, 2) result = playerCanMoveTo(dest, player1, exampleLevel2, 2) assert result