Exemple #1
0
    public void generate(List <sbyte> customPlacements = null)
    {
        //Destroy existing squares before we generate the new squares to fit the new board width and height.
        _destroyChildren(boardScript.chessPieceHolder);
        _destroyChildren(boardScript.chessSquareHolder);

        // Debug.Log("boardscript "+boardScript.chessSquareHolder);

        ////Add placemetns for the tiny board as is available from cgMenuScript
        //cgCustomBoardSettings.AddPiecePlacement(6, 7, new List<sbyte>{
        //            -2,-3,-5,-6,-3,-2,
        //            -1,-1,-1,-1,-1,-1,
        //             0, 0, 0, 0, 0, 0,
        //             0, 0, 0, 0, 0, 0,
        //             0, 0, 0, 0, 0, 0,
        //             1, 1, 1, 1, 1, 1,
        //             2, 3, 5, 6, 3, 2
        //            });
        _piecePlacements = customPlacements;// cgCustomBoardSettings.GetPiecePlacements(this.boardWidth, this.boardHeight);
        Debug.Log("piece placements " + _piecePlacements.Count);
        _board = new global::cgBoard(_piecePlacements, boardWidth, boardHeight);

        byte totalSquareCount         = (byte)(boardWidth * boardHeight);
        List <cgSquareScript> squares = new List <cgSquareScript>();

        for (byte b = 0; b < totalSquareCount; b++)
        {
            float      y         = Mathf.Floor(b / boardWidth);
            float      x         = b - (y * boardWidth);
            bool       black     = (b + y) % 2 == 0;
            GameObject newSquare = (GameObject)PrefabUtility.InstantiatePrefab(black ? blackSquarePrefab : whiteSquarePrefab);
            newSquare.transform.SetParent(boardScript.chessSquareHolder.transform);
            y *= squareSpacing.y;
            x *= squareSpacing.x;
            y  = -y;
            newSquare.transform.localScale    = new Vector3(squareScale.x, squareScale.y, newSquare.transform.localScale.z);
            newSquare.transform.localPosition = new Vector3(x + squareStartPoint.x, y - squareStartPoint.y, squareStartPoint.z);
            newSquare.GetComponent <cgSquareScript>().uniqueName = _board.SquareNames[b];
            //UnityEngine.Debug.Log("Scale: "+newSquare.transform.localScale);
            squares.Add(newSquare.GetComponent <cgSquareScript>());


            //newSquare.GetComponentInChildren<TextMesh>().text= b.ToString();
            //newSquare.GetComponentInChildren<TextMesh>().GetComponent<MeshRenderer>().sortingOrder = 9;
        }
        for (byte b = 0; b < _piecePlacements.Count; b++)
        {
            if (_piecePlacements[b] != 0)
            {
                //Create a piece and place it accordingly.
                cgChessPieceScript piece = ((GameObject)PrefabUtility.InstantiatePrefab(piecePrefab)).GetComponent <cgChessPieceScript>();
                piece.SetType(_piecePlacements[b]);
                piece.transform.SetParent(boardScript.chessPieceHolder.transform);
            }
        }
        boardScript.displayAs3d = use3d;
        boardScript.setBoardTo(_board);
        //We delay the start by 1 frame to allow unity garbage collector to destroy and remove any gameobjects that we've destroyed in this scope.
        //StartCoroutine(_startNextFrame(_board));
    }
    /// <summary>
    /// Place pieces according to the current state of the underlying abstractboard.
    /// </summary>
    public void placePieces()
    {
        //Collect existing pieces, we instantiate new ones if we run out of existing pieces below.
        List <cgChessPieceScript> pieces = new List <cgChessPieceScript>();

        foreach (cgChessPieceScript piece in chessPieceHolder.GetComponentsInChildren <cgChessPieceScript>())
        {
            pieces.Add(piece);
        }

        UnityEngine.Debug.Log("Place pieces called. " + pieces.Count + " " + chessPieceHolder.GetComponentsInChildren <cgChessPieceScript>().Length + " " + _abstractBoard.squares.Count);
        for (byte i = 0; i < _abstractBoard.squares.Count; i++)
        {
            if (_abstractBoard.squares[i] != 0)
            {
                if (pieces.Count == 0)   //We instantiate more pieces if we run out of pieces to place.
                {
                    cgChessPieceScript newPiece = GameObject.Instantiate(ChessPiecePrefab).GetComponent <cgChessPieceScript>();
                    newPiece.gameObject.transform.parent = chessPieceHolder.transform;
                    newPiece.displayAs3D = this.displayAs3d;
                    newPiece.gameObject.SetActive(true);
                    pieces.Add(newPiece);
                    //UnityEngine.Debug.Log("Instantiating piece");
                }
                cgChessPieceScript piece = pieces[pieces.Count - 1];
                //UnityEngine.Debug.Log("i: "+i+" squarenames: "+_abstractBoard.SquareNames.Length);
                piece.StartAtSquare(_getSquare(_abstractBoard.SquareNames[i]));
                _livePieces.Add(piece);
                piece.dead        = false;
                piece.displayAs3D = this.displayAs3d;
                piece.SetType((int)_abstractBoard.squares[i]);
                pieces.Remove(piece);
                //UnityEngine.Debug.Log("Finding piece for " + _abstractBoard.squares[i]);
            }
        }

        //Unused pieces
        foreach (cgChessPieceScript piece in pieces)
        {
            GameObject.DestroyImmediate(piece.gameObject);
        }
        cgChessPieceScript[] allPieces = chessPieceHolder.GetComponentsInChildren <cgChessPieceScript>();
        foreach (cgChessPieceScript piece in allPieces)
        {
            if (piece.square == null)
            {
                GameObject.Destroy(piece.gameObject);
            }
        }
        //Give all active pieces callback functions for mouse events.
        foreach (cgChessPieceScript piece in _livePieces)
        {
            piece.SetCallbacks(_pieceDown, _pieceUp);
        }
    }
    /// <summary>
    /// Peform the provided move on the visual board and the abstract board, with no legality checks - thus should be performed prior to calling this.
    /// </summary>
    /// <param name="move"></param>
    private void _makeMove(cgSimpleMove move)
    {
        UnityEngine.Debug.Log("White: " + _abstractBoard.whiteTurnToMove);
        movesMade++;
        playSound(moveSound);
        _abstractBoard.move(move);
        _writeLog(move);
        //_abstractBoard.debugReadBoard();
        if (_getPieceOn(_abstractBoard.SquareNames[move.to]) != null && !(move is cgCastlingMove))
        {
            _setDeadPiece(_getPieceOn(_abstractBoard.SquareNames[move.to]));
        }
        cgChessPieceScript piece = _getPieceOn(_abstractBoard.SquareNames[move.from]);

        if (move.queened)
        {
            piece.SetType(_abstractBoard.squares[move.to] > 0 ? cgChessPieceScript.Type.WhiteQueen : cgChessPieceScript.Type.BlackQueen);
        }
        if (move is cgCastlingMove)
        {
            cgChessPieceScript piece2 = _getPieceOn(_abstractBoard.SquareNames[(move as cgCastlingMove).secondFrom]);
            if (piece2)
            {
                piece2.moveToSquare(_getSquare(_abstractBoard.SquareNames[(move as cgCastlingMove).secondTo]));
            }
        }
        else if (move is cgEnPassantMove)
        {
            cgChessPieceScript piece2 = _getPieceOn(_abstractBoard.SquareNames[(move as cgEnPassantMove).attackingSquare]);
            piece2.dead = true;
        }
        piece.moveToSquare(_getSquare(_abstractBoard.SquareNames[move.to]));

        whiteTurnToMove = _abstractBoard.whiteTurnToMove;
        _checkGameOver();
        if (highlightLastMove)
        {
            //Color copyFrom = _getSquare(cgGlobal.SquareNames[move.to]).startColor;
            Color color = _getSquare(_abstractBoard.SquareNames[move.to]).recentMoveColor;

            _getSquare(_abstractBoard.SquareNames[move.to]).highlightTemporarily(color);
        }

        //Debug.Log("making move. " + _abstractBoard.whiteTurnToMove+" moves "+_abstractBoard.moves.Count);
        //UnityEngine.Debug.Log("Time elapsed: " + stopwatch.Elapsed);

        if (Mode == BoardMode.EngineVsEngine)
        {
            MakeEngineMove(_abstractBoard.duplicate(), _abstractBoard.whiteTurnToMove, _engineCallback);
        }
    }
Exemple #4
0
    /// <summary>
    /// Peform the provided move on the visual board and the abstract board, with no legality checks - thus should be performed prior to calling this.
    /// </summary>
    /// <param name="move"></param>
    private void _makeMove(cgSimpleMove move)
    {
        //Debug.Log("making move:" + cgGlobal.MoveToString(move));

        movesMade++;
        playSound(moveSound);
        _abstractBoard.move(move);
        _writeLog(move);
        //_abstractBoard.debugReadBoard();
        if (_getPieceOn(cgGlobal.SquareNames[move.to]) != null)
        {
            _setDeadPiece(_getPieceOn(cgGlobal.SquareNames[move.to]));
        }
        cgChessPieceScript piece = _getPieceOn(cgGlobal.SquareNames[move.from]);

        piece.moveToSquare(_getSquare(cgGlobal.SquareNames[move.to]));
        if (move.promoted)
        {
            piece.SetType(_abstractBoard.squares[move.to] > 0 ? cgChessPieceScript.Type.WhiteQueen : cgChessPieceScript.Type.BlackQueen);
        }
        if (move is cgCastlingMove)
        {
            cgChessPieceScript piece2 = _getPieceOn(cgGlobal.SquareNames[(move as cgCastlingMove).secondFrom]);
            if (piece2)
            {
                piece2.moveToSquare(_getSquare(cgGlobal.SquareNames[(move as cgCastlingMove).secondTo]));
            }
        }
        else if (move is cgEnPassantMove)
        {
            cgChessPieceScript piece2 = _getPieceOn(cgGlobal.SquareNames[(move as cgEnPassantMove).attackingSquare]);
            piece2.dead = true;
        }

        whiteTurnToMove = _abstractBoard.whiteTurnToMove;
        _checkGameOver();
        if (highlightLastMove)
        {
            //Color copyFrom = _getSquare(cgGlobal.SquareNames[move.to]).startColor;
            Color color = _getSquare(cgGlobal.SquareNames[move.to]).recentMoveColor;

            _getSquare(cgGlobal.SquareNames[move.to]).highlightTemporarily(color);
        }
    }