Beispiel #1
0
        /// <summary>
        /// Start the game with the given name,number of rows,number of columns and creator.
        /// </summary>
        /// <param name="name">
        /// The name of the game.
        /// </param>
        /// <param name="rows">
        /// Number of rows.
        /// </param>
        /// <param name="cols">
        /// Number of columns.
        /// </param>
        /// <param name="creator">
        /// The creator of this game.
        /// </param>
        /// <returns>
        /// The SearchGame that was started.
        /// </returns>
        public ISearchGame StartGame(string name, int rows, int cols, IClient creator)
        {
            /*
             * If the game already exists, the user didn't used this command as he should and won't be getting a game.
             * If we would join the client anyway, we will ignore his requst to "rows", "cols" and maybe he does't want
             * to play in a different size maze(it's like the difference between requesting a 100 pieces puzzle and
             * getting a 2000 pieces puzzle).
             * If a game with that name already exists, We will generate a new game for the client if and only if
             * he's the only player is the exitising game or, the exisiting game has ended.
             */
            bool        toCreate = true;
            Player      p = connector.GetPlayer(creator);
            ISearchGame g1 = connector.GetGame(p), g = null;

            if (!ReferenceEquals(null, g1))
            {
                toCreate = g1.HasEnded() || (g1.NumOfPlayer == 1 && g1.GetPlayers().Contains(p));
            }
            if (connector.ContainsGame(name))
            {
                g = connector.GetGame(name);

                /* g.NumOfPlayer > 0 always because the server know only multiplayer games
                 * replace the existing game if it has ended or the only player in it in the
                 * one who asks to replace it.*/
                toCreate &= (g.HasEnded() || (g.NumOfPlayer == 1 && g.GetPlayers().Contains(p)));
            }
            if (toCreate)
            {
                // delete exitsing games
                connector.DeleteGame(g);
                try
                {
                    connector.DeleteGame(g1);
                } catch (Exception)
                {
                    //The game with this name was deleted
                }
                // Create a game with this name.
                ISearchGame game = GenerateNewGame(name, rows, cols);
                connector.AddGame(game);
                connector.AddClientToGame(creator, game);
                game.AddPlayer(connector.GetPlayer(creator));
                return(game);
            }
            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Let the given player join the game with the given name.
        /// </summary>
        /// <param name="name">
        /// The name of the game that the player wishes to join.
        /// </param>
        /// <param name="player">
        /// The player that sent the Join command.
        /// </param>
        /// <returns>
        /// True if he joined the game, false otherwise.
        /// </returns>
        public bool Join(string name, IClient player)
        {
            Player      myPlayer = connector.GetPlayer(player);
            ISearchGame game     = connector.GetGame(name);

            // if the player isn't a part of an existing game and the game allows a player to join
            if (ReferenceEquals(null, connector.GetGame(myPlayer)) && game.CanAPlayerJoin())
            {
                connector.AddClientToGame(myPlayer, name);
                return(game.AddPlayer(myPlayer));
            } /*else if (!ReferenceEquals(null,connector.GetGame(myPlayer))) {
               * player.SendResponse(@"Deleted game named: " + connector.GetGame(myPlayer).Name
               + " to generate game: " + name);
               + connector.DeleteGame(connector.GetGame(myPlayer));
               + connector.AddClientToGame(myPlayer, name);
               + return game.AddPlayer(myPlayer);
               + }*/
            return(false);
        }