public BoardDto New(int numberOfRows, int numberOfColumns)
 {
     var board = new Board(numberOfRows, numberOfColumns);
     var sessionState = GetSessionState();
     sessionState[BoardSessionStateKey] = board;
     return _boardMapper.Map(board);
 }
Example #2
0
        public void Board_YellowTurn_ThrowsExceptionIfOutOfRange()
        {
            var sut = new Board(5, 5);

            Assert.DoesNotThrow(() => sut.YellowTurn(2));

            Assert.Throws<ArgumentOutOfRangeException>(() => sut.YellowTurn(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => sut.YellowTurn(5));
        }
        public void RowEnumerator_InitialState_InvalidUntilMoveNextCalled()
        {
            var board = new Board(5, 5);
            var sut = new RowEnumerator(board, 2);

            Assert.Throws<IndexOutOfRangeException>(() => { var location = sut.Current; });

            sut.MoveNext();

            Assert.DoesNotThrow(() => { var location = sut.Current; });
        }
        public void DiagonalUpEnumerator_MoveNext_ReturnsFalseAtEndOfDiagonal()
        {
            var board = new Board(5, 5);
            board[2, 0].Occupied = Occupied.Red;
            board[1, 1].Occupied = Occupied.Yellow;
            board[0, 2].Occupied = Occupied.Red;

            var sut = new DiagonalUpEnumerator(board, 1, 1);

            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(false);
        }
        public void RowEnumerator_MoveNext_ReturnsTheExpectedLocation()
        {
            var board = new Board(5, 5);
            board[1, 0].Occupied = Occupied.Red;
            board[1, 1].Occupied = Occupied.Yellow;
            board[1, 2].Occupied = Occupied.Red;

            var sut = new RowEnumerator(board, 1);

            sut.MoveNext();
            sut.Current.Occupied.ShouldBe(Occupied.Red);
            sut.MoveNext();
            sut.Current.Occupied.ShouldBe(Occupied.Yellow);
            sut.MoveNext();
            sut.Current.Occupied.ShouldBe(Occupied.Red);
        }
        public void RowEnumerator_MoveNext_ReturnsFalseAtEndOfRow()
        {
            var board = new Board(5, 5);
            board[1, 0].Occupied = Occupied.Red;
            board[1, 1].Occupied = Occupied.Yellow;
            board[1, 2].Occupied = Occupied.Red;

            var sut = new RowEnumerator(board, 1);

            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(true);
            sut.MoveNext().ShouldBe(false);
        }
        public void Scenario_One_Yellow_Wins_Horizontal()
        {
            var board = new Board(5, 5);
            var turn = board.RedTurn(0);
            turn.Location.ShouldBe(new Location(4, 0) { Occupied = Occupied.Red });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.YellowTurn(0);
            turn.Location.ShouldBe(new Location(3, 0) { Occupied = Occupied.Yellow });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.RedTurn(4);
            turn.Location.ShouldBe(new Location(4, 4) { Occupied = Occupied.Red });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.YellowTurn(3);
            turn.Location.ShouldBe(new Location(4, 3) { Occupied = Occupied.Yellow });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.RedTurn(2);
            turn.Location.ShouldBe(new Location(4, 2) { Occupied = Occupied.Red });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.YellowTurn(3);
            turn.Location.ShouldBe(new Location(3, 3) { Occupied = Occupied.Yellow });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.RedTurn(0);
            turn.Location.ShouldBe(new Location(2, 0) { Occupied = Occupied.Red });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.YellowTurn(2);
            turn.Location.ShouldBe(new Location(3, 2) { Occupied = Occupied.Yellow });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.RedTurn(0);
            turn.Location.ShouldBe(new Location(1, 0) { Occupied = Occupied.Red });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.YellowTurn(0);
            turn.Location.ShouldBe(new Location(0, 0) { Occupied = Occupied.Yellow });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.RedTurn(1);
            turn.Location.ShouldBe(new Location(4, 1) { Occupied = Occupied.Red });
            turn.IsWinningTurn.ShouldBe(false);
            turn = board.YellowTurn(1);
            turn.Location.ShouldBe(new Location(3, 1) { Occupied = Occupied.Yellow });
            turn.IsWinningTurn.ShouldBe(true);

            Console.WriteLine($"Yellow wins in {board.TurnCount} turns.");
        }
        public void Scenario_Four_Draw()
        {
            // Uncomment to run the generator.
            //DrawGenerator();

            var board = new Board(5, 5);
            var turn = board.YellowTurn(3);
            turn = board.RedTurn(0);
            turn = board.YellowTurn(3);
            turn = board.RedTurn(4);
            turn = board.YellowTurn(3);
            turn = board.RedTurn(1);
            turn = board.YellowTurn(2);
            turn = board.RedTurn(3);
            turn = board.YellowTurn(4);
            turn = board.RedTurn(2);
            turn = board.YellowTurn(0);
            turn = board.RedTurn(2);
            turn = board.YellowTurn(2);
            turn = board.RedTurn(3);
            turn = board.YellowTurn(0);
            turn = board.RedTurn(0);
            turn = board.YellowTurn(0);
            turn = board.RedTurn(4);
            turn = board.YellowTurn(1);
            turn = board.RedTurn(1);
            turn = board.YellowTurn(1);
            turn = board.RedTurn(1);
            turn = board.YellowTurn(4);
            turn = board.RedTurn(4);
            turn.IsDraw.ShouldBe(false);
            board.IsDraw.ShouldBe(false);

            turn = board.YellowTurn(2);

            turn.IsDraw.ShouldBe(true);
            board.IsDraw.ShouldBe(true);
        }
        /// <summary>
        /// Call from a test to iterate games until a Draw is detected, and show the Turn sequence that was used.
        /// </summary>
        private void DrawGenerator()
        {
            int iteration = 0;
            bool isDraw = false;
            byte[] seedGenerator = new Byte[4];

            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            while (!isDraw)
            {
                rng.GetBytes(seedGenerator);
                int seed = BitConverter.ToInt32(seedGenerator, 0);
                var rand = new Random(seed);

                Console.WriteLine($"Iteration {++iteration}");
                Console.Out.Flush();

                var board = new Board(5, 5);
                for (;;)
                {
                    Turn yelloWTurn = board.YellowTurn(rand.Next(5));
                    for (; !yelloWTurn.IsWinningTurn && yelloWTurn == Turn.InvalidTurn && board.TurnCount < 25; yelloWTurn = board.YellowTurn(rand.Next(5)))
                        ;

                    Console.WriteLine($"{yelloWTurn.Location}");

                    Turn redTurn = board.RedTurn(rand.Next(5));
                    for (; !redTurn.IsWinningTurn && redTurn == Turn.InvalidTurn && board.TurnCount < 25; redTurn = board.RedTurn(rand.Next(5)))
                        ;

                    Console.WriteLine($"{redTurn.Location}");

                    if (yelloWTurn.IsWinningTurn)
                    {
                        Console.WriteLine($"YELLOW won in {board.TurnCount} turns.");
                        break;
                    }
                    if (redTurn.IsWinningTurn)
                    {
                        Console.WriteLine($"RED won in {board.TurnCount} turns.");
                        break;
                    }
                    if (board.TurnCount == 25)
                    {
                        isDraw = true;
                        Console.WriteLine("DRAW!");
                        break;
                    }
                }
            }
        }
Example #10
0
        public void Scenario_Six_Invalid_Move()
        {
            var sut = new Board(5, 5);

            Assert.DoesNotThrow(() => sut.RedTurn(2));

            Assert.Throws<ArgumentOutOfRangeException>(() => sut.RedTurn(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => sut.YellowTurn(5));

            Assert.DoesNotThrow(() => sut.YellowTurn(2));

            Assert.Throws<ArgumentOutOfRangeException>(() => sut.YellowTurn(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => sut.YellowTurn(5));
        }