コード例 #1
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_a_checker_cannot_jump_empty_square()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(5, 2),
                To = new Square(3, 4),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[5][2] = p1Checker;
            gameLogic.Board[0][7] = p2Checker;

            var validation = gameLogic.IsValidMove(move);
            Assert.That(validation.IsValid, Is.False, "P1 attempted to jump over an empty square");
        }
コード例 #2
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_a_player_must_jump_when_another_checker_is_able_to_jump()
        {
            // If a player attempts to simply move when another checker has a jump, then the move is not valid
            var p1Checker = new Checker(Player.PlayerOne);
            var p1Jumper = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(7, 2),
                To = new Square(6, 3),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[7][2] = p1Checker;
            gameLogic.Board[5][2] = p1Jumper;
            gameLogic.Board[4][3] = p2Checker;

            var isSuccessful = gameLogic.SendMove(move);
            Assert.That(isSuccessful, Is.False, "P1 cannot move a checker when another cheker has a jump available");
        }
コード例 #3
0
ファイル: Game.cs プロジェクト: slateface/checkers
        public MoveResult Move(string source, string destination)
        {
            if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(destination) || source.Length != 2 || destination.Length != 2)
            {
                return new MoveResult
                           {
                               Message = "Source and Destination must be two digit numbers",
                               BoardState = CurrentGame.GetBoardState(),
                           };
            }

            int fromX, fromY, toX, toY;
            if (!int.TryParse(source.Substring(0, 1), out fromX)
                || !int.TryParse(source.Substring(1, 1), out fromY)
                || !int.TryParse(destination.Substring(0, 1), out toX)
                || !int.TryParse(destination.Substring(1, 1), out toY))
            {
                return new MoveResult
                           {
                               Message = "Source and Destination must be two digit numbers",
                               BoardState = CurrentGame.GetBoardState(),
                           };
            }

            var currentGame = CurrentGame;

            var move = new Move
                           {
                               Player = Caller.IsPlayerOne ? Player.PlayerOne : Player.PlayerTwo,
                               From = new Square(fromX, fromY),
                               To = new Square(toX, toY),
                           };

            var moveResult = currentGame.MakeMove(move);
            moveResult.Message = moveResult.IsSuccessful ? string.Format("{0} played {1}-{2}", Caller.Name, source, destination) : "You cannot make that move.";

            Clients.playHappened(moveResult);
            return moveResult;
        }
コード例 #4
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_player_one_goes_first_but_not_second()
        {
            var gameLogic = new GameLogic();

            var move = new Move
                           {
                               Player = Player.PlayerOne,
                               From = new Square(5,2),
                               To = new Square(4,3),
                           };

            var result = gameLogic.SendMove(move);
            Assert.That(result, Is.True, "At the start of a game, player one must make the first move.");

            result = gameLogic.SendMove(move);
            Assert.That(result, Is.False, "At the start of a game, player one may not make the second move.");
        }
コード例 #5
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_player_two_goes_second()
        {
            var gameLogic = new GameLogic();

            var p1Move = new Move
                           {
                               Player = Player.PlayerOne,
                               From = new Square {X = 5, Y = 2},
                               To = new Square {X = 4, Y = 3},
                           };

            var result = gameLogic.SendMove(p1Move);
            Assert.That(result, Is.True, "At the start of a game, player one must make the first move.");

            var p2Move = new Move
                             {
                                 Player = Player.PlayerTwo,
                                 From = new Square(2, 5),
                                 To = new Square(3, 4),
                             };

            result = gameLogic.SendMove(p2Move);
            Assert.That(result, Is.True, "At the start of a game, player two must make the second move.");
        }
コード例 #6
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_p2_is_kinged_upon_reaching_top_row()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerTwo,
                From = new Square(6, 1),
                To = new Square(7, 0),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard(), IsPlayerOnesTurn = false,};
            gameLogic.Board[3][2] = p1Checker;
            gameLogic.Board[6][1] = p2Checker;

            var success = gameLogic.SendMove(move);
            Assert.That(success, Is.True, "Send move succeeded");
            Assert.That(p2Checker.IsKinged, Is.True, "YOU DIDN'T KING ME!");
        }
コード例 #7
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_p2_king_can_jump_p1_baseline_test()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo) { IsKinged = true, };

            var move = new Move
            {
                Player = Player.PlayerTwo,
                From = new Square(5, 2),
                To = new Square(3, 4),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[5][2] = p2Checker;
            gameLogic.Board[4][3] = p1Checker;

            var validation = gameLogic.IsValidMove(move);
            Assert.That(validation.IsValid, Is.True, "P2 is jumping P1");
        }
コード例 #8
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_checker_cannot_jump_2_squares_at_once()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(5, 2),
                To = new Square(2, 5),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[5][2] = p1Checker;
            gameLogic.Board[4][3] = p2Checker;

            var validation = gameLogic.IsValidMove(move);
            Assert.That(validation.IsValid, Is.False, "P1 is jumping too far");
        }
コード例 #9
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_p1_is_kinged_upon_reaching_bottom_row()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(1, 6),
                To = new Square(0, 7),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[1][6] = p1Checker;
            gameLogic.Board[3][2] = p2Checker;

            var success = gameLogic.SendMove(move);
            Assert.That(success, Is.True, "Send move succeeded");
            Assert.That(p1Checker.IsKinged, Is.True, "YOU DIDN'T KING ME!");
        }
コード例 #10
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_jump_removes_checker()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(5, 2),
                To = new Square(3, 4),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[5][2] = p1Checker;
            gameLogic.Board[4][3] = p2Checker;

            var isSuccessful = gameLogic.SendMove(move);
            Assert.That(isSuccessful, Is.True, "P1 is jumping P2");

            var oldP2Checker = gameLogic.Board[4][3];
            Assert.That(oldP2Checker, Is.Null, "Player Two's checker was not removed.");
        }
コード例 #11
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_when_a_double_jump_is_possible_that_the_players_turn_does_not_end()
        {
            // If a player attempts to simply move when another checker has a jump, then the move is not valid
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Jumpee1 = new Checker(Player.PlayerTwo);
            var p2Jumpee2 = new Checker(Player.PlayerTwo);
            var p2LastMove = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(5, 2),
                To = new Square(3, 4),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[5][2] = p1Checker;
            gameLogic.Board[4][3] = p2Jumpee1;
            gameLogic.Board[4][5] = p2Jumpee2;
            gameLogic.Board[0][1] = p2LastMove;

            var isSuccessful = gameLogic.SendMove(move);
            Assert.That(isSuccessful, Is.True, "P1 has a double jump opportunity");

            var boardState = gameLogic.GetBoardState();
            Assert.That(boardState.IsPlayerOnesTurn, Is.True, "It should still be P1's turn");

            //Make double jump
            move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(3, 4),
                To = new Square(5, 6),
            };
            isSuccessful = gameLogic.SendMove(move);
            Assert.That(isSuccessful, Is.True, "P1 should have been alowed to make a double jump");

            boardState = gameLogic.GetBoardState();
            Assert.That(boardState.IsPlayerOnesTurn, Is.False, "It should be P2's turn after the double jump");
        }
コード例 #12
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_it_is_player_ones_turn()
        {
            var gameLogic = new GameLogic();

            var player = Player.PlayerTwo;

            var move = new Move
                           {
                               Player = player,
                               From = new Square {X = 5, Y = 2},
                               To = new Square {X = 4, Y = 3},
                           };

            var result = gameLogic.SendMove(move);

            Assert.That(result, Is.False, "At the start of a game, player two may not make the first move.");
        }
コード例 #13
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_jumping_last_checker_results_in_game_over()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var move = new Move
                           {
                               Player = Player.PlayerOne,
                           };

            var gameLogic = new GameLogic {Board = GetEmptyBoard()};
            gameLogic.Board[2][5] = p1Checker;

            var isGameOver = gameLogic.IsGameOver(move);
            Assert.That(isGameOver, Is.True);
        }
コード例 #14
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public bool Assert_that_invalid_first_moves_return_false(int fromX, int fromY, int toX, int toY)
        {
            var gameLogic = new GameLogic();

            var move = new Move
                           {
                               Player = Player.PlayerOne,
                               From = new Square {X = fromX, Y = fromY},
                               To = new Square {X = toX, Y = toY},
                           };

            return gameLogic.SendMove(move);
        }
コード例 #15
0
ファイル: GameLogic.cs プロジェクト: slateface/checkers
 public MoveResult MakeMove(Move move)
 {
     return new MoveResult
                {
                    IsSuccessful = SendMove(move),
                    IsGameOver = IsGameOver(move),
                    CanJumpAgain = false,
                    Message = "I love lamp!",
                    BoardState = GetBoardState(),
                };
 }
コード例 #16
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_checker_is_moved_on_the_board()
        {
            var gameLogic = new GameLogic();

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(5, 2),
                To = new Square(4, 3),
            };

            var checkerPreMove = gameLogic.Board[5][2];
            Assume.That(checkerPreMove, Is.Not.Null, "Before the move, the from square is empty");

            var result = gameLogic.SendMove(move);
            Assert.That(result, Is.True, "Simple first move failed");

            var checkerPostMove = gameLogic.Board[4][3];
            Assert.That(checkerPostMove, Is.Not.Null, "After the move, the destination square remains empty");
            Assert.That(checkerPostMove, Is.SameAs(checkerPreMove), "After the move, the checker in the new square is not the same checker");
        }
コード例 #17
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_suicidal_move_yields_turn_to_other_player_to_make_jump()
        {
            // If a player attempts to simply move when a jump is possible, then the move is not valid
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(5, 2),
                To = new Square(6, 3),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[5][2] = p1Checker;
            gameLogic.Board[4][3] = p2Checker;

            var isSuccessful = gameLogic.SendMove(move);
            Assert.That(isSuccessful, Is.False, "P1 cannot avoid a jump when available");
        }
コード例 #18
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_a_player_must_jump_when_possible()
        {
            // If a player attempts to simply move when a jump is possible, then the move is not valid
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(5, 2),
                To = new Square(4, 3),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[5][2] = p1Checker;
            gameLogic.Board[3][4] = p2Checker;

            var isSuccessful = gameLogic.SendMove(move);
            Assert.That(isSuccessful, Is.True, "P1 is allowed to commit suicide");

            var boardState = gameLogic.GetBoardState();
            Assert.That(boardState.IsPlayerOnesTurn, Is.False, "Should be P2's turn after P1's suicide");
        }
コード例 #19
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_unkinged_p2_checker_cannot_jump_backwards()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerTwo,
                From = new Square(2, 5),
                To = new Square(3, 6),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[2][5] = p1Checker;
            gameLogic.Board[7][0] = p2Checker;

            var validation = gameLogic.IsValidMove(move);
            Assert.That(validation.IsValid, Is.False, "Cannot jump backwards!");
        }
コード例 #20
0
ファイル: GameLogic.cs プロジェクト: slateface/checkers
        public MoveValidation IsValidMove(Move move)
        {
            var result = new MoveValidation();

            if (!move.From.IsValidSquare() || !move.To.IsValidSquare()) return result;
            if (!PlayerHasCheckerOnSquare(move.Player, move.From)) return result;
            if (!IsSquareEmpty(move.To)) return result;

            // Check if an actual move was sent
            var deltaX = move.From.X - move.To.X;
            var deltaY = move.From.Y - move.To.Y;
            if (deltaX == 0 || deltaY == 0) return result;
            if (Math.Abs(deltaX) != Math.Abs(deltaY)) return result;

            if (Math.Abs(deltaX) == 2)
            {
                // Attempted a jump
                var jumpedSquare = new Square((move.From.X + move.To.X)/2, (move.From.Y + move.To.Y)/2);
                var jumpedChecker = Board[jumpedSquare.X][jumpedSquare.Y];
                if (jumpedChecker == null) return result;
                if (jumpedChecker.Player == move.Player) return result;

                result.JumpedChecker = jumpedChecker;
                result.JumpedSquare = jumpedSquare;
            }
            else if (Math.Abs(deltaX) != 1) return result;
            else if (PlayerHasJumpAvailable(move.Player)) return result;

            var checker = Board[move.From.X][move.From.Y];
            if (!checker.IsKinged)
            {
                if (move.Player == Player.PlayerOne && deltaY > 0) return result;
                if (move.Player == Player.PlayerTwo && deltaY < 0) return result;
            }

            result.IsValid = true;
            return result;
        }
コード例 #21
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_kinged_p1_checker_can_jump_backwards()
        {
            var p1Checker = new Checker(Player.PlayerOne) { IsKinged = true, };
            var p2Checker = new Checker(Player.PlayerTwo);

            var move = new Move
            {
                Player = Player.PlayerOne,
                From = new Square(3, 2),
                To = new Square(2, 1),
            };

            var gameLogic = new GameLogic { Board = GetEmptyBoard() };
            gameLogic.Board[3][2] = p1Checker;
            gameLogic.Board[0][7] = p2Checker;

            var validation = gameLogic.IsValidMove(move);
            Assert.That(validation.IsValid, Is.True, "Kings can jump backwards!");
        }
コード例 #22
0
ファイル: GameLogic.cs プロジェクト: slateface/checkers
        public bool SendMove(Move move)
        {
            //TODO change result from bool to complex state
            //TODO Make computer able to play
            if (!IsPlayersTurn(move.Player)) return false;

            var moveValidation = IsValidMove(move);
            if (!moveValidation.IsValid) return false;

            // Move the checker
            var checker = Board[move.From.X][move.From.Y];
            Board[move.To.X][move.To.Y] = checker;
            Board[move.From.X][move.From.Y] = null;

            if (moveValidation.JumpedSquare != null)
                Board[moveValidation.JumpedSquare.X][moveValidation.JumpedSquare.Y] = null;

            if (!checker.IsKinged)
            {
                if ((move.To.Y == 0 && move.Player == Player.PlayerTwo) ||
                    (move.To.Y == 7 && move.Player == Player.PlayerOne))
                    checker.IsKinged = true;
            }

            if (moveValidation.JumpedChecker == null || !PlayerHasJumpAvailable(move.Player))
            {
                IsPlayerOnesTurn = !IsPlayerOnesTurn;
            }

            return true;
        }
コード例 #23
0
ファイル: GameLogicTests.cs プロジェクト: slateface/checkers
        public void Assert_that_logjam_forces_game_over()
        {
            var p1Checker = new Checker(Player.PlayerOne);
            var p2Checker1 = new Checker(Player.PlayerTwo);
            var p2Checker2 = new Checker(Player.PlayerTwo);

            var move = new Move
                           {
                               Player = Player.PlayerTwo,
                           };

            var gameLogic = new GameLogic {Board = GetEmptyBoard()};
            gameLogic.Board[0][7] = p1Checker;
            gameLogic.Board[1][6] = p2Checker1;
            gameLogic.Board[2][5] = p2Checker2;

            var isGameOver = gameLogic.IsGameOver(move);
            Assert.That(isGameOver, Is.True);
        }
コード例 #24
0
ファイル: GameLogic.cs プロジェクト: slateface/checkers
        public bool IsGameOver(Move move)
        {
            var otherPlayerCheckers = Board.Select((row, x) => row.Select((checker, y) => new {checkerWithLocation = checker, Square = new Square(x, y)})
                                                                  .Where(o => o.checkerWithLocation != null && o.checkerWithLocation.Player != move.Player))
                                           .SelectMany(x => x)
                                           .ToArray();

            if (!otherPlayerCheckers.Any())
            {
                return true;
            }

            var hasAtLeastOneValidMove = otherPlayerCheckers.SelectMany(x => new[]
                                                                                 {
                                                                                     x.Square.MoveNW(),
                                                                                     x.Square.MoveNE(),
                                                                                     x.Square.MoveSW(),
                                                                                     x.Square.MoveSE(),
                                                                                     x.Square.JumpNW(),
                                                                                     x.Square.JumpNE(),
                                                                                     x.Square.JumpSW(),
                                                                                     x.Square.JumpSE(),
                                                                                 }
                                                                                 .Select(to => new Move {Player = x.checkerWithLocation.Player, From = x.Square, To = to}))
                                                            .Any(m => IsValidMove(m).IsValid);
            if (!hasAtLeastOneValidMove) return true;

            return false;
        }