Exemplo n.º 1
0
        public virtual void Play(Board board)
        {
            bool hasPlayedCard;
            hasPlayedCard = PlayReserveCardToBuild(board) ||
                PlayCardFromHandToBuild(board) ||
                PlayFromDiscardPileToBuild(board);

            if (!hasPlayedCard && board.GetHand(this).Count != 0 && board.Winner == null)
                Discard(board);
        }
Exemplo n.º 2
0
        public void AddPlayersTest()
        {
            Board board = new Board();
            IPlayer player1 = new SimplePlayer();
            IPlayer player2 = new SimplePlayer();
            board.AddPlayer(player1);
            board.AddPlayer(player2);

            Assert.IsNotNull(board.Players);
            Assert.AreEqual(2, board.Players.Count);

            Assert.AreEqual(player1, board.CurrentPlayer);
            board.NextPlayer();
            Assert.AreEqual(player2, board.CurrentPlayer);
            board.NextPlayer();
            Assert.AreEqual(player1, board.CurrentPlayer);
        }
Exemplo n.º 3
0
 private bool PlayCardFromHandToBuild(Board board)
 {
     foreach (Pile pile in board.BuildPiles)
     {
         Pile hand = board.GetHand(this);
         foreach (Card card in hand)
         {
             int playedValue;
             if (pile.IsLegalPlay(card, pile, out playedValue))
             {
                 card.PlayedValue = playedValue;
                 board.DoPlay(new Play(hand, card, pile));
                 return true;
             }
         }
     }
     return false;
 }
Exemplo n.º 4
0
 public void BoardTest()
 {
     Board board = new Board();
     Assert.IsNotNull(board.DrawPile);
     Assert.AreEqual(Board.TotalCards, board.DrawPile.Count);
     AssertDrawPileHistogramCorrect(board);
 }
Exemplo n.º 5
0
 private static Board DiscardOnly2PlayerSetup()
 {
     Board board = new Board();
     IPlayer player1 = new DiscardOnlyPlayer();
     IPlayer player2 = new DiscardOnlyPlayer();
     board.AddPlayer(player1);
     board.AddPlayer(player2);
     return board;
 }
Exemplo n.º 6
0
 private static Board Default2PlayerSetup()
 {
     Board board = new Board();
     IPlayer player1 = new SimplePlayer();
     IPlayer player2 = new SimplePlayer();
     board.AddPlayer(player1);
     board.AddPlayer(player2);
     return board;
 }
Exemplo n.º 7
0
        private static void AssertFullDeck(Board board)
        {
            Histogram histogram = new Histogram();

            histogram.AddPile(board.DrawPile);

            foreach (IPlayer player in board.Players)
            {
                histogram.AddPile(board.GetReservePile(player));
                histogram.AddPile(board.GetHand(player));
                foreach (Pile pile in board.GetDiscardPiles(player))
                    histogram.AddPile(pile);
            }

            foreach (Pile pile in board.BuildPiles)
                histogram.AddPile(pile);

            histogram.AssertFullDeck();
        }
Exemplo n.º 8
0
        private static void AssertDrawPileIsRandom(Board board)
        {
            Card previousCard = null;
            RunCounter rcInc = new RunCounter();
            RunCounter rcDec = new RunCounter();
            RunCounter rcEqual = new RunCounter();

            foreach (Card card in board.DrawPile)
            {
                if (previousCard == null)
                {
                    rcInc.Extend(true);
                    rcDec.Extend(true);
                    rcEqual.Extend(true);
                }
                else
                {
                    rcInc.Extend(previousCard.Value == card.Value - 1);
                    rcDec.Extend(previousCard.Value == card.Value + 1);
                    rcEqual.Extend(previousCard.Value == card.Value);
                }

                previousCard = card;
            }

            Debug.WriteLine("DrawPile Max ascending sequence is " + rcInc.Max);
            Debug.WriteLine("DrawPile Max descending sequence is " + rcDec.Max);
            Debug.WriteLine("DrawPile Max equal sequence is " + rcEqual.Max);
            Debug.WriteLine("Deck is " + board.DrawPile);
            // Probability of 4 in a row is 1/12^4 or about one in 20,000 cards
            if (rcInc.Max > 4 || rcDec.Max > 4 || rcEqual.Max > 4)
            {
                Assert.Fail("DrawPile is not random.");
            }
        }
Exemplo n.º 9
0
 private static void AssertDrawPileHistogramCorrect(Board board)
 {
     Histogram histogram = new Histogram();
     histogram.AddPile(board.DrawPile);
     histogram.AssertFullDeck();
 }
Exemplo n.º 10
0
        private static void AssertBoardValid(Board board)
        {
            AssertFullDeck(board);

            foreach (IPlayer player in board.Players)
            {
                Assert.IsTrue(board.GetReservePile(player).Count <= Board.ReservePileMax, "Excessive reserve pile: " + board.GetReservePile(player).Count);
                Assert.AreEqual(Board.NumDiscardPiles, board.GetDiscardPiles(player).Count, "Wrong number of discard piles.");
                Assert.AreEqual(Board.NumBuildPiles, board.BuildPiles.Count, "Wrong number of build piles.");
            }
        }
Exemplo n.º 11
0
 public void ShuffledDrawPileTest()
 {
     Board board = new Board();
     AssertDrawPileHistogramCorrect(board);
     AssertDrawPileIsRandom(board);
 }
Exemplo n.º 12
0
        public void PlayManyGamesTest()
        {
            IPlayer player1 = new SimplePlayer();
            IPlayer player2 = new DiscardOnlyPlayer();
            int NumDrawGames = 0;
            const int TotalGamesPlayed = 25;

            for (int gameNumber = 0; gameNumber < TotalGamesPlayed; gameNumber++)
            {
                Board board = new Board();
                board.AddPlayer(player1);
                board.AddPlayer(player2);

                board.PrepGame();
                board.PlayGame();

                if (board.Winner == null)
                    NumDrawGames++;
            }

            Debug.WriteLine(string.Format("{0} won {1}, {2} won {3}. {4} Drawn = Total {5}", player1, player1.GamesWon, player2, player2.GamesWon, NumDrawGames, TotalGamesPlayed));
            Assert.AreEqual(TotalGamesPlayed, NumDrawGames+player1.GamesWon + player2.GamesWon, "Total number of games won and drawn must match games played.");
        }
Exemplo n.º 13
0
 public void NewBoardTest()
 {
     Board board;
     board = new Board();
 }
Exemplo n.º 14
0
 private bool PlayFromDiscardPileToBuild(Board board)
 {
     foreach (Pile discardPile in board.GetDiscardPiles(this))
     {
         if (discardPile.HasCards)
         {
             foreach (Pile buildPile in board.BuildPiles)
             {
                 int playedValue;
                 if (buildPile.IsLegalPlay(discardPile.Top, buildPile, out playedValue))
                 {
                     discardPile.Top.PlayedValue = playedValue;
                     board.DoPlay(new Play(discardPile, buildPile));
                     return true;
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 15
0
 protected void Discard(Board board)
 {
     // Select first card in hand and discard to random pile
     board.DoPlay(new Play(board.GetHand(this), board.GetDiscardPiles(this)[Utility.Random().Next(Board.NumDiscardPiles)]));
 }
Exemplo n.º 16
0
 private bool PlayReserveCardToBuild(Board board)
 {
     Card topReserveCard = board.GetReservePile(this).Top;
     foreach (Pile buildPile in board.BuildPiles)
     {
         int playedValue;
         if (buildPile.IsLegalPlay(topReserveCard, buildPile, out playedValue))
         {
             board.GetReservePile(this).Top.PlayedValue = playedValue;
             board.DoPlay(new Play(board.GetReservePile(this), buildPile));
             return true;
         }
     }
     return false;
 }