Ejemplo n.º 1
0
    // Check validity of a move, return true if valid
    public bool MovePiece(PlayerPiece p, int targetX, int targetY)
    {
        bool moveMade = false;
        int  tmpX     = p.x;
        int  tmpY     = p.y;

        if (!pieceList[targetX][targetY].occupied)
        {
            if (playerTurn && p.isRed)
            {
                if (Mathf.Abs(targetX - p.x) == 1 && targetY - p.y == 1 &&
                    !forceJump)
                {
                    moveMade = true;
                }
                else if (Mathf.Abs(targetX - p.x) == 2 && targetY - p.y == 2)
                {
                    int jumpX = (targetX + p.x) / 2;
                    int jumpY = p.y + 1;
                    CheckersBoardPiece jumping = pieceList[jumpX][jumpY];
                    if (jumping.occupied && !jumping.occupant.isRed)
                    {
                        moveMade = true;
                        Destroy(jumping.occupant.gameObject);
                        RemovePiece(jumpX, jumpY);
                        jumpCount++;
                    }
                }
                if (p.isKing)
                {
                    if (Mathf.Abs(targetX - p.x) == 1 && targetY - p.y == -1 &&
                        !forceJump)
                    {
                        moveMade = true;
                    }
                    else if (Mathf.Abs(targetX - p.x) == 2 && targetY - p.y == 2)
                    {
                        int jumpX = (targetX + p.x) / 2;
                        int jumpY = p.y - 1;
                        CheckersBoardPiece jumping = pieceList[jumpX][jumpY];
                        if (jumping.occupied && !jumping.occupant.isRed)
                        {
                            moveMade = true;
                            Destroy(jumping.occupant.gameObject);
                            RemovePiece(jumpX, jumpY);
                            jumpCount++;
                        }
                    }
                }
            }
            else if (playerTurn && Player.instance.isCompetitor && !p.isRed)
            {
                if (Mathf.Abs(targetX - p.x) == 1 && targetY - p.y == -1 &&
                    !forceJump)
                {
                    moveMade = true;
                }
                else if (Mathf.Abs(targetX - p.x) == 2 && targetY - p.y == -2)
                {
                    int jumpX = (targetX + p.x) / 2;
                    int jumpY = p.y - 1;
                    CheckersBoardPiece jumping = pieceList[jumpX][jumpY];
                    if (jumping.occupied && jumping.occupant.isRed)
                    {
                        moveMade = true;
                        Destroy(jumping.occupant.gameObject);
                        RemovePiece(jumpX, jumpY);
                        jumpCount++;
                    }
                }
                if (p.isKing)
                {
                    if (Mathf.Abs(targetX - p.x) == 1 && targetY - p.y == 1 &&
                        !forceJump)
                    {
                        moveMade = true;
                    }
                    else if (Mathf.Abs(targetX - p.x) == 2 && targetY - p.y == 2)
                    {
                        int jumpX = (targetX + p.x) / 2;
                        int jumpY = p.y + 1;
                        CheckersBoardPiece jumping = pieceList[jumpX][jumpY];
                        if (jumping.occupied && jumping.occupant.isRed)
                        {
                            moveMade = true;
                            Destroy(jumping.occupant.gameObject);
                            RemovePiece(jumpX, jumpY);
                            jumpCount++;
                        }
                    }
                }
            }
            if (moveMade)
            {
                Debug.Log(p.x + ", " + p.y + " to " + targetX + ", " + targetY);
                p.transform.position = pieceList[targetX][targetY].MoveToLocation.position
                                       + Vector3.back;
                p.x            = targetX;
                p.y            = targetY;
                p.isBeingMoved = false;

                if (!p.isKing)
                {
                    if (p.isRed && targetY == 7)
                    {
                        p.Promote();
                    }
                    else if (targetY == 0)
                    {
                        p.Promote();
                    }
                }
                if (p.mustJump && !JumpAvailable(p))
                {
                    p.mustJump = false;
                }

                pieceList[targetX][targetY].occupant = p;
                pieceList[targetX][targetY].occupied = true;
                RemovePiece(tmpX, tmpY);

                if (!p.mustJump)
                {
                    EndTurn();
                }
            }
        }
        return(moveMade);
    }