Ejemplo n.º 1
0
 /// <summary>
 /// Checks the win.
 /// </summary>
 /// <param name="p">The p.</param>
 private void CheckWin(MazeGamePlayer p)
 {
     if (p.Position.Equals(Maze.GoalPos))
     {
         onGameOver(this, new GameOverEventArgs(p.Client));
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Plays the move.
        /// </summary>
        /// <param name="client">The client that moves.</param>
        /// <param name="d">The direction of the movement.</param>
        /// <returns></returns>
        public void PlayMove(IClient client, Direction d)
        {
            if (!Started)
            {
                throw new InvalidOperationException(string.
                                                    Format("The game \"{0}\" did not start yet.", Name));
            }

            if (Finished)
            {
                throw new InvalidOperationException(string.
                                                    Format("The game \"{0}\" is over.", Name));
            }

            MazeGamePlayer player = GetPlayerByClient(client);

            if (player == null)
            {
                throw new InvalidOperationException(string.
                                                    Format("You are not in the game \"{0}\"", Name));
            }

            Position estimated = player.GetEstimatedPos(d);

            if (!CanMoveTo(estimated))
            {
                throw new InvalidOperationException("You can not move to that direction.");
            }

            player.Position = estimated;
            onPlayerMoved(this, player.Client, d);

            CheckWin(player);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Equalses the specified other.
 /// </summary>
 /// <param name="other">The other.</param>
 /// <returns></returns>
 public bool Equals(MazeGamePlayer other)
 {
     if (other == null)
     {
         return(false);
     }
     return(this.Client.Equals(other.Client));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Called when a player won the match
        /// </summary>
        /// <param name="winner"></param>
        private void Finish(MazeGamePlayer winner)
        {
            if (!Started || Finished)
            {
                return;
            }

            Finished = true;
            onGameOver(this, new GameOverEventArgs(winner.Client));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// removes a client from the game.
        /// If less than 2 clients left, closes the game.
        /// </summary>
        /// <param name="client"></param>
        public void RemovePlayer(IClient client)
        {
            MazeGamePlayer player = GetPlayerByClient(client);

            if (player == null)
            {
                throw new InvalidOperationException(string.Format("You are not in the game \"{0}\"", Name));
            }

            players.Remove(player);

            if (players.Count <= 1)
            {
                Close();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds a player to the game.
        /// </summary>
        /// <param name="client"></param>
        public void AddPlayer(IClient client)
        {
            if (Started)
            {
                throw new InvalidOperationException("Game already started");
            }

            if (ContainsClient(client))
            {
                throw new InvalidOperationException("Player already exists");
            }

            MazeGamePlayer player = new MazeGamePlayer(client, Maze.InitialPos);

            players.Add(player);

            if (players.Count >= 2)
            {
                Start();
            }
        }