Пример #1
0
        public bool IsPlayerReachedExit(string playerId)
        {
            MultiPlayerGame game    = this.playerToGame[playerId];
            Position        goalPos = game.Maze.GoalPos;
            Position        curPos  = game.GetPlayer(playerId).Location;

            return(goalPos.Row == curPos.Row && goalPos.Col == curPos.Col);
        }
Пример #2
0
        public string GetCompetitorOf(string playerId)
        {
            if (!playerToGame.ContainsKey(playerId))
            {
                // Player is not participate in any game.
                return(null);
            }
            MultiPlayerGame game = this.playerToGame[playerId];

            return(game.GetCompetitorOf(playerId).PlayerId);
        }
Пример #3
0
        public Maze JoinTo(string nameOfGame, string playerId)
        {
            if (!this.availablesMPGames.ContainsKey(nameOfGame))
            {
                // The game is not exist in the system.
                throw new Exception($"There is no game with the name '{nameOfGame}'");
            }
            // Assign the player to the needed game.
            MultiPlayerGame game = availablesMPGames[nameOfGame];
            Maze            maze = game.Maze;

            game.Guest = new PlayerInfo(playerId, maze.InitialPos);
            // Remove the game from the available games and add it to the unavailable games.
            availablesMPGames.Remove(nameOfGame);
            unAvailablesMPGames.Add(nameOfGame, game);
            playerToGame.Add(playerId, game);
            return(maze);
        }
Пример #4
0
        public Maze StartGame(string nameOfGame, int rows, int cols, string playerId)
        {
            if (GetMazeInfoOf(nameOfGame) != null)
            {
                // The game is already exist in the system.
                throw new Exception($"The game '{nameOfGame}' already exists");
            }
            // Generate a maze with the given size.
            IMazeGenerator generator = new DFSMazeGenerator();
            Maze           maze      = generator.Generate(rows, cols);

            maze.Name = nameOfGame;
            MultiPlayerGame mpGame = new MultiPlayerGame(maze, playerId, maze.InitialPos);

            // Add the game to the suitable dictionaries.
            this.availablesMPGames.Add(nameOfGame, mpGame);
            this.playerToGame.Add(playerId, mpGame);
            return(maze);
        }
Пример #5
0
        public string Play(string direction, string playerId)
        {
            if (!playerToGame.ContainsKey(playerId))
            {
                // Player tried to move although he is not participate in any game.
                throw new Exception("Player is not in a game, need to be in a game to play");
            }
            // Find the player-info of the player.
            MultiPlayerGame game       = playerToGame[playerId];
            PlayerInfo      playerInfo = game.GetPlayer(playerId);
            // Update the player location.
            bool validMove = playerInfo.Move(game.Maze, direction);

            if (!validMove)
            {
                // The direction of the player was invalid.
                throw new Exception($"Invalid Direction '{direction}'");
            }
            return(game.Maze.Name);
        }
Пример #6
0
        public void Close(string nameOfGame)
        {
            MultiPlayerGame game = null;

            if (unAvailablesMPGames.ContainsKey(nameOfGame))
            {
                // Close multiplayer game with 2 players.
                game = unAvailablesMPGames[nameOfGame];
                unAvailablesMPGames.Remove(nameOfGame);
                playerToGame.Remove(game.Guest.PlayerId);
            }
            else if (availablesMPGames.ContainsKey(nameOfGame))
            {
                // Close multiplayer game with one player.
                game = availablesMPGames[nameOfGame];
                availablesMPGames.Remove(nameOfGame);
            }
            else
            {
                // The game with the given name is not exist in the system.
                throw new Exception($"There is no game with the name '{nameOfGame}'");
            }
            playerToGame.Remove(game.Host.PlayerId);
        }