Exemplo n.º 1
0
    /// <summary>
    /// Makes the move after confirming that it is legal
    /// </summary>
    public void TryMakeMove(string algebraicMove)
    {
        if (isWhite == BoardGround.IsWhiteToPlay())
        {
            legalMoves = moveGenerator.GetMoves(false, false);
            for (int i = 0; i < legalMoves.Count; i++)
            {
                int moveFromIndex = legalMoves.GetMove(i) & 127;
                int moveToIndex   = (legalMoves.GetMove(i) >> 7) & 127;

                int moveFromX = BoardGround.Convert128to64(moveFromIndex) % 8;
                int moveFromY = BoardGround.Convert128to64(moveFromIndex) / 8;
                int moveToX   = BoardGround.Convert128to64(moveToIndex) % 8;
                int moveToY   = BoardGround.Convert128to64(moveToIndex) / 8;


                string fromAlgebraic = DefinitionsGround.fileNames[moveFromX].ToString() + DefinitionsGround.rankNames[moveFromY].ToString();
                string toAlgebraic   = DefinitionsGround.fileNames[moveToX].ToString() + DefinitionsGround.rankNames[moveToY].ToString();


                string moveCoords = fromAlgebraic + toAlgebraic;
                if (moveCoords == algebraicMove)                   // move confirmed as legal
                //Debug.Log(algebraicMove);
                {
                    MakeMove(legalMoves.GetMove(i));
                    break;
                }
            }
        }
    }
Exemplo n.º 2
0
    void Update()
    {
        if (!active)
        { // input is active if one or more players have been assigned to it (or game is over)
            return;
        }

        Vector3 endPointerPosition = GvrPointerInputModule.CurrentRaycastResult.worldPosition;

        // Vector2 mousePosition = (Vector2)viewCamera.ScreenToWorldPoint (Input.mousePosition);

        // Pick up piece
        // If not holding any Piece and clicked anywhere
        if (!holdingPiece && Input.GetMouseButtonDown(0))
        // if (/*Input.GetKeyDown(KeyCode.Mouse0)*/ Input.GetMouseButtonDown(0) && !holdingPiece)
        {
            ChessUIGround.instance.ResetHighlights();
            // isPlayerMove just checks if any HumanPlayerGround has his chance
            // If so then we will highlight the regions
            bool isPlayerMove = false;
            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].isMyMove)
                {
                    isPlayerMove = true;
                    break;
                }
            }

            holdingPiece = TryGetPieceUIAtPoint(endPointerPosition, out pieceHeld);
            // This line improves efficiency
            if (!isThereAPiece)
            {
                holdingPiece = false;
            }
            // Debug.Log("holding piece: " + holdingPiece + " isPlayerMove: " + isP);
            if (holdingPiece && isPlayerMove)
            {
                // highlight legal moves for held piece
                HeapGround legalMoveHeap = HumanPlayerGround.legalMoves;
                for (int i = 0; i < legalMoveHeap.Count; i++)
                {
                    HighlightSquare(legalMoveHeap.GetMove(i), pieceHeld.algebraicCoordinate);
                }
            }
        }
        // Let go of piece
        // If holding anything and clicked somewhere
        else if (Input.GetMouseButtonDown(0) && holdingPiece)
        // else if (/*Input.GetKeyUp(KeyCode.Mouse0)*/Input.GetMouseButtonUp(0) && holdingPiece)
        {
            PieceUIGround dropSquare;
            ChessUIGround.instance.ResetHighlights();
            if (TryGetPieceUIAtPoint(endPointerPosition, out dropSquare))
            {
                string algebraicMove = pieceHeld.algebraicCoordinate + dropSquare.algebraicCoordinate;
                for (int i = 0; i < players.Count; i++)
                {
                    players[i].TryMakeMove(algebraicMove);
                }
            }
            pieceHeld.Release();
            holdingPiece = false;
        }
        // Drag piece
        // else if (/*Input.GetKey(KeyCode.Mouse0)*/ Input.GetMouseButton(0) && holdingPiece)
        // {
        //    pieceHeld.Move(endPointerPosition);
        // }
    }