public void HandleInput(ChessGameController gameController)
    {
        if (piecePromotionIsActive)
        {
            HandlePromotion(gameController);
            return;
        }

        ChessPlayer player = gameController.ActivePlayer;
        Board       board  = gameController.Board;

        if (board.TurnIsActive || (gameController.State != GameState.Normal && gameController.State != GameState.Check))
        {
            return;
        }

        ChessPlayer   playerCopy = new ChessPlayer(null, player.TeamColor);
        ChessPlayer   enemyCopy  = new ChessPlayer(null, gameController.GetOppositeToActivePlayer().TeamColor);
        ChessGridInfo gridCopy   = playerCopy.TeamColor == TeamColor.White ? ChessGridInfo.CopyGrid(board.ChessGrid, playerCopy, enemyCopy) :
                                   ChessGridInfo.CopyGrid(board.ChessGrid, enemyCopy, playerCopy);

        Move foundMove = FindBestMove(gridCopy, playerCopy, enemyCopy);
        Move bestMove  = new Move(board.ChessGrid.GetPieceOnSquareIndex(foundMove.MovingPiece), foundMove.TargetSquare,
                                  board.ChessGrid.GetPieceOnSquareIndex(foundMove.TargetPiece), foundMove.PromotionFlag, foundMove.CastlingFlag,
                                  board.ChessGrid.GetPieceOnSquareIndex(foundMove.CastlingPiece), foundMove.CastlingTargetSquare);

        board.OnSelectPiece(bestMove.MovingPiece);
        board.OnSelectedPieceMove(bestMove);
        gameController.OnEnteredNormalMode();
        board.OnDeselectActivePiece();
    }
 public void HandleInput(ChessGameController gameController)
 {
     if (gameController.ActivePlayer.TeamColor == TeamColor)
     {
         InputHandler.HandleInput(gameController);
     }
 }
Exemple #3
0
 public void Init(ChessGameController cgs, int depth)
 {
     this.cgs   = cgs;
     this.depth = depth;
     lastMove   = null;
     bestMove   = null;
     thinking   = false;
 }
Exemple #4
0
    public void ReadInput(Vector3 point, ChessGameController gameController)
    {
        if ((gameController.Board.TurnIsActive && !piecePromotionIsActive) ||
            (gameController.State != GameState.Normal && gameController.State != GameState.Check))
        {
            return;
        }

        recievedInput    = true;
        SelectedPosition = point + new Vector3(Board.BOARD_OFFSET, 0, Board.BOARD_OFFSET);
    }
    public override void RecieveInput(ChessGameController gameController)
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hitPoint;
            Ray        ray = mainCamera.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hitPoint))
            {
                foreach (var reader in inputReaders)
                {
                    reader.ReadInput(hitPoint.point, gameController);
                }
            }
        }
    }
Exemple #6
0
    public void HandleInput(ChessGameController gameController)
    {
        if (piecePromotionIsActive)
        {
            HandlePromotion(gameController);
            return;
        }

        Board board         = gameController.Board;
        bool  validPosition = board.CheckIfPositionIsValid(SelectedPosition);

        if (!validPosition)
        {
            board.OnDeselectActivePiece();
        }

        if (!recievedInput || !validPosition || board.TurnIsActive || (gameController.State != GameState.Normal && gameController.State != GameState.Check))
        {
            return;
        }

        recievedInput = false;
        Vector2Int chosenPosition = Board.CalculateSquareIndexFromBoardPosition(SelectedPosition);
        Piece      chosenPiece    = board.ChessGrid.GetPieceOnSquareIndex(chosenPosition);

        if (board.ChessGrid.SelectedPiece != null)
        {
            if (board.ChessGrid.SelectedPiece.CanMoveToSquare(chosenPosition))
            {
                board.OnSelectedPieceMove(board.ChessGrid.SelectedPiece.GetMoveFromSquareIndex(chosenPosition));
                gameController.OnEnteredNormalMode();
            }
            board.OnDeselectActivePiece();
        }
        else if (chosenPiece != null && chosenPiece.TeamColor == gameController.ActivePlayer.TeamColor)
        {
            board.OnSelectPiece(chosenPiece);
        }
    }
Exemple #7
0
    private void HandlePromotion(ChessGameController gameController)
    {
        if (!recievedInput)
        {
            return;
        }

        bool       foundPiece     = false;
        GameObject selectedPiece  = null;
        Vector2Int chosenPosition = Board.CalculateSquareIndexFromBoardPosition(SelectedPosition);

        foreach (var pieceObject in pieceToChoose)
        {
            if (Board.CalculateSquareIndexFromBoardPosition(pieceObject.transform.position) == chosenPosition)
            {
                foundPiece    = true;
                selectedPiece = pieceObject;
                break;
            }
        }

        recievedInput = false;

        if (foundPiece)
        {
            ChessGameController.CreatePiecePrefabAndInitialize(promotedPieceSquare, selectedPiece.GetComponent <PieceEntity>().GetPieceType(),
                                                               gameController.ActivePlayer.TeamColor, gameController.Board.ChessGrid, gameController.ActivePlayer);

            foreach (var pieceObject in pieceToChoose)
            {
                pieceObject.GetComponent <PieceEntity>().Die();
            }

            piecePromotionIsActive = false;
            completePromotion();
        }
    }
    public void MakeMove(Move move, ChessPlayer player, ChessPlayer enemy, bool saveSelectedPiece = false)
    {
        if (move.TargetPiece != null)
        {
            SetChessPieceOnGrid(null, move.TargetPiece.SquareIndex);
            enemy.RemovePiece(move.TargetPiece);
        }

        if (saveSelectedPiece)
        {
            SelectPiece(move.MovingPiece);
            SaveMovedPiece();
        }

        if (move.CastlingFlag)
        {
            Move castlingMove = new Move(move.CastlingPiece, (Vector2Int)move.CastlingTargetSquare, null);
            MovePieceToNewSquare(castlingMove);
            move.CastlingPiece.MovePiece(castlingMove, null);
        }

        if (move.PromotionFlag)
        {
            MovePieceToNewSquare(move);
            move.MovingPiece.MovePiece(move, null);
            Piece promotedPiece = ChessGameController.CreateChessPiece(move.TargetSquare, PieceType.Queen, this, player);
            promotedPiece.SetData(move.TargetSquare, move.TargetSquare, PieceType.Queen, move.MovingPiece.TeamColor, false, null, null);
            player.AddPiece(promotedPiece);
            player.RemovePiece(move.MovingPiece);
        }
        else
        {
            MovePieceToNewSquare(move);
            move.MovingPiece.MovePiece(move, null);
        }
    }
Exemple #9
0
 public void SetDependencies(ChessGameController chessController)
 {
     this.chessController = chessController;
 }
Exemple #10
0
 public abstract void RecieveInput(ChessGameController gameController);
Exemple #11
0
 public Human(ChessGameController cgs)
 {
     Assert.AreNotEqual(cgs, null);
     this.cgs = cgs;
     lastMove = null;
 }
Exemple #12
0
 public void SetGameController(ChessGameController chessGameController)
 {
     gameController = chessGameController;
 }
Exemple #13
0
 private void Awake()
 {
     gameController = GetComponent <ChessGameController>();
 }