// Test the protocol components related to the 'board state' message
            // type. Throws Exception if a 'board state' message is generated
            // which is inconsistent with the physical board state, or if a
            // board state defined by a 'board state' message is physically
            // inconsistent with the desired state.
            private static void TestBoardStateMessage()
            {
                // Create a new empty game board.
                TzaarBoard board = new TzaarBoard(true);

                // Create some game pieces of various types.
                TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK);
                TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK);
                TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK);

                // Add the pieces we created to the board at the target
                // position.
                board.Add(p1, 0, 0);
                board.Add(p2, 1, 0);
                board.Add(p3, 2, 0);

                TzaarMessage.BoardState message = new TzaarMessage.BoardState(board);

                // Check that the generated 'board state' message matches the
                // physical state of the board defined above.
                if (((string)message).CompareTo("BoardState{{BLACK,Tzaar},{},{},{},{},{BLACK,Tzarra},{},{},{},{},{},{BLACK,Tott},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}}") != 0)
                    throw new Exception();

                message = new TzaarMessage.BoardState("BoardState{{BLACK,Tzaar},{},{},{},{},{BLACK,Tzarra},{},{},{},{},{},{BLACK,Tott},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}}");

                // Check that a board state generated from a 'board state'
                // message is consistent with the desired state.
                Stack<TzaarPiece> S = ((TzaarBoard)message.Board).Query(0, 0);
                if (S == null || S.Count == 0 || S.Peek().GetType() != typeof(TzaarPiece.Tzaar))
                    throw new Exception();
            }
            // The goal is to test various representative valid and invalid
            // moves for correctness. Throws Exception if a validly specified
            // path is determined to be invalid, or if an invalidly specified
            // path is determined to be valid.
            private static void TestBoardMapPathing()
            {
                // Create a new empty game board.
                TzaarBoardMap boardMap = new TzaarBoardMap();
                TzaarBoard board = new TzaarBoard(true);

                // Create some game pieces of various types.
                TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK);
                TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK);
                TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK);

                // Operate on this specific position.
                int col = 8;
                int row = 0;

                // Add the pieces we created to the board at the target
                // position.
                board.Add(p1, col, row);
                board.Add(p2, col, row);
                board.Add(p3, col, row);

                // Check that a valid move is reported as valid.
                if (!boardMap.IsValidPath(board, 0, 0, 0, 1))
                    throw new Exception();

                // Check that an invalid move is reported as invalid.
                if (boardMap.IsValidPath(board, 4, 3, 4, 4))
                    throw new Exception();

                board.Add(p1, 2, 2);
                board.Add(p2, 2, 3);

                // Check that an invalid move is reported as invalid.
                if (!boardMap.IsValidPath(board, 2, 2, 2, 3))
                    throw new Exception();

                // Check that passing through another piece is reported as
                // invalid.
                board.Add(p1, 1, 1);
                if (boardMap.IsValidPath(board, 0, 0, 2, 2))
                    throw new Exception();

                // Remove the obstructing piece and verify that (0, 0) and
                // (2, 2) are now connected.
                board.Take(1, 1);
                if (!boardMap.IsValidPath(board, 0, 0, 2, 2))
                    throw new Exception();
            }
            // Check that stacking pieces works properly. Additionally, it
            // should be possible to add a piece to an invalid position on the
            // board without error. Throws Exception if the determined stack
            // size is not equal to the size of the stack placed at the
            // specified position.
            private static void TestAdd()
            {
                // Create a new empty game board.
                TzaarBoard board = new TzaarBoard(true);

                // Create some game pieces of various types.
                TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK);
                TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK);
                TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK);

                board.Add(p1, 0, 0);
                board.Add(p2, 0, 0);
                board.Add(p3, 0, 0);

                Stack<TzaarPiece> S = board.Query(0, 0);
                if (S.Count != 3)
                    throw new Exception();
                if (S.Peek() != p3)
                    throw new Exception();

                // Try to add to a spot that doesn't exist.
                board.Add((TzaarPiece)null, 8, 8);
            }
            // The goal is to test the board-querying functionality. Throws
            // Exception if the piece(s) returned from the specified position
            // do not match those placed at that position.
            private static void TestQuery()
            {
                // Create a new empty game board.
                TzaarBoard board = new TzaarBoard(true);

                // Create some game pieces of various types.
                TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK);
                TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK);
                TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK);

                // Add the pieces we created to the board at the target
                // position.
                board.Add(p1, 0, 0);
                board.Add(p2, 0, 0);
                board.Add(p3, 0, 0);

                // Check that there are 3 pieces on the target position.
                Stack<TzaarPiece> pieces = board.Query(0, 0);
                if (pieces.Count != 3)
                    throw new Exception();
            }
            // Test the functionality related to 'taking' a piece, or stack of
            // pieces, from a position on the game board. Additionally, it
            // should be possible to attempt to 'take' a piece from an invalid
            // position on the game board without error. Throws Exception if
            // 'taking' the pieces from the board does not result in the
            // specified position being empty.
            private static void TestTake()
            {
                // Create a new empty game board.
                TzaarBoard board = new TzaarBoard(true);

                // Create some game pieces of various types.
                TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK);
                TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK);
                TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK);

                // Add the pieces we created to the board at the target
                // position.
                board.Add(p1, 0, 0);
                board.Add(p2, 0, 0);
                board.Add(p3, 0, 0);

                // Check that there are 3 pieces on the target position.
                if (board.Query(0, 0).Count != 3)
                    throw new Exception();

                // Take the pieces.
                if (board.Take(0, 0).Count != 3)
                    throw new Exception();

                // Check that there are now 0 pieces on the target position.
                if (board.Query(0, 0).Count != 0)
                    throw new Exception();

                // Try to Take from a spot that doesn't exist.
                board.Take(-1, 8);
            }
            // Test components controlling the logic for one player to 'capture'
            // the piece of another player. Throws Exception if a valid
            // 'capture' results in an inconsistent game state, if an invalid
            // capture is allowed, or if a player is allowed to move the
            // opposite player's game pieces.
            private static void TestCapture()
            {
                // Test a simple, valid capture.
                TzaarBoard board = new TzaarBoard(true);
                TzaarPiece whitePiece = new TzaarPiece.Tzaar(TzaarColor.WHITE);
                board.Add(whitePiece, 2, 2);
                board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3);
                TzaarGameState state = new TzaarGameState(board);
                TzaarLogic game = new TzaarLogic(state);

                game.Move(2, 2, 2, 3);

                // The destination position should now contain the white piece
                // which captured the black piece.
                if (board.Query(2, 3).Peek() != whitePiece)
                    throw new Exception();

                // The origin position should no longer contain any pieces.
                if (board.Query(2, 2).Count() != 0)
                    throw new Exception();

                // Test an illegal capture.  The capture is illegal because the
                // stack in the target position is more powerful than the stack
                // at the source.
                board = new TzaarBoard(true);
                board.Add(new TzaarPiece.Tzaar(TzaarColor.WHITE), 2, 2);
                board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3);
                board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3);
                state = new TzaarGameState(board);
                game = new TzaarLogic(state);

                bool passedTest = false;
                try
                {
                    game.Move(2, 2, 2, 3);
                }
                catch
                {
                    passedTest = true;
                }
                if (!passedTest)
                {
                    throw new Exception();
                }

                // Now try controlling some of the opponent's pieces. The game
                // should not allow it!
                board = new TzaarBoard(true);
                board.Add(new TzaarPiece.Tzaar(TzaarColor.WHITE), 2, 2);
                board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3);
                board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3);
                state = new TzaarGameState(board);
                game = new TzaarLogic(state);

                passedTest = false;
                try
                {
                    game.Move(2, 3, 2, 2);
                }
                catch
                {
                    passedTest = true;
                }
                if (!passedTest)
                {
                    throw new Exception();
                }
            }