InitialPosition() public method

public InitialPosition ( ) : void
return void
Example #1
0
 public void OutputAbroadTest()
 {
     //a - arange
     var board = new Board();
     board.InitialPosition();
     //a - act
     board.DoMove("e8", "e9");
     //a - assert
 }
Example #2
0
 public void NotationTest2()
 {
     //a - arange
     var board = new Board();
     board.InitialPosition();
     //a - act
     board.DoMove("z0", "de");
     //a - assert
 }
Example #3
0
 public void SimpleMoveTest()
 {
     //a - arange
     var board = new Board();
     board.InitialPosition();
     //a - act
     board.DoMove("e2", "e4");
     //a - assert
     Assert.AreEqual(typeof(FigureNone), board["e2"].GetType());
     Assert.AreEqual(typeof(FigurePawn), board["e4"].GetType());
 }
Example #4
0
 public override void DoWork(IEnumerable<string> args)
 {
     if (Utils.CheckArgs(ArgsNeed, args.Count()))
     {
         if (Utils.IsInGame())
         {
             var gameboard = new Board();
             gameboard.InitialPosition();
             var commandMoveList = new CommandMoveList();
             gameboard.ApplyMoves(commandMoveList.GetList());
             gameboard.ShowBoardToConcole();
         }
     }
 }
Example #5
0
        public ActionResult Status()
        {
            var board = new Board();
            board.InitialPosition();
            string res;
            var request = new MoveListRequest {Game = int.Parse(Request.Params["gameID"])};
            var response = ServerProvider.MakeRequest<MoveListResponse>(request);
            var request2 = new GameStatRequest {gameID = int.Parse(Request.Params["gameID"])};
            var response2 = ServerProvider.MakeRequest<GameStatResponse>(request2);

            board.ApplyMoves(response.Moves);

            switch (response2.Act)
            {
                case Act.AbandonedByWhite:
                    res = ("Was abandoned by White");
                    break;

                case Act.AbandonedByBlack:
                    res = ("Was abandoned by Black");
                    break;

                case Act.Canceld:
                    res = ("Was canceld");
                    break;

                case Act.WaitingOpponent:
                    res = ("Waiting for 2nd player");
                    break;

                case Act.Pat:
                    res = ("Finished with pat");
                    break;

                case Act.WhiteWon:
                    res = ("Won by White");
                    break;

                case Act.BlackWon:
                    res = ("Won by Black");
                    break;

                case Act.BlackCheck:
                    res = ("Check Black");
                    break;

                case Act.WhiteCheck:
                    res = ("Check White");
                    break;

                case Act.InProgress:
                    res = ("Now in progress");
                    break;

                default:
                    res = ("Unexpected act");
                    break;
            }

            /*   return Json(new {DataBoard = board.ShowBoardToWeb(), DataMove = response.Moves.Select(move =>
            {
                dynamic m = new {From = move.From, To = move.To};
                if (move.Result != MoveResult.SilentMove)
                {
                    m.Result = move.Result;
                }
                return m;
            }),  DataStatus = response2.Act, DataTextStatus = res,
                DataWhitePlayer = response2.PlayerWhite, DataBlackPlayer = response2.PlayerBlack, DataTurn = response2.Turn,
                EatedWhites = response2.EatedWhites, EatedBlacks = response2.EatedBlacks}, JsonRequestBehavior.AllowGet);
            }*/
            return Json(new
            {
                DataBoard = board.ShowBoardToWeb(),
                DataMove = response.Moves.Select(move =>
                {
                    dynamic m = new {
                        From = move.From,
                        To = move.To,
                        DataMovedFigure = move.MovedFigure,
                        InWhom = move.InWhom,
                        Result1 = (move.Result & MoveResult.Taking) == MoveResult.Taking ? "×" : "—",
                        Result2 = (move.Result & MoveResult.Check) == MoveResult.Check ? "+" : ((move.Result & MoveResult.Mate) == MoveResult.Mate ? "#" : ((move.Result & MoveResult.Pat) == MoveResult.Pat) ? "=" : "" ),
                        Result3 = (move.Result & MoveResult.LongCastling) == MoveResult.LongCastling ? "0—0—0" : ((move.Result & MoveResult.ShortCastling) == MoveResult.ShortCastling ? "0—0" : "y")
                    };

                    return m;
                }),
                DataStatus = response2.Act,
                DataTextStatus = res,
                DataWhitePlayer = response2.PlayerWhite,
                DataBlackPlayer = response2.PlayerBlack,
                DataTurn = response2.Turn,
                EatedWhites = response2.EatedWhites,
                EatedBlacks = response2.EatedBlacks
            }, JsonRequestBehavior.AllowGet);
        }
Example #6
0
        public AttackMap(List<Move> moves, Board forceBoard = null, bool isRecursive = false)
        {
            if (forceBoard == null)
            {
                SourceBoard = new Board();
                SourceBoard.InitialPosition();
                SourceBoard.ApplyMoves(moves);
            }
            else
            {
                SourceBoard = forceBoard;
            }

            for (int i = 0; i < Board.BoardSize; i++)
            {
                for (int j = 0; j < Board.BoardSize; j++)
                {
                    Attackers[i, j] = new List<Figure>();
                }
            }
            for (char currentCellX = 'a'; currentCellX <= 'h'; currentCellX++)
            {
                for (int currentCellY = 1; currentCellY <= Board.BoardSize; currentCellY++)
                {
                    Figure currentFigure = SourceBoard[currentCellX.ToString(CultureInfo.InvariantCulture) + currentCellY];
                    _figuresPosition[currentFigure] = currentCellX.ToString(CultureInfo.InvariantCulture) + currentCellY;
                    if (currentFigure.GetType() == typeof(FigureNone))
                    {
                        continue;
                    }

                    if (currentFigure.GetType() == typeof(FigurePawn))
                    {
                        if (currentCellY + 1 <= Board.BoardSize && currentCellY - 1 >= 1)
                        {
                            int cellY;
                            if (currentFigure.Side == Side.WHITE)
                            {
                                cellY = currentCellY + 1;
                                Figure figure = SourceBoard[currentCellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                /*if (figure.Side != currentFigure.Side)
                                    Attackers[currentCellX - 'a', cellY - 1].Add(currentFigure);*/
                                if (figure.GetType() == typeof(FigureNone))
                                {
                                    Attackers[currentCellX - 'a', cellY - 1].Add(currentFigure);
                                    if (currentCellY == 2) // первый или нет
                                    {
                                        cellY += 1;
                                        figure =
                                            SourceBoard[currentCellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                        if (figure.GetType() == typeof (FigureNone))
                                            Attackers[currentCellX - 'a', cellY - 1].Add(currentFigure);
                                    }
                                }

                                cellY = currentCellY + 1;
                                char cellX = (char)(currentCellX + 1);
                                if (cellX <= 'h')
                                {
                                    figure = SourceBoard[cellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                    if (figure.GetType() != typeof(FigureNone) && figure.Side != currentFigure.Side)
                                        Attackers[cellX - 'a', cellY - 1].Add(currentFigure);
                                }
                                cellX = (char)(currentCellX - 1);
                                if (cellX >= 'a')
                                {
                                    figure = SourceBoard[cellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                    if (figure.GetType() != typeof(FigureNone))
                                    {
                                        if (figure.Side != currentFigure.Side)
                                            Attackers[cellX - 'a', cellY - 1].Add(currentFigure);
                                    }
                                }
                            }

                            if (currentFigure.Side == Side.BLACK)
                            {
                                cellY = currentCellY - 1;
                                Figure figure = SourceBoard[currentCellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                if (figure.GetType() == typeof(FigureNone))
                                {
                                    Attackers[currentCellX - 'a', cellY - 1].Add(currentFigure);
                                    if (currentCellY == Board.BoardSize - 1) // первый или нет
                                    {
                                        cellY -= 1;
                                        figure = SourceBoard[currentCellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                        if (figure.GetType() == typeof(FigureNone))
                                            Attackers[currentCellX - 'a', cellY - 1].Add(currentFigure);
                                    }
                                }

                                cellY = currentCellY - 1;
                                char cellX = (char)(currentCellX + 1);
                                if (cellX <= 'h')
                                {
                                    figure = SourceBoard[cellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                    if (figure.Side != currentFigure.Side && figure.GetType() != typeof(FigureNone))
                                        Attackers[cellX - 'a', cellY - 1].Add(currentFigure);
                                }
                                cellX = (char)(currentCellX - 1);
                                if (cellX >= 'a')
                                {
                                    figure = SourceBoard[cellX.ToString(CultureInfo.InvariantCulture) + cellY];
                                    if (figure.Side != currentFigure.Side && figure.GetType() != typeof(FigureNone))
                                        Attackers[cellX - 'a', cellY - 1].Add(currentFigure);
                                }
                            }

                        }
                        PassedPawn(moves, SourceBoard, currentFigure);
                        continue;
                    }
                    if (currentFigure.GetType() == typeof(FigureRook))
                    {
                        North(SourceBoard, currentCellX, currentCellY, currentFigure);
                        South(SourceBoard, currentCellX, currentCellY, currentFigure);
                        East(SourceBoard, currentCellX, currentCellY, currentFigure);
                        West(SourceBoard, currentCellX, currentCellY, currentFigure);
                        continue;
                    }

                    if (currentFigure.GetType() == typeof(FigureQueen))
                    {
                        North(SourceBoard, currentCellX, currentCellY, currentFigure);
                        South(SourceBoard, currentCellX, currentCellY, currentFigure);
                        East(SourceBoard, currentCellX, currentCellY, currentFigure);
                        West(SourceBoard, currentCellX, currentCellY, currentFigure);
                        NorthEast(SourceBoard, currentCellX, currentCellY, currentFigure);
                        SouthEast(SourceBoard, currentCellX, currentCellY, currentFigure);
                        NorthWest(SourceBoard, currentCellX, currentCellY, currentFigure);
                        SouthWest(SourceBoard, currentCellX, currentCellY, currentFigure);
                        continue;
                    }

                    if (currentFigure.GetType() == typeof(FigureBishop))
                    {
                        NorthEast(SourceBoard, currentCellX, currentCellY, currentFigure);
                        SouthEast(SourceBoard, currentCellX, currentCellY, currentFigure);
                        NorthWest(SourceBoard, currentCellX, currentCellY, currentFigure);
                        SouthWest(SourceBoard, currentCellX, currentCellY, currentFigure);
                        continue;
                    }

                    if (currentFigure.GetType() == typeof(FigureKnight))
                    {
                        var x = (char)(currentCellX + 2);
                        int y;
                        if (x <= 'h')
                        {
                            y = currentCellY + 1;
                            if (y <= Board.BoardSize)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                            y = currentCellY - 1;
                            if (y >= 1)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                        }
                        x = (char)(currentCellX + 1);
                        if (x <= 'h')
                        {
                            y = currentCellY + 2;
                            if (y <= Board.BoardSize)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                            y = currentCellY - 2;
                            if (y >= 1)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                        }
                        x = (char)(currentCellX - 2);
                        if (x >= 'a')
                        {
                            y = currentCellY + 1;
                            if (y <= Board.BoardSize)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                            y = currentCellY - 1;
                            if (y >= 1)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                        }
                        x = (char)(currentCellX - 1);
                        if (x >= 'a')
                        {
                            y = currentCellY + 2;
                            if (y <= Board.BoardSize)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                            y = currentCellY - 2;
                            if (y >= 1)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                        }
                        continue;
                    }
                    if (currentFigure.GetType() == typeof(FigureKing))
                    {
                        var x = (char)(currentCellX + 1);
                        int y;

                        if (x <= 'h')
                        {
                            y = currentCellY + 1;
                            if (y <= Board.BoardSize)
                                KingKnightStep(SourceBoard, currentFigure, x, y);

                            y = currentCellY - 1;
                            if (y >= 1)
                                KingKnightStep(SourceBoard, currentFigure, x, y);

                            KingKnightStep(SourceBoard, currentFigure, x, currentCellY);
                        }

                        x = (char)(currentCellX - 1);
                        if (x >= 'a')
                        {
                            y = currentCellY + 1;
                            if (y <= Board.BoardSize)
                                KingKnightStep(SourceBoard, currentFigure, x, y);
                            y = currentCellY - 1;
                            if (y > 0)
                                KingKnightStep(SourceBoard, currentFigure, x, y);

                            KingKnightStep(SourceBoard, currentFigure, x, currentCellY);
                        }

                        y = currentCellY + 1;
                        if (y <= Board.BoardSize)
                            KingKnightStep(SourceBoard, currentFigure, currentCellX, y);

                        y = currentCellY - 1;
                        if (y >= 1)
                            KingKnightStep(SourceBoard, currentFigure, currentCellX, y);

                        //if (SourceBoard["a1"].GetType() == typeof(FigureRook) && SourceBoard["a1"].side == Side.WHITE)
                        char kingX = SourceBoard.ReturnPosition(currentFigure).Item1;
                        int kingY = SourceBoard.ReturnPosition(currentFigure).Item2;

                        Side side = currentFigure.Side == Side.WHITE ? Side.BLACK : Side.WHITE;

                        if (kingX == 'e' && (kingY == 1 || kingY == 8))
                        {
                            if (IsColorFigureAttack(this[kingX + kingY.ToString(CultureInfo.InvariantCulture)], side))
                                if (SourceBoard["a" + kingY].GetType() == typeof(FigureRook))
                                    Castling(moves, SourceBoard, currentFigure, SourceBoard["a" + kingY]);
                                if (SourceBoard["h" + kingY].GetType() == typeof(FigureRook))
                                    Castling(moves, SourceBoard, currentFigure, SourceBoard["h" + kingY]);
                        }
                        if (currentFigure.Side == Side.WHITE)
                        {
                            _whiteKing = (currentCellX.ToString(CultureInfo.InvariantCulture) + currentCellY);
                        }

                        if (currentFigure.Side == Side.BLACK)
                        {
                            _blackKing = (currentCellX.ToString(CultureInfo.InvariantCulture) + currentCellY);
                        }
                    }
                }
            }
            if (!isRecursive)
            {

                foreach (var move in AllPossibleMoves)
                {
                    Board newBoard = SourceBoard.Clone();
                    var additionalMoves = new List<Move> { move };
                    newBoard.ApplyMoves(additionalMoves);
                    var newAttackMap = new AttackMap(moves.Concat(additionalMoves).ToList(), newBoard, true);
                    var figure = SourceBoard[move.From];
                    if ((figure.Side == Side.WHITE && newAttackMap.IsCheckWhite)
                      || (figure.Side == Side.BLACK && newAttackMap.IsCheckBlack))
                    {
                        Attackers[Board.GetCoords(move.To).Item1, Board.GetCoords(move.To).Item2].Remove(figure);
                    }
                }
            }
        }