Beispiel #1
0
 //成り決定用ガイド
 public static void CreateGuidePromote(int board_x, int board_y, PieceBase piece)
 {
     for (int i = 0; i < 2; i++)
     {
         GameObject clone = CreateGuideInstant(board_x, board_y, GuideKind.PROMOTE);
         Guide      comp  = clone.GetComponent <Guide> ();
         //スケール2倍
         clone.transform.localScale = new Vector3(2, 2, 1);
         //位置設定
         comp.SetPos(board_x, board_y);
         float x = BoardManager.GetPieceXFromBoardPosX(board_x);
         float y = BoardManager.GetPieceYFromBoardPosY(board_y);
         //選択用
         if (i == 0)
         {
             comp.promote = true;                         //成る
             x           -= define.chip_size_x_local * 2; //位置を左にずらす
         }
         else if (i == 1)
         {
             comp.promote = false;
             x           += define.chip_size_x_local * 2;      //位置を右にずらす
         }
         Vector3 pos = new Vector3(x, y, 0);
         clone.transform.localPosition = pos;            //ローカル位置を設定
         //画像設定
         Sprite sprite = PieceManager.GetInstance().GetSprite(piece.kind, piece.enemy_flag, comp.promote);
         UnityEngine.UI.Image image;
         image        = clone.gameObject.GetComponent <UnityEngine.UI.Image> ();
         image.sprite = sprite;
         image.color  = new Color(1.0f, 1.0f, 1.0f, 1.0f);        //透明度をリセットする
     }
 }
Beispiel #2
0
        /// <summary>
        /// clona el chessBoard e instancia nuevas HashSet con los mismos objetos piece del nuevo chessboard clonado
        /// </summary>
        public PieceBase[,] CloneChessBoardAndPiecesHashs()
        {
            var clonedChessBoard = new PieceBase[Constants.Size, Constants.Size];

            this.WhitePieces = new HashSet <PieceBase>();
            this.BlackPieces = new HashSet <PieceBase>();

            for (int i = Constants.ForStart; i < Constants.Size; i++)
            {
                for (int j = Constants.ForStart; j < Constants.Size; j++)
                {
                    if (this.ChessBoard[i, j] != null)
                    {
                        clonedChessBoard[i, j] = this.ChessBoard[i, j].Clone() as PieceBase;
                        if (clonedChessBoard[i, j].Color)
                        {
                            this.WhitePieces.Add(clonedChessBoard[i, j]);
                        }
                        else
                        {
                            this.BlackPieces.Add(clonedChessBoard[i, j]);
                        }
                    }
                }
            }

            return(clonedChessBoard);
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the chess console application!");
            Console.WriteLine();

            Console.WriteLine("The application will prompt for a chess piece, and a starting position to get ");
            Console.WriteLine("the possible moves of the chess piece.");
            Console.WriteLine();

            PieceBase chessPiece = Program._GetChessPieceFromUser();

            Console.WriteLine();

            Board chessBoard = new Board();

            Square startingSquare = Program._GetStartingSquareFromUser(chessBoard);

            startingSquare.piece = chessPiece;

            List <Square> availableMoves = chessBoard.GetAvailableMoves(startingSquare);

            string availableMovesOutput = Board.ConvertToCSV(availableMoves);

            Console.WriteLine();
            Console.WriteLine("Below are the available moves for the chosen chess piece and starting position.");
            Console.WriteLine(availableMovesOutput);
        }
        public void UndoMoveTest(int x1, int y1, int x2, int y2)
        {
            // Arrange
            var board      = new Board(true);
            var boardLogic = new BoardLogic(true);


            // Act
            boardLogic.LogicMove(4, 1, 4, 3, board);
            boardLogic.LogicMove(4, 6, 4, 4, board);
            var       expected  = board.CloneChessBoard();
            var       expected2 = board.CloneWhitePieces();
            var       expected3 = board.CloneBlackPieces();
            PieceBase auxPiece  = board.ChessBoard[x2, y2];

            if (boardLogic.LogicMove(x1, y1, x2, y2, board))
            {
                boardLogic.UndoMove(x1, y1, x2, y2, auxPiece, board);
            }


            var result  = board.ChessBoard;
            var result2 = board.WhitePieces;
            var result3 = board.BlackPieces;

            // Assert
            Assert.AreEqual(expected, result);
            Assert.AreEqual(expected2, result2);
            Assert.AreEqual(expected3, result3);
        }
Beispiel #5
0
        internal void ImplicitOperatorPiece(PieceBase piece, char symbol)
        {
            Piece expected = symbol;
            Piece actual   = piece;

            actual.ToString().Should().Be(expected.ToString());
        }
        private bool ComparePiece(PieceBase source, PieceBase target)
        {
            var areBothNull  = source == null && target == null;
            var areBothEqual = source?.Equals(target) ?? false;

            return(!areBothNull && !areBothEqual);
        }
Beispiel #7
0
        public static PieceBase CreateChessPiece(string chessPieceName)
        {
            PieceBase chessPiece = null;

            switch (chessPieceName.ToLower())
            {
            case "rook":
                chessPiece = new Rook();
                break;

            case "queen":
                chessPiece = new Queen();
                break;

            case "bishop":
                chessPiece = new Bishop();
                break;

            case "king":
                chessPiece = new King();
                break;
            }

            return(chessPiece);
        }
        public List <Move> AllPosiblePiecePlays(Board board, BoardLogic boardLogic, PieceBase piece, List <Move> AllMoves, double actualValue)
        {
            board = boardLogic.GoTo(board.TurnNumber, board);
            int x = piece.Position.PositionX;
            int y = piece.Position.PositionY;

            foreach (Position position in piece.ValidMoves(board))
            {
                if (boardLogic.FinallyMove(x, y, position.x1, position.y1, board, boardLogic, false))
                {
                    // solo lo guardo si no empeora la situacion (cuanto ams negativo mejor para la pc
                    double Eval = EvaluateBoard(board);

                    if (Eval <= actualValue)
                    {
                        //actualValue = EvaluateBoard(board);
                        AllMoves.Add(new Move {
                            x1 = x, y1 = y, x2 = position.x1, y2 = position.y1
                        });
                    }
                    // back to previous board
                    board = boardLogic.GoTo(board.TurnNumber - 1, board);
                    boardLogic.futureMoves.Clear();
                }
                //}
            }
            return(AllMoves);
        }
Beispiel #9
0
        public IActionResult GetAvailableMoves(string chessPieceName, [Required] string startingPosition)
        {
            PieceBase chessPiece = PieceFactory.CreateChessPiece(chessPieceName);

            if (chessPiece == null)
            {
                return(NotFound("Chess piece '" + chessPieceName + "' not found."));
            }

            Board chessBoard = new Board();

            Square startingSquare = chessBoard.GetSquare(startingPosition);

            if (startingSquare == null)
            {
                return(BadRequest("Starting position was not found on the chess board."));
            }

            startingSquare.piece = chessPiece;

            List <Square> availableMoves = chessBoard.GetAvailableMoves(startingSquare);

            string availableMovesOutput = Board.ConvertToCSV(availableMoves);
            string json = "{ 'availableChessMoves': '" + availableMovesOutput + "' }";

            return(Ok(json));
        }
Beispiel #10
0
 private PieceBase PlacePieceAtTopCenterOfBoard(PieceBase piece)
 {
     piece.PosX = this._width / 2;
     //default rotation offset
     piece.PosY = 1;
     return(piece);
 }
Beispiel #11
0
        internal void ToString(PieceBase piece, string position, char symbol)
        {
            var expected = $"{symbol}{position}";
            var actual   = piece.ToString();

            actual.Should().Be(expected);
        }
Beispiel #12
0
    //駒をアニメーション移動させる	事前にmove_target_posを決定しておくこと
    IEnumerator PieceMoveAnim(GameObject obj)
    {
        PieceBase comp = obj.GetComponent <PieceBase> ();

        if (comp.move_anim_flag == true)
        {
            yield break;            //すでにアニメーション移動中
        }
        while (true)
        {
            yield return(null);                                               //1フレーム待つ

            Vector3 MyPos    = obj.transform.localPosition;                   //現在地
            Vector3 DiffPos  = comp.move_targer_pos - MyPos;                  //位置差
            float   DiffDist = DiffPos.x * DiffPos.x + DiffPos.y * DiffPos.y; //三平方の定理から距離差を計算
            if (DiffDist < 1)
            {
                //終了
                obj.transform.localPosition = comp.move_targer_pos;
                comp.move_anim_flag         = false;
                if (comp.have_flag == false)
                {
                    //持ち駒でなければ持ち駒のアニメーション移動を行う
                    UpdateHavePiece(true);
                }
                break;
            }
            Vector3 pos = MyPos + DiffPos * 0.2f;            //差を20%ずつ詰めて滑らかに移動
            obj.transform.localPosition = pos;
        }
    }
Beispiel #13
0
        /// <summary>
        /// 放置棋子方法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        private bool PlaceAPiece(int x, int y)
        {
            bool      placeSuccess = false;
            PieceBase piece        = game.PlaceAPiece(x, y);

            if (piece == null)
            {
                return(placeSuccess);
            }

            tempPieces.Add(piece);

            MethodInvoker callMethod = new MethodInvoker(() =>
            {
                this.Controls.Add(piece);
                this.Controls.SetChildIndex(piece, 1);
            });

            this.BeginInvoke(callMethod);
            placeSuccess = true;
            string msg = game.CheckWinner();

            if (msg != string.Empty)
            {
                MessageBox.Show(msg);
                this.ResetGame();
                return(placeSuccess);
            }

            return(placeSuccess);
        }
 public bool IsPawn(PieceBase piece)
 {
     if (piece != null)
     {
         return(piece.GetType() == typeof(Pawn));
     }
     return(false);
 }
 // pone un flag en el peon para avisar que puede ser comido y anota el turno
 public void UpDateEnPassant(int y1, int y2, PieceBase piece, Board board)
 {
     if (piece.GetType() == typeof(Pawn) && Math.Abs(y2 - y1) == 2)
     {
         piece.EnPassantAllowed    = true;
         piece.EnPassantTurnNumber = board.TurnNumber;
     }
 }
        /// <summary>
        /// Guarda en una lista las jugadas, pero lo tengo que rever
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <param name="piece1"></param>
        /// <param name="player1"></param>
        public void WriteMove(int x1, int y1, int x2, int y2, PieceBase piece1, bool player1, Board board)
        {
            if (player1 == board.Turn) // para escribir el numero de la jugada
            {
                movement = movement.Insert(pointer, ".");
                movement = movement.Insert(pointer, board.FullMoveNumber.ToString());
                pointer += board.FullMoveNumber.ToString().Length + 1;
            }
            movement = movement.Insert(pointer++, " ");
            if (piece1.GetType() == typeof(Pawn) && IsPawnCapturing(x1, x2)) //peon comiendo
            {
                movement = movement.Insert(pointer++, columnLetters[x1]);
            }
            movement = movement.Insert(pointer++, " ");
            if (piece1.GetType() == typeof(Bishop))
            {
                movement = movement.Insert(pointer++, "B");
            }
            if (piece1.GetType() == typeof(Knight))
            {
                movement = movement.Insert(pointer++, "N");
            }
            if (piece1.GetType() == typeof(Queen))
            {
                movement = movement.Insert(pointer++, "Q");
            }
            if (piece1.GetType() == typeof(Rook))
            {
                movement = movement.Insert(pointer++, "R");
            }
            if (piece1.GetType() == typeof(King))
            {
                if (piece1.ShortCastling)
                {
                    movement = movement.Insert(pointer, "0-0");
                    pointer += 3;
                    return;
                }
                if (piece1.ShortCastling)
                {
                    movement = movement.Insert(pointer, "0-0-0");
                    pointer += 5;
                    return;
                }
                movement = movement.Insert(pointer++, "K");
            }
            movement = movement.Insert(pointer++, columnLetters[x2]);
            movement = movement.Insert(pointer++, (y2).ToString());

            if (player1 != board.Turn) // guarda la jugada completa
            {
                stackFullPlay.Push(movement);
                pointer = 0;
                //Console.WriteLine("--------- Esta fue la jugada: " + movement);
                movement = "";
            }
        }
        private PieceBase[,] GetExpectedChessBoard2()
        {
            var chessBoard = new PieceBase[Size, Size];

            chessBoard[0, 0] = new Pawn(true, 0, 0);
            chessBoard[Size - 1, Size - 1] = new Rook(false, Size - 1, Size - 1);

            return(chessBoard);
        }
Beispiel #18
0
        private bool FigureCollision(PieceBase piece)
        {
            bool isCollision             = false;
            var  currBoardPieceCollision = SnipPieceAreaFromBoardLines(piece);

            if (_containsCollidingBlocksSpecification.IsSatisfiedBy(currBoardPieceCollision))
            {
                isCollision = FigurePieceBlockCollsion(piece, currBoardPieceCollision);
            }
            return(isCollision);
        }
Beispiel #19
0
 public bool IsMovePossible(PieceBase piece, MoveDirection direction, int interval)
 {
     piece.Move(direction, interval);
     if (_pieceInsideBoardSpecification.IsSatisfiedBy(piece))
     {
         //Todo bear in mind interva
         bool isCollision = FigureCollision(piece);
         return(!isCollision);
     }
     return(false);
 }
Beispiel #20
0
 public void PutPiece(PieceBase removedPiece, int x2, int y2)
 {
     if (removedPiece != null)
     {
         ChessBoard[x2, y2] = removedPiece;
         ChessBoard[x2, y2].Position.PositionX = x2;
         ChessBoard[x2, y2].Position.PositionY = y2;
         var hashSet = removedPiece.Color ? WhitePieces : BlackPieces;
         hashSet.Add(removedPiece);
     }
 }
Beispiel #21
0
        private IList <List <bool> > SnipPieceAreaFromBoardLines(PieceBase piece)
        {
            var bounding = piece.BoundingArea;
            var result   = new List <List <bool> >();

            for (int y = 0; y < bounding.Height; y++)
            {
                var currBoardLine = BoardLines[bounding.Y + y];
                result.Add(currBoardLine.Positions.GetRange(bounding.X, bounding.Width));
            }
            return(result);
        }
Beispiel #22
0
        protected override bool IsValidRule(
            PieceBase piece,
            Position newPosition,
            Chessboard chessboard)
        {
            var(filesToMove, ranksToMove) = piece.GetDistanceTo(newPosition);

            if (filesToMove > 1)
            {
                return(false);
            }

            if (ranksToMove == 0)
            {
                return(false);
            }

            if (ranksToMove > 2)
            {
                return(false);
            }

            if (ranksToMove == 2 && !piece.IsFirstMove)
            {
                return(false);
            }

            if (chessboard.GetPiece(newPosition) && filesToMove != ranksToMove)
            {
                return(false);
            }

            if (filesToMove == ranksToMove && !chessboard.GetPiece(newPosition))
            {
                return(false);
            }

            var(horizontal, _) = piece.GetDirectionTo(newPosition);

            if (piece.Color == WhitePiece && horizontal.Equals(Bottom))
            {
                return(false);
            }

            if (piece.Color == BlackPiece && horizontal.Equals(Top))
            {
                return(false);
            }

            return(true);
        }
 /// <summary>
 /// Draw all pieces on the display
 /// </summary>
 private void FillDisplay()
 {
     for (int y = 0; y < 8; y++)
     {
         for (int x = 0; x < 8; x++)
         {
             PieceBase piece = game.GameArray[x, y];
             if (piece != null)
             {
                 SenseHat.Display.Screen[y, x] = piece.Color;
             }
         }
     }
 }
Beispiel #24
0
        protected override bool IsValidRule(
            PieceBase piece,
            Position newPosition,
            Chessboard chessboard)
        {
            var(filesToMove, ranksToMove) = piece.GetDistanceTo(newPosition);

            if (filesToMove != ranksToMove)
            {
                return(false);
            }

            return(true);
        }
Beispiel #25
0
    //王手判定
    bool CheckOhte()
    {
        Guide.AllGuideDelete();         //ガイドリセット
        //ターンプレイヤーの王の位置を取得
        int oh_x = -1;
        int oh_y = -1;

        foreach (var p in PiecesArrayID)
        {
            PieceBase piece = p.GetComponent <PieceBase>();
            if (piece.kind == PieceKind.OH)
            {
                if (GameManager.GetInstance().turn_player_id == piece.owner_ID)
                {
                    oh_x = piece.board_pos_x;
                    oh_y = piece.board_pos_y;
                    break;
                }
            }
        }
        Guide.ohte_check_flag = true;        //ガイド制作時に味方駒の扱いを変更する
        //非ターンプレイヤーの全駒の移動可能範囲の取得
        foreach (var p in PiecesArrayID)
        {
            PieceBase piece = p.GetComponent <PieceBase>();
            if (GameManager.GetInstance().turn_player_id == piece.owner_ID)
            {
                continue;                                                                      //非ターンプレイヤー
            }
            if (piece.have_flag == true)
            {
                continue;                                   //持ち駒
            }
            piece.move.CreateMoveGuide(piece.board_pos_x, piece.board_pos_y);
        }
        Guide.ohte_check_flag = false;        //リセット
        GameObject[] guides = GameObject.FindGameObjectsWithTag(define.GuideTag);
        foreach (var g in guides)
        {
            Guide guide = g.GetComponent <Guide>();
            if (guide.pos_x == oh_x && guide.pos_y == oh_y)
            {
                //王手
                Debug.Log("王手");
                StartCoroutine(DrawOhte());
                return(true);
            }
        }
        return(false);
    }
Beispiel #26
0
 private IEnumerator DestroyPiece(PieceBase piece)
 {
     SetPieceInfo(piece, -1, -1, false, true);
     if (piece.Player == PlayerType.Player1)
     {
         yield return(SetActivePieceUI(PiecesObject1[piece.PieceNum].gameObject, false));
     }
     else if (piece.Player == PlayerType.Player2)
     {
         yield return(SetActivePieceUI(PiecesObject2[piece.PieceNum].gameObject, false));
     }
     Sound.LoadSe("14", "14_stop");
     Sound.PlaySe("14");
 }
 /// <summary>
 /// true si la pieza en x,y tiene al menos un movimiento válido (es para saber si no es ahogado)
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <returns></returns>
 public bool CanPieceMoveOrDraw(int x, int y, PieceBase piece, Board board)
 {
     foreach (Position position in piece.ValidMoves(board))
     {
         int       i            = position.x1;
         int       j            = position.y1;
         PieceBase pieceRemoved = board.GetPiece(i, j);
         if (LogicMove(x, y, i, j, board)) //va true (osea testea) porque solo quiere saber si se puede mover
         {
             UndoMove(x, y, i, j, pieceRemoved, board);
             return(true);
         }
     }
     return(false);
 }
Beispiel #28
0
 public bool IsRotationPossible(PieceBase piece)
 {
     piece.Turn();
     if (_pieceInsideBoardSpecification.IsSatisfiedBy(piece))
     {
         //Todo bear in mind interva
         bool isCollision = FigureCollision(piece);
         return(!isCollision);
     }
     else
     {
         //out of the board
         return(false);
     }
 }
        public void UndoRookCastling(int x1, int y1, int x2, Board board)
        {
            PieceBase piece = board.GetPiece(x1, y1);

            if (piece != null && piece.GetType() == typeof(King) && Math.Abs(x2 - x1) == 2)
            {
                if (x1 > x2)
                {
                    board.Move(x2 + 1, y1, 0, y1);
                }
                if (x1 < x2)
                {
                    board.Move(x2 - 1, y1, 7, y1);
                }
            }
        }
Beispiel #30
0
        private void PieceToBoardBlocks(PieceBase piece)
        {
            var bounding = piece.BoundingArea;
            IList <List <bool> > result = piece.DecomposeCollisionMapInLinesOfBlocks();

            for (int y = 0; y < bounding.Height; y++)
            {
                for (int x = 0; x < bounding.Width; x++)
                {
                    if (bounding.Y + y > 0 && bounding.Y + y < _boardLines.Count)
                    {
                        _boardLines[bounding.Y + y].Positions[bounding.X + x] = result[y][x];
                    }
                }
            }
        }