/// <summary> /// The user has released a dragged piece. Verify that its a legal move, if so perform the move and perform the next move if appropriate mode. /// </summary> /// <param name="piece"></param> private void _pieceUp(cgChessPieceScript piece) { if (_downPiece != null) { if (playerCanMove || Mode == BoardMode.PlayerVsPlayer) { cgSimpleMove legalMove = null; cgSquareScript closestSquare = _findSquareAt(_downPiece.transform.position); List <cgSimpleMove> legalMoves = _abstractBoard.findLegalMoves(whiteTurnToMove); foreach (cgSimpleMove move in legalMoves) { if (cgGlobal.SquareNames[move.from] == _downPiece.square.uniqueName && cgGlobal.SquareNames[move.to] == closestSquare.uniqueName) { legalMove = move; } } //test legality of move here. if (legalMove != null && _abstractBoard.verifyLegality(legalMove)) { //piece.moveToSquare(closestSquare); _makeMove(legalMove); if (Mode == BoardMode.PlayerVsEngine) { _engine.MakeMove(_abstractBoard, false, _engineCallback); } else if (Mode == BoardMode.EngineVsPlayer) { _engine.MakeMove(_abstractBoard, true, _engineCallback); } } else { piece.moveToSquare(piece.square); } } else { piece.moveToSquare(piece.square); } _downPiece = null; } else { piece.moveToSquare(piece.square); _downPiece = null; } if (highlightLastMove) {//revert colors if highlighting is active foreach (cgSquareScript square in _squares) { square.changeColor(square.startColor); } } }
/// <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); } }
/// <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); } }