Exemplo n.º 1
0
    private void SelectChessman(int x, int y)
    {
        if (Chessmans [x, y] == null)
        {
            return;
        }
        if (Chessmans [x, y].isWhite != isWhiteTurn)
        {
            return;
        }
        bool hasAtleastOneMove = false;

        allowedMoves = Chessmans [x, y].PossibleMove();
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (allowedMoves [i, j])
                {
                    hasAtleastOneMove = true;
                }
            }
        }
        if (!hasAtleastOneMove)
        {
            return;
        }

        selectedChessman        = Chessmans [x, y];
        previousMat             = selectedChessman.GetComponent <MeshRenderer> ().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        BoardHighlights.Instance.HighlightALLowedMoves(allowedMoves);
    }
Exemplo n.º 2
0
    // This function selects the chessman at the given position at its chance
    private void SelectChessman(int x, int z)
    {
        if (Chessmans[x, z] != null && Chessmans[x, z].isWhite == isWhiteChance)
        {
            bool hasAtLeastOneMove = false;
            allowedMoves = Chessmans[x, z].PossibleMove();
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (allowedMoves[i, j])
                    {
                        hasAtLeastOneMove = true;
                    }
                }
            }

            if (!hasAtLeastOneMove)
            {
                // dont even select this thing and simply return which is the reason why we wrote this thing in this function
                return;
            }

            selectedChessman = Chessmans[x, z];

            prevMat = selectedChessman.GetComponent <MeshRenderer>().material;
            selectedMat.mainTexture = prevMat.mainTexture;
            selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;

            BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
        }
        return;
    }
    private void SelectChessman(int x, int y)
    {
        // Se seleziono una casa vuota
        if (Chessmans[x, y] == null)
        {
            return;
        }

        // Se non è un pezzo bianco
        if (Chessmans[x, y].isWhite != isWhiteTurn)
        {
            return;
        }

        // Calcolo mosse possibili
        allowedMoves = Chessmans[x, y].PossibleMove();

        // Aggiornamento pezzo selezionato
        selectedChessman = Chessmans[x, y];

        // Cambio materiale
        previousMat             = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;

        // Illumina le case
        BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
    }
Exemplo n.º 4
0
    private void SelectChessman(int x, int y)
    {
        if (Chessmans[x, y] == null)
        {
            UIManager.Instance.CascadeSidebar();
            return;
        }


        //Get all possible moves and instantiate objects to indicate them
        allowedMoves = Chessmans[x, y].PossibleMove();

        selectedChessman = Chessmans[x, y];


        //Store this chessman's material and replace it with selectMat
        previousMat = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedChessman.GetComponent <MeshRenderer>().material = selectMat;
        BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);

        UIManager.Instance.CascadeSidebar(); // Refresh sidebar to destroy previously highlighted piece
        UIManager.Instance.PreviewPiece(selectedChessman._name);
        UIManager.Instance.DrawSidebar();
        UIManager.Instance.SetMorale(selectedChessman.emotion);
    }
Exemplo n.º 5
0
 private void Deselect()
 {
     if (selectedChessman != null)
     {
         BoardHighlight.Instance.HideHighlights();
         selectedChessman.GetComponent <Outline>().enabled = false;
         selectedChessman = null;
     }
 }
Exemplo n.º 6
0
    private void Update()
    {
        //check where mouse is
        UpdateSelection();
        DrawChessboard();

        if (Input.GetMouseButtonDown(0))
        {
            if (selectionX >= 0 && selectionY >= 0)
            {
                //if no piece selected
                if (selectedChessman == null)
                {
                    //select the chessman clicked on
                    SelectChessman(selectionX, selectionY);
                }
                //if there is a piece where you click
                else if (selectedChessman != null && Chessmans[selectionX, selectionY] != null)
                {
                    //if the piece is the same color, select it
                    if (selectedChessman.isWhite == Chessmans[selectionX, selectionY].isWhite)
                    {
                        DeselectChessman();
                        SelectChessman(selectionX, selectionY);
                    }
                    //if not, move the piece there
                    else
                    {
                        MoveChessman(selectionX, selectionY);
                    }
                }
                //if piece is selected and there is no piece where clicking on
                else
                {
                    //move the chessman to space clicked on
                    MoveChessman(selectionX, selectionY);
                }
            }
            //if clicked somewhere off the board
            else
            {
                BoardHighlights.Instance.HideHighlights();
                if (selectedChessman != null)
                {
                    selectedChessman.GetComponent <MeshRenderer>().material = previousMat;
                    selectedChessman = null;
                }
            }
        }
    }
Exemplo n.º 7
0
    private void SelectChessman(int x, int y)
    {
        // Выбрано пустое поле
        if (Chessmans[x, y] == null)
        {
            return;
        }

        // Выбрана фигура цвет которой не ходит
        if (Chessmans[x, y].isWhite != isWhiteTurn)
        {
            return;
        }

        // Имеет ли фигура хотя бы 1 доступный ход
        bool hasAtLeastOneMove = false;

        // Получить список ходов выбранной фигуры
        allowedMoves = Chessmans[x, y].PossibleMove();
        // Проверить, есть ли хотя бы 1 доступный ход
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8 && !hasAtLeastOneMove; j++)
            {
                if (allowedMoves[i, j])
                {
                    hasAtLeastOneMove = true;
                }
            }
        }
        // Фигура не имеет доступных ходов
        if (!hasAtLeastOneMove)
        {
            return;
        }
        // Выбрать данную фигуру
        selectedChessman = Chessmans[x, y];
        // Запомнить стандартный материал
        previousMat = selectedChessman.GetComponent <MeshRenderer>().material;
        // Заменить стандартный материал на материал выдления
        if (selectedMat != null)
        {
            selectedMat.mainTexture = previousMat.mainTexture;
            selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        }

        // Показать достепные ходы
        BoardHighLights.Instance.HighlightAllowedMoves(allowedMoves);
    }
Exemplo n.º 8
0
    public void SelectChessman(int x, int y)
    {
        _connect.text.text = "SC " + x.ToString() + y.ToString();
        if (isEnded || wait)
        {
            return;
        }
        if (Chessmans[x, y] == null)
        {
            return;
        }
        if (Chessmans[x, y].isWhite != isWhiteTurn)
        {
            return;
        }
        if (puzzleMode && _connect.timeWhite < 0f)
        {
            return;
        }

        bool hasAtleastOneMove = false;

        allowedMoves = Chessmans[x, y].PossibleMove();
        piece_x      = x;
        piece_y      = y;
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (allowedMoves[i, j] && (puzzleMode || isUserWhite != isWhiteTurn || _connect.Possible(i, j)))
                {
                    hasAtleastOneMove = true;
                    break;
                }
            }
        }

        if (!hasAtleastOneMove && !replay)
        {
            return;
        }

        selectedChessman        = Chessmans[x, y];
        previousMat             = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
    }
Exemplo n.º 9
0
    private void MoveChessman(int x, int y)
    {
        if (allowedMoves[x, y])
        {
            Chessman c = Chessmans [x, y];

            if (c != null && c.isWhite != isWhiteTurn)
            {
                if (c.GetType() == typeof(King))
                {
                    EndGame();
                    return;
                }

                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }

            // Выбор шахматной фигуры

            Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
            selectedChessman.transform.position = GetTileCenter(x, y);
            selectedChessman.SetPosition(x, y);
            Chessmans [x, y] = selectedChessman;
            isWhiteTurn      = !isWhiteTurn;
        }


        selectedChessman.GetComponent <MeshRenderer> ().material = previousMat;
        BoardHighLights.Instance.Hidehighlights();
        selectedChessman = null;
    }
Exemplo n.º 10
0
    private void MoveChessman(int x, int y)
    {
        if (allowedMoves[x, y])
        {
            Chessman c = Chessmans[x, y];
            if (c != null && c.isWhite != isWhiteTurn)
            {
                //Capture a piece

                //if it is the     king
                if (c.GetType() == typeof(King))
                {
                    //End the game
                    EndGame();
                    isFinished = true;
                    return;
                }

                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }


            Chessmans[selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
            selectedChessman.transform.position = GetCenter(x, y);
            selectedChessman.SetPosition(x, y);
            Chessmans[x, y] = selectedChessman;
            isWhiteTurn     = !isWhiteTurn;
        }
        selectedChessman.GetComponent <MeshRenderer>().material = previousMat;
        //unselect
        selectedChessman = null;
        BoardHighlights.Instance.Hidehighlights();
    }
Exemplo n.º 11
0
 private void CastlingRookMove(int x)
 {
     if (selectedChessman.isWhite && (selectedChessman.CurrentX + 2) == x)
     {
         selectedChessman = Chessmen[7, 0];
         ChangePiecePosition(5, 0);
         selectedChessman = Chessmen[whiteKing.x, whiteKing.y];
         selectedChessman.GetComponent <Outline>().enabled = false;
     }
     if (selectedChessman.isWhite && (selectedChessman.CurrentX - 2) == x)
     {
         selectedChessman = Chessmen[0, 0];
         ChangePiecePosition(3, 0);
         selectedChessman = Chessmen[whiteKing.x, whiteKing.y];
         selectedChessman.GetComponent <Outline>().enabled = false;
     }
     if (!selectedChessman.isWhite && (selectedChessman.CurrentX + 2) == x)
     {
         selectedChessman = Chessmen[7, 7];
         ChangePiecePosition(5, 7);
         selectedChessman = Chessmen[blackKing.x, blackKing.y];
         selectedChessman.GetComponent <Outline>().enabled = false;
     }
     if (!selectedChessman.isWhite && (selectedChessman.CurrentX - 2) == x)
     {
         selectedChessman = Chessmen[0, 7];
         ChangePiecePosition(3, 7);
         selectedChessman = Chessmen[blackKing.x, blackKing.y];
         selectedChessman.GetComponent <Outline>().enabled = false;
     }
 }
Exemplo n.º 12
0
    public void Promote(int type)
    {
        if (!isWhiteTurn)
        {
            type += 6;
        }
        activeChessman.Remove(selectedChessman.gameObject);
        Destroy(selectedChessman.gameObject);
        SpawnChessman(type, x2, y2);
        selectedChessman = Chessmans[x2, y2];

        Chessmans[selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
        selectedChessman.transform.position = GetTileCenter(x2, y2);
        selectedChessman.SetPosition(x2, y2);
        Chessmans[x2, y2] = selectedChessman;
        isWhiteTurn       = !isWhiteTurn;

        selectedChessman.GetComponent <MeshRenderer>().material = previousMat;
        BoardHighlights.Instance.HideHighlights();
        selectedChessman = null;
        if (x1 != -1)
        {
            lineRenderer.enabled        = true;
            lineRenderer.startColor     = color;
            lineRenderer.endColor       = color;
            lineRenderer.material.color = color;
            lineRenderer.startWidth     = 0.3f;
            Vector3 vfrom = GetTileCenter(x1, y1);
            Vector3 vto   = GetTileCenter(x2, y2);
            if (vfrom.z < vto.z)
            {
                vfrom.z -= 0.2f;
                vto.z   += 0.2f;
            }
            else if (vfrom.z > vto.z)
            {
                vfrom.z += 0.2f;
                vto.z   -= 0.2f;
            }

            lineRenderer.SetPosition(0, vfrom);
            lineRenderer.SetPosition(1, vto);

            if (!isEngineOn)
            {
                isUserWhite = !isUserWhite;
            }
            if ((isUserWhite != isWhiteTurn || !isEngineOn) && !replay)
            {
                send = true;
            }
        }

        wait = false;
        _queen.Ready();
        _rook.Ready();
        _bishop.Ready();
        _knight.Ready();
    }
Exemplo n.º 13
0
 private void DeSelectChessman()
 {
     if (selectedChessman != null)
     {
         selectedChessman.GetComponent <MeshRenderer>().material = previousMat;
     }
     selectedChessman = null;
 }
Exemplo n.º 14
0
    private void MoveChessman(int x, int y)
    {
        if (allowedMoves[x, y])
        {
            Chessman c = Chessmans [x, y];
            if (c != null && c.isWhite != isWhiteTurn)
            {
                //capture a piece

                //if it is the king
                if (c.GetType() == typeof(King))
                {
                    EndGame();
                    return;
                }

                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }
            //EnPassantMove [1] = -1;
            //EnPassantMove [1] = -1;
            if (selectedChessman.GetType() == typeof(Pawns))
            {
                if (y == 7)
                {
                    activeChessman.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessman(1, x, y);
                    selectedChessman = Chessmans [x, y];
                }
                if (y == 0)
                {
                    activeChessman.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessman(7, x, y);
                    selectedChessman = Chessmans [x, y];
                }
                //if(selectedChessman.CurrentY == 1&& y ==3){
                //EnPassantMove [1] = x;
                //EnPassantMove [1] = y - 1;

                //}
                //else if (selectedChessman.CurrentY == 6&& y ==4){
                //EnPassantMove [1] = x;
                //EnPassantMove [1] = y + 1;
                //}
            }

            Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
            selectedChessman.transform.position = GetTileCenter(x, y);
            selectedChessman.SetPosition(x, y);
            Chessmans [x, y] = selectedChessman;
            isWhiteTurn      = !isWhiteTurn;
        }
        selectedChessman.GetComponent <MeshRenderer> ().material = previousMat;
        BoardHighlights.Instance.Hidehighlights();
        selectedChessman = null;
    }
Exemplo n.º 15
0
    private bool SelectChessman(int x, int y)
    {
        if (Chessmans[x, y] == null)
        {
            return(false);
        }

        selectedChessman = Chessmans[x, y];

        // change render for selected chessman
        previousMat             = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;

        // BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);

        return(true);
    }
Exemplo n.º 16
0
    private void SelectChessman(int x, int y)
    {
        //if there is no chessman on the space clicked
        if (Chessmans[x, y] == null)
        {
            return;
        }
        //if not turn of the color of piece clicked on
        if (Chessmans[x, y].isWhite != isWhiteTurn)
        {
            return;
        }

        bool hasAtLeastOneMove = false;

        allowedMoves = Chessmans[x, y].PossibleMove();

        checkIllegalMoves(x, y);

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (allowedMoves[i, j])
                {
                    hasAtLeastOneMove = true;
                }
            }
        }
        if (!hasAtLeastOneMove)
        {
            return;
        }

        BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);

        //select the clicked on chess piece
        selectedChessman        = Chessmans[x, y];
        previousMat             = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
    }
Exemplo n.º 17
0
    private void SelectChessman(int x, int y)
    {
        if (isFinished)
        {
            return;
        }
        //if there is no chessman on that position, return
        if (Chessmans [x, y] == null)
        {
            return;
        }
        //if it is not your turn, return.
        if (Chessmans[x, y].isWhite != isWhiteTurn)
        {
            return;
        }

        bool leastOneMove = false;

        allowedMoves = Chessmans[x, y].PossibleMove();

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (allowedMoves[i, j])
                {
                    leastOneMove = true;
                }
            }
        }

        if (!leastOneMove)
        {
            return;
        }
        selectedChessman        = Chessmans[x, y];
        previousMat             = selectedChessman.GetComponent <MeshRenderer> ().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        BoardHighlights.Instance.HighlighAllowedMoves(allowedMoves);
    }
Exemplo n.º 18
0
    //말을 움직인다 (상대 말을 없애는 것도 있음)
    void MoveChessman(int x, int y)
    {
        if (AllowedMoves[x, y])           //배열안에 값이 있다면
        {
            Number--;                     //횟수를 깍는다
            Chessman c = Chessmans[x, y]; //Chessmans배열을(위치를) 넣은 Chessman형 변수 선언
            sound.Play();

            if (c != null && c.isWhite != isWhiteTurn) //c가 null값이 아니고 isWhite와 isWhiteTurn이 서로 다를때(흑-흑이나 백-백이아닌 상황일때)
            {                                          //이동한곳에 c가 있다면(흑 - 백 / 백 - 흑) 이런 상황 // isWhite는 도달한곳에 있는 말이 흑인지 백인지를 나타낸다.
                ActiveEnemy.Remove(c.gameObject);      // 해당gameObject가 들어가있는 activeChessman리스트를 지운다.
                Destroy(c.gameObject);                 //해당gameObject를 없앤다.
                Debug.Log(ActiveEnemy.Count);
                if (Number > -1)                       //number가 -1보다 클때
                {
                    if (ActiveEnemy.Count == 0)        //ActiveEnemy리스트안에 아무것도 없다면(체스판에 검은돌이 없다면)
                    {
                        Clear++;                       //Clear + 1 올린다.
                        Number = 10;                   //횟수를 10으로 초기화시킨다.(다음 스테이지를 위해)
                        if (Clear == 2)                //1스테이지를 깼을때
                        {
                            EndGame1();                //2스테이지를 불러오는 함수를 부른다.
                            //Debug.Log(ActiveChessman.Count);
                        }
                        if (Clear == 3) //2스테이지를 깼을때
                        {
                            EndGame2(); //3스테이지를 불러오는 함수를 부른다.
                        }
                        if (Clear == 4) //3스테이지를 깼을때
                        {
                            Vic_ChangeScene();
                        }
                    }
                }
            }
            if (Number == 0)
            {
                if (ActiveEnemy.Count > 0)
                {
                    Defeat_ChangeScene();
                }
            }

            Chessmans[SelectedChessman.CurrentX, SelectedChessman.CurrentY] = null; //다른 위치로 옮길 것이기 때문에 현재위치를 null로 바꿈
            SelectedChessman.transform.position = GetTileCenter(x, y);              //체스말을 놓을 좌표를 잡아주는 함수에다가 이동시킬 좌표를 넣는다
            SelectedChessman.SetPosition(x, y);                                     //선택한 말에 현재위치값을 바꿔준다(SetPosition 함수에서 바꿔준다)
            Chessmans[x, y] = SelectedChessman;                                     //움직인 말의 현재위치를 Chessmans배열(8 x 8)에 넣는다
            //isWhiteTurn = !isWhiteTurn; //말을 움직이면 턴이 바뀐다.
        }

        SelectedChessman.GetComponent <MeshRenderer>().material = previousMat; //선택한 material을 기존걸로 바꾼다.
        BoardHighlights.Instance.Hidehighlights();                             //하이라이트를 숨기는 함수를 부른다.
        SelectedChessman = null;                                               //턴이 바뀌고 다시 null값이 된다.
    }
Exemplo n.º 19
0
    private void SelectChessman(int x, int y)
    {
        //Nema figura na toj pozicij
        if (Chessmans [x, y] == null)
        {
            return;
        }

        //Provjera boje (Uvjet vrijedi ako za crne figure)
        if (Chessmans[x, y].isWhite != isWhiteTurn)
        {
            return;
        }

        //Ako se figura ne moze pomjerit ne odabiri je
        bool hasAtleastOneMove = false;

        allowedMoves = Chessmans [x, y].PossibleMove();
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (allowedMoves[i, j])
                {
                    hasAtleastOneMove = true;
                }
            }
        }

        if (!hasAtleastOneMove)
        {
            return;
        }

        selectedCehessman = Chessmans[x, y];
        //Mjenjanje materijala pri odabiru figure
        previousMat = selectedCehessman.GetComponent <MeshRenderer>().material;
        // selectedMat.mainTexture = previousMat.mainTexture;
        selectedCehessman.GetComponent <MeshRenderer>().material = selectedMat;
        BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
    }
Exemplo n.º 20
0
    void SetRookPositionInCastle(int toX_World, King kingComponent)
    {
        int xDelta = board.GetBoardPos(toX_World) - kingComponent.X_Board;
        int row    = kingComponent.isWhite ? 0 : 7;

        int rookOldX = xDelta == -3 ? 0 : 7;
        int rookNewX = xDelta == -3 ? 2 : 5;

        Chessman rook = board.GetComponentInChessman <Chessman>(row, rookOldX);

        RpcMoveFigure(rook.GetComponent <NetworkIdentity>(), row, rookOldX, board.GetWorldPos(row), board.GetWorldPos(rookNewX), row == 0, false);
    }
Exemplo n.º 21
0
    //말을 선택한다
    void SelectChessman(int x, int y)
    {
        //Chessman c = Chessmans[x, y]; //이걸 안넣으면 3스테이지에서 안먹어지는 버그가 일어난다 (이유는 나도 잘..)

        if (Chessmans[x, y] == null) //Chessmans배열이 null값이라면
        {
            return;
        }

        if (Chessmans[x, y].isWhite == false)
        {
            return;
        }

        bool hasAtleastOneMove = false;                //false로 선언

        AllowedMoves = Chessmans[x, y].PossibleMove(); //반환받은 동적인 bool형이중배열을 bool형이중배열인 allowedMoves에 넣는다.
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (AllowedMoves[i, j])
                {
                    hasAtleastOneMove = true; //움직일 수 있는 위치가 존재한다면 true가 된다.
                }
            }
        }
        if (!hasAtleastOneMove) //false 일때
        {
            Debug.Log(hasAtleastOneMove);
            return; //밑으로 안내려가고 밖으로 벗어난다.
        }

        SelectedChessman        = Chessmans[x, y];                                         //좌표값을 넣은 배열을 selectedChessman변수에 넣는다.
        previousMat             = SelectedChessman.GetComponent <MeshRenderer>().material; //선택한 말의 material을 previousMat변수에 넣는다
        selectedMat.mainTexture = previousMat.mainTexture;                                 //selectedMat의 텍스쳐를 previousMat의 텍스쳐(selectedChessman의 텍스쳐)로 한다.
        SelectedChessman.GetComponent <MeshRenderer>().material = selectedMat;             //selectedChessman의 material을 지정해둔 selectedMat의 material로 한다.
        BoardHighlights.Instance.HighlightAllowedMoves(AllowedMoves);                      //움직일 수 있는 좌표값을 HighlightAllowedMoves함수에 보낸다
    }
Exemplo n.º 22
0
    // Chon quan co tai vi tri x, y tren ban co
    private void SelectChessman(int x, int y)
    {
        if (Chessmans[x, y] == null)    // Neu vi tri tren ban co khong co quan co
        {
            return;
        }

        if (Chessmans[x, y].isWhite != isWhiteTurn) // Neu nhu quan co duoc chon la quan trang nhung khong phai luot cua quan trang thi return
        {
            return;
        }

        bool hasAtleastOneMove = false;

        allowedMoves = Chessmans[x, y].PossibleMove(); // Tim so nuoc di co the cua quan co [x,y] tren ban co
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (allowedMoves[i, j]) // Neu nhu co ton tai nuoc di
                {
                    hasAtleastOneMove = true;
                }
            }
        }

        if (!hasAtleastOneMove) // Nếu như khong có nước đi
        {
            return;
        }

        selectedChessman = Chessmans[x, y]; // Lưu quân cờ được chon
        // Thao tác với kiểu mô hình được chọn
        previousMat             = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        // Hiển thị đường đi có thể di chuyển của quân cờ
        BoardHighligts.Instance.HighlightAllowedMoves(allowedMoves);
    }
Exemplo n.º 23
0
    private void SelectChessman(int x, int y)
    {
        if (Chessmen[x, y] == null)
        {
            return;
        }

        if (Chessmen[x, y].isWhite != isWhiteTurn)
        {
            return;
        }


        var hasAtLeastOneMove = false;

        AllowedMoves = Chessmen[x, y].PossibleMove();
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (AllowedMoves[i, j])
                {
                    hasAtLeastOneMove = true;
                }
            }
        }

        if (!hasAtLeastOneMove)
        {
            return;
        }

        _selectedChessman       = Chessmen[x, y];
        _previousMat            = _selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = _previousMat.mainTexture;
        _selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        BoardHighlights.Instance.HighlightAllowedMoves(AllowedMoves);
    }
Exemplo n.º 24
0
    private void SelectChessman(int x, int y)
    {
        if (Chessmans[x, y] == null)
        {
            return; //return nothing if user is not selecting a piece
        }
        if (Chessmans[x, y].isWhite != isWhiteTurn)
        {
            return; //white player is trying to select a black piece
        }
        bool hasAtLeastOneMove = false;

        allowedMoves = Chessmans [x, y].PossibleMove();
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (allowedMoves[i, j])
                {
                    hasAtLeastOneMove = true;
                }
            }
        }

        if (!hasAtLeastOneMove)
        {
            return;
        }

        selectedChessman        = Chessmans[x, y];
        previousMat             = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = previousMat.mainTexture;

        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        //BoardFeedback.Instance.HighlightAllowedMoves(allowedMoves); //bug when uncommented, when this line is used, more than the appropriate # of
        //allowed moves are highlighted, not sure why this is.
    }
Exemplo n.º 25
0
    public void RpcMoveChessman(int x0, int y0, int x, int y)
    {
        if (Chessmans[x0, y0].PossibleMove(x, y))
        {
            Chessman c = Chessmans[x, y];
            if (c != null && c.isWhite != isWhiteTurn)
            {
                activeChessman.Remove(c.gameObject);
                if (c.IsKing())
                {
                    ClearBoard();
                    SpawnAllChessmans();
                    isWhiteTurn = true;
                    BoardHighlightsOffline.Instace.HideHighlights();
                    return;
                }
                Destroy(c.gameObject);
                audioSource.PlayOneShot(captureSound, 1f);
            }
            else
            {
                audioSource.PlayOneShot(moveSound, .6f);
            }

            Chessmans[x0, y0].transform.position = GetTileCenter(x, y);
            Chessmans[x0, y0].SetPosition(x, y);
            Chessmans[x0, y0].nMoves += 1;
            Chessmans[x, y]           = Chessmans[x0, y0];
            Chessmans[x0, y0]         = null;
            isWhiteTurn = !isWhiteTurn;
        }
        selectedChessman.transform.localScale = new Vector3(.1f, .1f, .1f);
        selectedChessman.GetComponent <Renderer>().material.color = Color.white;
        oldSelectionX = -1;
        oldSelectionY = -1;
        Vector3    pos = selectedChessman.transform.position;
        Quaternion rot = selectedChessman.transform.rotation;

        pos.y = 0f;
        selectedChessman.transform.position = pos;
        selectedChessman.transform.rotation = Quaternion.Euler(0f, rot.eulerAngles.y, 0f);
        // selectedChessman.transform.position = GetTileCenter(selectedChessman.CurrentX, selectedChessman.CurrentY);
        BoardHighlightsOffline.Instace.HideHighlights();
        selectedChessman = null;
    }
Exemplo n.º 26
0
    private void Update()
    {
        // Добралась ли какая-то пешка до противоположного края
        if (deleteChessman != null)
        {
            // Выбор игрока
            if (ReplacePawn.figNum == 1 ||
                ReplacePawn.figNum == 2 ||
                ReplacePawn.figNum == 3 ||
                ReplacePawn.figNum == 4)
            {
                // Запомнить пешку
                var delete = deleteChessman;

                if (deleteChessman.isWhite) // Поставить белую фигуру
                {
                    SpawnChessman(ReplacePawn.figNum, deleteChessman.CurrentX, deleteChessman.CurrentY);
                }
                else // Поставить чёрную фигуру
                {
                    SpawnChessman(ReplacePawn.figNum + 6, deleteChessman.CurrentX, deleteChessman.CurrentY);
                }

                // Удалить пешку из списка живых фигур
                activeChessman.Remove(delete.gameObject);
                // Удалить саму пешку
                Destroy(delete.gameObject);

                // Привести переменные к стандартным значениям
                deleteChessman     = null;
                ReplacePawn.figNum = 0;
            }
        }
        else
        {
            DrawChessboard();
            UpdateSelection();
            SetCamera();

            if (Input.GetMouseButtonDown(0))
            {
                if (selectionX >= 0 && selectionY >= 0)
                {
                    if (selectedChessman == null)
                    {
                        SelectChessman(selectionX, selectionY);
                    }
                    else
                    {
                        if (Chessmans[selectionX, selectionY]?.isWhite == selectedChessman.isWhite)
                        {
                            BoardHighLights.Instance.Hidehighlights();
                            selectedChessman.GetComponent <MeshRenderer>().material = previousMat;

                            SelectChessman(selectionX, selectionY);
                        }
                        else
                        {
                            MoveChessman(selectionX, selectionY);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 27
0
    public void MoveChessmanAI()
    {
        // Cancello l'highlight dell'ultima mossa
        BoardHighlights.Instance.Hidelastmove();

        // Posizione finale
        Chessman c = Chessmans[pendingX, pendingY];

        // Cattura pezzo
        if (c != null)
        {
            // Se è il re
            if (c.GetType() == typeof(King))
            {
                // End the game
                //EndGame();
                return;
            }

            activeChessman.Remove(c.gameObject);
            Destroy(c.gameObject);
            selectedChessman = null;
        }
        BoardHighlights.Instance.Hidelastmove();
        // Evidenzia la mossa
        bool[,] lastMove = new bool[8, 8];
        lastMove[selectionX, selectionY] = true;
        lastMove[pendingX, pendingY]     = true;
        BoardHighlights.Instance.HighlightLastMove(lastMove);
        // Partenza
        selectedChessman          = Chessmans[selectionX, selectionY];
        selectedChessman.CurrentX = selectionX;
        selectedChessman.CurrentY = selectionY;
        Chessmans[selectedChessman.CurrentX, selectedChessman.CurrentY] = null;

        // Arrocco
        if (selectedChessman.GetType() == typeof(KingUci) && canCastling_AI)
        {
            Castling_AI();
        }
        // EnPassant
        if (selectedChessman.GetType() == typeof(PawnUci) && c == null)
        {
            EnPassant_AI(pendingX, pendingY);
        }
        // Arrivo
        selectedChessman.transform.position = Utility.Instance.GetTileCenter(pendingX, pendingY);
        selectedChessman.SetPosition(pendingX, pendingY);
        Chessmans[pendingX, pendingY] = selectedChessman;

        // Cambio turno
        isPending   = false;
        isWhiteTurn = !isWhiteTurn;

        // Cambio Materiale
        previousMat             = selectedChessman.GetComponent <MeshRenderer>().material;
        selectedMat.mainTexture = previousMat.mainTexture;
        selectedChessman.GetComponent <MeshRenderer>().material = selectedMat;
        selectedChessman.GetComponent <MeshRenderer>().material = previousMat;

        // reset
        selectedChessman = null;
    }
Exemplo n.º 28
0
    private void MoveChessman(int x, int y)
    {
        if (allowedMoves[x, y])
        {
            Chessman c = Chessmans[x, y];

            if (c != null && c.isWhite != isWhiteTurn)
            {
                // Cattura pezzo

                // Se è il re
                if (c.GetType() == typeof(KingUci))
                {
                    // End the game
                    //EndGame();
                    return;
                }
                // Tolgo il pezzo
                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }

            // Prendo la posizione iniziale del pezzo
            initialX = selectedChessman.CurrentX;
            initialY = selectedChessman.CurrentY;

            string initial   = Utility.Instance.Translate(initialX, initialY);
            string ending    = Utility.Instance.Translate(x, y);
            string finalMove = initial + ending;
            Debug.Log(finalMove);
            //---MOSSE LEGALI---

            /*
             * if (!(IsLegal(finalMove)))
             * {
             *  Debug.Log("Mossa illegale!");
             *  return;
             * }*/

            // Metto a null la casa da cui muovo
            Chessmans[selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
            // Muovo effettivamente il pezzo
            selectedChessman.transform.position = Utility.Instance.GetTileCenter(x, y);
            selectedChessman.SetPosition(x, y);
            Chessmans[x, y] = selectedChessman;

            // Arrocco
            if (selectedChessman.GetType() == typeof(KingUci) && canCastling)
            {
                Castling(finalMove);
            }
            // Promozione
            if (selectedChessman.GetType() == typeof(PawnUci) && ending[1] == '8')
            {
                Promote(finalMove, x, y);
                return;
            }
            // EnPassant
            if (selectedChessman.GetType() == typeof(PawnUci) && c == null)
            {
                EnPassant(x, y);
            }

            // Invio la mossa al server
            Middleware.Instance.DoMove(finalMove);
            //Cambio turno
            isWhiteTurn = !isWhiteTurn;
        }
        // Togli l'evidenzia
        selectedChessman.GetComponent <MeshRenderer>().material = previousMat;
        // Eliminazione degli highlight
        BoardHighlights.Instance.Hidehighlights();

        selectedChessman = null;
    }
Exemplo n.º 29
0
    private void MoveChessman(int x, int y)
    {
        if (allowedMoves[x, y])
        {
            Chessman c = Chessmans[x, y];

            if (c != null && c.isWhite != isWhiteTurn)
            {
                //Capture a piece
                activeChessPieces.Remove(c.gameObject);
                Destroy(c.gameObject);
                destroySound.Play();
            }

            // enpassant move
            if (x == EnPassantMove[0] && y == EnPassantMove[1])
            {
                if (isWhiteTurn)
                {
                    c = Chessmans[x, y - 1];
                }
                else
                {
                    c = Chessmans[x, y + 1];
                }

                activeChessPieces.Remove(c.gameObject);
                Destroy(c.gameObject);
                destroySound.Play();
            }
            EnPassantMove[0] = -1;
            EnPassantMove[1] = -1;
            if (selectedChessman.GetType() == typeof(Pawn))
            {
                // promote for white rook
                if (y == 7)
                {
                    activeChessPieces.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessPieces(0, x, y);
                    selectedChessman = Chessmans[x, y];
                    promotionSound.Play();
                }
                // promote for black rook
                else if (y == 0)
                {
                    activeChessPieces.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessPieces(2, x, y);
                    selectedChessman = Chessmans[x, y];
                    promotionSound.Play();
                }

                if (selectedChessman.CurrentY == 1 && y == 3)
                {
                    EnPassantMove[0] = x;
                    EnPassantMove[1] = y - 1;
                }
                else if (selectedChessman.CurrentY == 6 && y == 4)
                {
                    EnPassantMove[0] = x;
                    EnPassantMove[1] = y + 1;
                }
            }

            Chessmans[selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
            selectedChessman.transform.position = GetTileCenter(x, y);
            selectedChessman.SetPosition(x, y);
            Chessmans[x, y] = selectedChessman;
            isWhiteTurn     = !isWhiteTurn;
            moveSound.Play();
        }

        selectedChessman.GetComponent <MeshRenderer>().material = previousMat;
        BoardHighlights.Instance.HideHighlights();
        selectedChessman = null;
    }
Exemplo n.º 30
0
    private void MoveChessman(int x, int y)
    {
        if (allowedMoves[x, y])
        {
            Chessman c = Chessmans [x, y];

            if (c != null && c.isWhite != isWhiteTurn)
            {
                // CAPTURE A PIECE

                // IF IT IS THE KING
                if (c.GetType() == typeof(King))
                {
                    EndGame();
                    return;
                }

                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }

            if (x == EnPassantMove[0] && y == EnPassantMove[1])
            {
                c = Chessmans[x, isWhiteTurn ? y - 1 : y + 1];

                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }
            EnPassantMove[0] = -1;
            EnPassantMove[1] = -1;
            if (selectedChessman.GetType() == typeof(Pawn))
            {
                if (y == 7)
                {
                    activeChessman.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessman(1, x, y);
                    selectedChessman = Chessmans[x, y];
                }

                else if (y == 0)
                {
                    activeChessman.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessman(7, x, y);
                }

                if (selectedChessman.CurrentY == 1 && y == 3)
                {
                    EnPassantMove[0] = x;
                    EnPassantMove[1] = y - 1;
                }
                else if (selectedChessman.CurrentY == 6 && y == 4)
                {
                    EnPassantMove[0] = x;
                    EnPassantMove[1] = y + 1;
                }
            }

            Chessmans[selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
            selectedChessman.transform.position = GetTileCenter(x, y);
            selectedChessman.SetPosition(x, y);
            Chessmans[x, y] = selectedChessman;
            isWhiteTurn     = !isWhiteTurn;
        }

        selectedChessman.GetComponent <MeshRenderer>().material = previousMat;
        BoardHighlights.Instance.HideHighlights();
        selectedChessman = null;
    }