Ejemplo n.º 1
0
        /// <summary>
        /// Construct a new game object.
        /// </summary>
        /// <param name="player1">The first player.</param>
        /// <param name="player2">The second player.</param>
        /// <param name="deck">The deck to play with.</param>
        /// <param name="pathToLogFile">The path to the log file for this game.</param>
        public Game(GamePlayer player1, GamePlayer player2, PlayingDeck deck, string pathToLogFile)
        {
            #region Param Checking
            if (player1 == null)
            {
                throw new ArgumentNullException("player1");
            }
            if (player2 == null)
            {
                throw new ArgumentNullException("player2");
            }
            if (deck == null)
            {
                throw new ArgumentNullException("deck");
            }

            if (String.IsNullOrWhiteSpace(pathToLogFile))
            {
                throw new ArgumentNullException(pathToLogFile);
            }
            try
            {
                Path.GetFullPath(pathToLogFile);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("The path to the log file is not valid.  See inner exception for details.", ex);
            }
            #endregion

            this.Player1 = player1;
            this.Player2 = player2;
            this.Deck = deck;
            this.Status = GameStatus.NotStarted;
            this.PathToGameLog = pathToLogFile;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Start a new game.
        /// </summary>
        /// <param name="player1Name">The name of player 1.</param>
        /// <param name="player2Name">The name of player 2.</param>
        /// <returns>The newly started game.</returns>
        public Game StartGame(string player1Name, string player2Name)
        {
            #region Param Checking
            if (String.IsNullOrWhiteSpace(player1Name))
            {
                throw new ArgumentNullException("player1Name");
            }
            if (String.IsNullOrWhiteSpace(player2Name))
            {
                throw new ArgumentNullException("player2Name");
            }
            #endregion

            GamePlayer p1 = new GamePlayer(player1Name);
            GamePlayer p2 = new GamePlayer(player2Name);

            PlayingDeck deck = null;

            try
            {
                deck = this.DeckService.GetDeck();

                int shuffleCount = this.ShuffleAmount;

                for (int i = 0; i < shuffleCount; i++)
                {
                    this.DeckService.ShuffleDeck(deck);
                }

                for (int j = 0; j < deck.UndealtCards.Count; j += 2)
                {
                    p1.Cards.Enqueue(deck.UndealtCards[j]);

                    //Odd case where there is an uneven number of cards in the deck.
                    if (deck.UndealtCards.Count > j + 1)
                    {
                        p2.Cards.Enqueue(deck.UndealtCards[j + 1]);
                    }

                }

                deck.UndealtCards.Clear();
            }
            catch (Exception ex)
            {
                throw new Exception("There was a problem dealing out the cards.  See inner exception for details.", ex);
            }

            Game g = null;

            try
            {
                g = new Game(p1, p2, deck, this.LogFilePath);

                g.Status = GameStatus.InProgress;
            }
            catch (Exception ex)
            {
                throw new Exception("There was an exception initializing the game.  See inner exception for details.", ex);
            }

            return g;
        }