示例#1
0
        /// <summary>
        /// Determine the status of a game and if there is a winner.
        /// </summary>
        /// <param name="g">The game instance.</param>
        /// <param name="round">The round instance.</param>
        protected internal void DetermineGameWinner(Game g, GameRound round)
        {
            #region Param Checking
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (round == null)
            {
                throw new ArgumentNullException("round");
            }
            if (g.Player1 == null)
            {
                throw new ArgumentNullException("g.Player1");
            }
            if (g.Player2 == null)
            {
                throw new ArgumentNullException("g.Player2");
            }
            #endregion

            switch (round.Outcome)
            {
                case GameRoundOutcome.Player1Winner:
                    g.Status = g.Player2.CardsRemaining == 0 ? GameStatus.Player1Wins : GameStatus.InProgress;
                    break;
                case GameRoundOutcome.Player2Winner:
                    g.Status = g.Player1.CardsRemaining == 0 ? GameStatus.Player2Wins : GameStatus.InProgress;
                    break;
                case GameRoundOutcome.Draw:
                    g.Status = GameStatus.Draw;
                    break;
                default:
                    g.Status = GameStatus.InProgress;
                    break;
            }
        }
示例#2
0
        /// <summary>
        /// Log the results of a single round for a game or War.
        /// </summary>
        /// <param name="g">The game instance.</param>
        /// <param name="round">The round instance.</param>
        protected internal void LogRoundResults(Game g, GameRound round)
        {
            #region Param Checking
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (round == null)
            {
                throw new ArgumentNullException("round");
            }
            if (g.Player1 == null)
            {
                throw new ArgumentNullException("g.Player1");
            }
            if (g.Player2 == null)
            {
                throw new ArgumentNullException("g.Player2");
            }
            #endregion

            try
            {
                using (StreamWriter stream = File.AppendText(g.PathToGameLog))
                {
                    stream.Write(string.Format(round.ToString(), (g.Player1.Name ?? "<UNKNOWN>"), (g.Player2.Name ?? "<UNKNOWN>")));
                    stream.WriteLine(g.ToString());

                    switch (g.Status)
                    {
                        case GameStatus.Player1Wins:
                            stream.WriteLine("*********************************************");
                            stream.WriteLine(string.Format("** {0} Wins!", g.Player1.Name));
                            stream.WriteLine("*********************************************");
                            break;
                        case GameStatus.Player2Wins:
                            stream.WriteLine("*********************************************");
                            stream.WriteLine(string.Format("** {0} Wins!", g.Player2.Name));
                            stream.WriteLine("*********************************************");
                            break;
                        case GameStatus.Draw:
                            break;
                        case GameStatus.InProgress:
                        case GameStatus.NotStarted:
                        case GameStatus.Unknown:
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("There wa an exception writing to the log file.  See the inner exception for details.", ex);
            }
        }
示例#3
0
        /// <summary>
        /// For the given game, run a round.
        /// </summary>
        /// <param name="g">The game to run a round for.</param>
        /// <returns>The round.  Also update the status of the game as required.</returns>
        public GameRound RunRound(Game g)
        {
            #region Private Members
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (g.Player1 == null)
            {
                throw new ArgumentNullException("g.Player1");
            }
            if (g.Player2 == null)
            {
                throw new ArgumentNullException("g.Player2");
            }
            #endregion

            GameRound returnValue = new GameRound();

            //1. While the outcome of the card pair is in question:
            while (returnValue.Outcome == GameRoundOutcome.Unknown)
            {
                //A. Create a Card Pair
                PlayingCardPair pair = new PlayingCardPair();
                pair.Player1Card = g.Player1.GetNextCard();
                pair.Player2Card = g.Player2.GetNextCard();

                //B. Determine the outcome of the Pair
                pair.Status = this.GetPairOutcome(pair);

                returnValue.Pairs.Add(pair);

                //C. Loop if there was not a winner.
                if (pair.Status == PlayingCardPairOutcome.Player1Wins)
                {
                    returnValue.Outcome = GameRoundOutcome.Player1Winner;
                }
                else if (pair.Status == PlayingCardPairOutcome.Player2Wins)
                {
                    returnValue.Outcome = GameRoundOutcome.Player2Winner;
                }
                else
                {
                    if (g.Player1.CardsRemaining == 0 && g.Player2.CardsRemaining == 0)
                    {
                        returnValue.Outcome = GameRoundOutcome.Draw;
                    }
                    else
                    {
                        //D. In the case of a draw, add the burn card.
                        pair = new PlayingCardPair();
                        pair.Player1Card = g.Player1.GetNextCard();
                        pair.Player2Card = g.Player2.GetNextCard();
                        pair.Status = PlayingCardPairOutcome.FaceDown;

                        returnValue.Pairs.Add(pair);
                    }
                }
            }

            //2. Add the Loser's and Winner's cards to the winner's queue.
            GamePlayer roundWinner = null;

            switch (returnValue.Outcome)
            {
                case GameRoundOutcome.Player1Winner:
                    roundWinner = g.Player1;
                    break;
                case GameRoundOutcome.Player2Winner:
                    roundWinner = g.Player2;
                    break;
                case GameRoundOutcome.Draw:
                case GameRoundOutcome.Unknown:
                default:
                    break;
            }

            if (roundWinner != null)
            {
                foreach (PlayingCardPair p in returnValue.Pairs)
                {
                    roundWinner.StoreCard(p.Player1Card);
                    roundWinner.StoreCard(p.Player2Card);
                }
            }

            //3. Determine if there was a game winner.
            DetermineGameWinner(g, returnValue);

            //3. Log the results
            this.LogRoundResults(g, returnValue);

            //4. Return the round.
            return returnValue;
        }