Exemple #1
0
        public void IsMovePossible_PossibleMove_ReturnTrue(string rookPositionString, string moveString)
        {
            var rook = (Rook)_board[rookPositionString];
            var move = MoveParser.ParseMove(moveString, _board);

            Assert.IsTrue(rook.IsMovePossible(move));
        }
Exemple #2
0
        public void IsMovePossible_PossibleMove_ReturnTrue(string kingPositionString, string moveString)
        {
            var king = (King)_board[kingPositionString];
            var move = MoveParser.ParseMove(moveString, _board);

            Assert.IsTrue(king.IsMovePossible(move));
        }
        /// <summary>
        /// Mapping method from entity model to view model.
        /// </summary>
        /// <param name="match">Object that will be mapped.</param>
        /// <returns>New mapped object.</returns>
        public static MatchViewModel MapToMatchViewModel(Match match)
        {
            var matchViewModel = SetDataFromDescription(match);

            matchViewModel.Moves = MoveParser.StringBuildMatchMoves(match.Moves);
            return(matchViewModel);
        }
Exemple #4
0
        public void IsMovePossible_PossibleMove_ReturnTrue(string bishopPositionString, string moveString)
        {
            var bishop = (Bishop)_board[bishopPositionString];
            var move   = MoveParser.ParseMove(moveString, _board);

            Assert.IsTrue(bishop.IsMovePossible(move));
        }
Exemple #5
0
        public void King_AllowsCastling_WhenSpacesOpen(string input, string kingSquare, string expectedCastlingSquare)
        {
            var king           = MoveParser.ParseSquare(kingSquare);
            var expectedResult = MoveParser.ParseSquare(expectedCastlingSquare);
            IEnumerable <Square> validMoves;

            var game  = new Game();
            var moves = input.Split(',');

            foreach (var move in moves.SkipLast(2))
            {
                game.Move(move);

                validMoves = game.GetValidMoves(king.File, king.Rank);
                Assert.That(validMoves, Does.Not.Contain(expectedResult), $"Unexpected castling square after move {move}");
            }

            foreach (var move in moves.TakeLast(2))
            {
                game.Move(move);
            }

            validMoves = game.GetValidMoves(king.File, king.Rank);
            Assert.That(validMoves, Does.Contain(expectedResult));
        }
        private void btnMoves_Click(object sender, EventArgs e)
        {
            var moves = MoveParser.Parse(txtAlg.Text);

            txtCubeMoves.Text = string.Join("\r\n", moves.Select(x => x.ToString()).ToArray());

            var calculator = new MoveCalculator();

            foreach (Move mv in moves)
            {
                calculator.AddCubeMove(mv);
            }

            txtMachineMoves.Text = string.Join("\r\n", calculator.MachineMoves.Select(x => x.ToString()).ToArray());

            txtMotorMoves.Text = string.Join("\r\n", calculator.MachineMoves.MotorMoves.Select(x => x.ToString()).ToArray());

            txtNBMotorMoves.Text = "1/4: " + calculator.MachineMoves.MotorMoves.Sum(x => x.QuarterNumber).ToString() + "  moves: " + calculator.MachineMoves.MotorMoves.Count;

            textLongMove.Text = string.Join("  ", calculator.MachineMoves.MotorMoves.Select(x => x.EquivalentCubeMove()));


            var dir = GetUriViewer(/*txtAlg.Text+ "%0A%2F%2F" +*/ textLongMove.Text, txtMoves.Text);

            viewer.FrameLoadEnd += ResetCubeOnLoad; // reset du cube au chargement

            viewer.Load(dir);
        }
        public void Deserialize_ShouldSetBoardState_WhenValidStringGiven()
        {
            const string  FenString = "pQ4N1/3k3R/1r4n1/KbBbBppP/8/8/q7/7n";
            FenSerializer ser       = new FenSerializer();
            BoardState    actual;
            BoardState    expected = BoardState.Empty
                                     .SetPiece(MoveParser.ParseSquare("a8"), SquareContents.Black | SquareContents.Pawn)
                                     .SetPiece(MoveParser.ParseSquare("b8"), SquareContents.White | SquareContents.Queen)
                                     .SetPiece(MoveParser.ParseSquare("g8"), SquareContents.White | SquareContents.Knight)

                                     .SetPiece(MoveParser.ParseSquare("d7"), SquareContents.Black | SquareContents.King)
                                     .SetPiece(MoveParser.ParseSquare("h7"), SquareContents.White | SquareContents.Rook)

                                     .SetPiece(MoveParser.ParseSquare("b6"), SquareContents.Black | SquareContents.King)
                                     .SetPiece(MoveParser.ParseSquare("g6"), SquareContents.Black | SquareContents.Knight)

                                     .SetPiece(MoveParser.ParseSquare("a5"), SquareContents.White | SquareContents.King)
                                     .SetPiece(MoveParser.ParseSquare("b5"), SquareContents.Black | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("c5"), SquareContents.White | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("d5"), SquareContents.Black | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("e5"), SquareContents.White | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("f5"), SquareContents.Black | SquareContents.Pawn)
                                     .SetPiece(MoveParser.ParseSquare("g5"), SquareContents.Black | SquareContents.Pawn)
                                     .SetPiece(MoveParser.ParseSquare("h5"), SquareContents.White | SquareContents.Pawn)

                                     .SetPiece(MoveParser.ParseSquare("a2"), SquareContents.Black | SquareContents.Queen)
                                     .SetPiece(MoveParser.ParseSquare("h1"), SquareContents.Black | SquareContents.Knight);

            // Act
            actual = ser.Deserialize(FenString);

            Assert.That(expected.AllPieces, Is.EqualTo(actual.AllPieces));
            Assert.That(expected.WhitePieces, Is.EqualTo(actual.WhitePieces));
            Assert.That(expected.BlackPieces, Is.EqualTo(actual.BlackPieces));
        }
Exemple #8
0
        public void IsMovePossible_PossibleMove_ReturnTrue(string knightPositionString, string moveString)
        {
            var knight = (Knight)_board[knightPositionString];
            var move   = MoveParser.ParseMove(moveString, _board);

            Assert.IsTrue(knight.IsMovePossible(move));
        }
Exemple #9
0
        public void IsMovePossible_PossibleMove_ReturnTrue(string pawnPositionsString, string moveString)
        {
            var pawn = (Pawn)_board[pawnPositionsString];
            var move = MoveParser.ParseMove(moveString, _board);

            Assert.IsTrue(pawn.IsMovePossible(move));
        }
        public ActionResult ViewMatch(int id)
        {
            var match = matchRepository.FindMatchById(id);

            ViewBag.Moves = MoveParser.StringBuildProblemMoves(match.Moves);
            return(View(match));
        }
Exemple #11
0
        public static void ParseMove_CorrectNotation_ReturnExpectedEnPassant(string moveString,
                                                                             string expectedFromPositionString,
                                                                             string expectedToPositionString)
        {
            var expectedMove = new EnPassant((Pawn)_board[expectedFromPositionString], new Position(expectedToPositionString), _board);
            var move         = MoveParser.ParseMove(moveString, _board);

            Assert.That(move.Equals(expectedMove));
        }
Exemple #12
0
        public JsonResult GetSolution(int id)
        {
            var position = positionRepository.FindPositionById(id);

            if (position == null)
            {
                return(Json(new { result = false, message = Resources.DefaultErrorMessage }));
            }
            return(Json(new { moves = MoveParser.StringBuildProblemMoves(position.Solution) }));
        }
 /// <summary>
 /// Mapping method from view model to entity model.
 /// </summary>
 /// <param name="match">Object that will be mapped.</param>
 /// <returns>New mapped object.</returns>
 public static Match MapToMatch(MatchViewModel match)
 {
     return(new Match
     {
         ID = match.ID,
         DateCreated = match.DatePlayed,
         Description = MatchService.GetDescription(match.WhiteName, match.BlackName, match.Result, match.Place, match.DatePlayed.ToShortDateString()),
         Result = match.Result,
         Moves = MoveParser.ParseMatchMoves(match.Moves, "")
     });
 }
Exemple #14
0
        public void GeneratesExpectedSquares_MatchesCount(string sq, SquareContents piece, int expectedSquares)
        {
            var   square    = MoveParser.ParseSquare(sq);
            ulong bitSquare = BitTranslator.TranslateToBit(square.File, square.Rank);
            var   board     = BoardState.Empty.SetPiece(bitSquare, piece);
            var   game      = new Game(board);

            var validMoves = game.GetValidMoves(square.File, square.Rank);

            Assert.That(validMoves.Count, Is.EqualTo(expectedSquares));
        }
Exemple #15
0
        public void GeneratesExpectedSquares_OmitsUnexpected(string sq, SquareContents piece, string expectedSquares)
        {
            var expected = expectedSquares.Split(',').Select(MoveParser.ParseSquare).ToArray();

            var   square    = MoveParser.ParseSquare(sq);
            ulong bitSquare = BitTranslator.TranslateToBit(square.File, square.Rank);
            var   board     = BoardState.Empty.SetPiece(bitSquare, piece);

            var game = new Game(board);

            var validMoves = game.GetValidMoves(square.File, square.Rank).ToArray();

            Assert.That(validMoves, Does.Not.Contain(expected[0]));
        }
Exemple #16
0
        public void TryParseMove_ParsesPromotion(string input, string expectedStart, string expectedEnd, PieceColor color, SquareContents piece)
        {
            var endSq      = MoveParser.ParseSquare(expectedEnd);
            var startSq    = MoveParser.ParseSquare(expectedStart);
            var pieceColor = color == PieceColor.White ? SquareContents.White : SquareContents.Black;
            var board      = BoardState.Empty.SetPiece(startSq, SquareContents.Pawn | pieceColor);

            var game           = new Game(board, color);
            var result         = game.Move(input);
            var expectedSquare = game.GetSquareContents(endSq.File, endSq.Rank);

            Assert.That(result, Is.EqualTo(ErrorCondition.None));
            Assert.That(expectedSquare, Is.EqualTo(piece));
        }
Exemple #17
0
        public void PlacesPieceOnIntendedSquare(string focusSquare, SquareContents expectedContents, params string[] moveStrings)
        {
            var game = new Game();

            foreach (var moveString in moveStrings)
            {
                var result = game.Move(moveString);
                Assert.That(result, Is.EqualTo(ErrorCondition.None), "Failed for move: " + moveString);
            }

            var targetSquare   = MoveParser.ParseSquare(focusSquare);
            var actualContents = game.GetSquareContents(targetSquare.File, targetSquare.Rank);

            Assert.That(actualContents, Is.EqualTo(expectedContents));
        }
 /// <summary>
 /// Mapping method from entity model to view model.
 /// </summary>
 /// <param name="position">Object that will be mapped.</param>
 /// <returns>New mapped object.</returns>
 public static PositionViewModel MapToPositionViewModel(Position position)
 {
     return(new PositionViewModel
     {
         ID = position.ID,
         WhiteIsPlaying = position.WhiteIsPlaying,
         Category = position.Category,
         Difficulty = position.Difficulty,
         Name = position.Name,
         DateCreated = position.DateCreated,
         Description = position.Description,
         PiecePositions = position.PiecePositions,
         Solution = MoveParser.StringBuildProblemMoves(position.Solution)
     });
 }
Exemple #19
0
        public void GetValidMoves_DetectsWhenOnEdge(string position, string targetSq, string expectedSq)
        {
            var fen   = new FenSerializer();
            var board = fen.Deserialize(position);

            var target     = MoveParser.ParseSquare(targetSq);
            var targetMask = BitTranslator.TranslateToBit(target.File, target.Rank);

            var validMovesMask = MoveGenerator.GetBishopMovements(targetMask, board);
            var validMoves     = BitTranslator.TranslateToSquares(validMovesMask);

            var expected = MoveParser.ParseSquare(expectedSq);

            Assert.That(validMoves, Does.Contain(expected));
        }
Exemple #20
0
        private static IMove ReadMove(Board board)
        {
            var moveString = Console.ReadLine();

            try
            {
                return(MoveParser.ParseMove(moveString, board));
            }
            catch (Exception ex)
            {
                if (ex is FormatException || ex is ArgumentException)
                {
                    return(null);
                }
                throw;
            }
        }
Exemple #21
0
        public void Move_UpdatesPiecesWhenCastling(string input, string kingSquare, string rookSquare, SquareContents color)
        {
            var king = MoveParser.ParseSquare(kingSquare);
            var rook = MoveParser.ParseSquare(rookSquare);

            var game = new Game();

            foreach (var move in input.Split(','))
            {
                game.Move(move);
            }

            var expectedKing = game.GetSquareContents(king.File, king.Rank);
            var expectedRook = game.GetSquareContents(rook.File, rook.Rank);

            Assert.That(expectedKing, Is.EqualTo(SquareContents.King | color));
            Assert.That(expectedRook, Is.EqualTo(SquareContents.Rook | color));
        }
        public void Match(int whiteId, int blackId, string result, string pieces, string moves, bool isRated)
        {
            var white = userRepository.FindUserById(whiteId);
            var black = userRepository.FindUserById(blackId);
            var match = new Match
            {
                White       = white,
                Black       = black,
                Comments    = new List <Comment>(),
                Moves       = MoveParser.ParseMatchMoves(moves, pieces),
                DateCreated = DateTime.Now,
                Result      = result,
                IsPlayed    = true,
                IsRated     = isRated,
                Description = MatchService.GetDescription(white.UserName, black.UserName, result, "ChessMasters.com", DateTime.Now.ToShortDateString())
            };

            matchRepository.SaveMatch(match);
            CalculateRating(result, white, black);
        }
Exemple #23
0
        public static void ParseMove_CorrectNotation_ReturnExpectedPromotion(string moveString,
                                                                             string expectedFromPositionString,
                                                                             string expectedToPositionString,
                                                                             char expectedPromotedPawn)
        {
            var pawn = _board[expectedFromPositionString];

            Assert.That(pawn is Pawn);

            var expectedMove = new Promotion((Pawn)_board[expectedFromPositionString], PieceParser.ParsePiece(expectedPromotedPawn, pawn.Color), new Position(expectedToPositionString), _board);
            var move         = MoveParser.ParseMove(moveString, _board);

            Assert.That(move is Promotion);
            var promotion = (Promotion)move;

            Assert.That(expectedMove.To == promotion.To);
            Assert.That(expectedMove.Piece == promotion.Piece);
            Assert.That(expectedMove.Board == promotion.Board);
            Assert.That(expectedMove.PromotedPawn.GetType() == promotion.PromotedPawn.GetType());
        }
Exemple #24
0
        public void King_DisallowsCastling_WhenInvalidState(string input, string kingSquare, string expectedCastlingSquare)
        {
            var king           = MoveParser.ParseSquare(kingSquare);
            var expectedResult = MoveParser.ParseSquare(expectedCastlingSquare);

            var game  = new Game();
            var moves = input.Split(',');

            foreach (var move in moves.SkipLast(1))
            {
                game.Move(move);
            }

            game.Move(moves.Last());
            Assert.That(game.AttackState, Is.Not.EqualTo(AttackState.Check));

            var validMoves = game.GetValidMoves(king.File, king.Rank);

            Assert.That(validMoves, Does.Not.Contain(expectedResult));
        }
Exemple #25
0
        public void MakeMove(string input)
        {
            IPawn toPromote = board.PromotionRequired() as IPawn;

            if (toPromote == null)
            {
                MoveParser parser = new MoveParser();
                IMove      move   = parser.Parse(input);
                IPiece     toMove = board.GetFigureAt(move.Start);
                if (toMove != null && board.CurrentTurn() == toMove.color)
                {
                    IBoard newBoard = board.Move(move);
                    if (newBoard != null)
                    {
                        board = newBoard;
                    }
                }
            }
            else
            {
                Promote(input);
            }
        }
Exemple #26
0
        private void BuildReplayStates(IEnumerable <string> allMoves)
        {
            allReplayStates = new List <BoardState>();


            var topPlayer    = new Player(PlayerType.TopPlayer);
            var bottomPlayer = new Player(PlayerType.BottomPlayer);

            var boardState = BoardStateTransition.CreateInitialBoadState(topPlayer, bottomPlayer);

            allReplayStates.Add(boardState);

            var playerAtMove = bottomPlayer;

            foreach (var move in allMoves)
            {
                var nextMove = MoveParser.GetMove(move);

                boardState = boardState.ApplyMove(nextMove);
                allReplayStates.Add(boardState);

                playerAtMove = playerAtMove == topPlayer ? bottomPlayer : topPlayer;
            }
        }
        public void ParseCoordinatesCorrectly(string input, ushort row, ushort column)
        {
            Tuple <ushort, ushort> calculatedCoordinates = MoveParser.GetCoordinates(input);

            calculatedCoordinates.Should().Be(new Tuple <ushort, ushort>(row, column));
        }
        public void ParseMoveCorrectly(string input, Moves expectedMove)
        {
            Moves parsedMove = MoveParser.ExtractMove(input);

            parsedMove.Should().Be(expectedMove);
        }
Exemple #29
0
 public static bool IsValidMove(string move)
 {
     return(MoveParser.GetMove(move) != null);
 }
Exemple #30
0
 public void ParseMove_IncorrectMove_ThrowArgumentException(string moveString)
 {
     Assert.Throws <ArgumentException>(() => MoveParser.ParseMove(moveString, _board));
 }