Beispiel #1
0
    public void OnPieceSelected(int _pos)
    {
        bool canMove = false;

        Debug.Log("source pos = " + m_SelectedPiece.ToString() + ", target pos = " + _pos.ToString());

        if ((m_SelectedPiece != -1) && (m_SelectedPiece != _pos))
        {
            List <cgSimpleMove> moveSet = m_Board.findStrictLegalMoves(m_Board.whiteTurnToMove);
            foreach (cgSimpleMove move in moveSet)
            {
                if (move.from == m_SelectedPiece && move.to == _pos)
                {
                    if (IsPromotionMove(move))
                    {
                        m_CurMove = move;
                        WndManager.Singleton.OpenPromotionSelectWnd(m_Board.whiteTurnToMove, CallbackPromotionSelect);
                    }
                    else
                    {
                        OnMovePiece(move);
                        canMove = true;
                    }
                    break;
                }
            }

            UnselectAllSquare();
            m_SelectedPiece = -1;
        }

        if (m_SelectedPiece == -1 || !canMove)
        {
            UnselectAllSquare();
//             List<cgSimpleMove> moveSet = m_Board.findStrictLegalMoves(m_Board.whiteTurnToMove);
//             foreach (cgSimpleMove move in moveSet)
//                 if (move.from == _pos)
//                     SelectSquare(move.to);
            if (m_Board.squares[_pos] != 0 &&
                ((m_Board.squares[_pos] > 0 && m_Board.whiteTurnToMove && (m_CanMove == CanMove.All || (m_CanMove == CanMove.White))) ||
                 (m_Board.squares[_pos] < 0 && !m_Board.whiteTurnToMove && (m_CanMove == CanMove.All || (m_CanMove == CanMove.Black)))))
            {
                SelectSquare(_pos);
                m_SelectedPiece = _pos;
            }
            else
            {
                m_SelectedPiece = -1;
            }
        }
    }
Beispiel #2
0
    /// <summary>
    /// A piece has callbacked that the user has pressed it.
    /// </summary>
    /// <param name="piece"></param>
    private void _pieceDown(cgChessPieceScript piece)
    {
        if (highlightLegalMoves)
        {
            List <cgSimpleMove> moves = _abstractBoard.findStrictLegalMoves(_abstractBoard.whiteTurnToMove);
            foreach (cgSimpleMove move in moves)
            {
                if (cgGlobal.SquareNames[move.from] == piece.square.uniqueName)
                {
                    _getSquare(cgGlobal.SquareNames[move.to]).changeColor(_getSquare(cgGlobal.SquareNames[move.from]).legalMoveToColor);
                }
            }
        }
        _downPiece = piece;


        //int indexPosition = cgGlobal.IndexFromCellName(_downPiece.square.uniqueName);

        //_abstractBoard.squares[indexPosition = 2//make the changes you want.
    }
    /// <summary>
    /// Called when the analysis starts.
    /// </summary>
    /// <returns></returns>
    private IEnumerator _startAnalysis(Action <float> progressCallback = null)
    {
        yield return(null);

        //Console.WriteLine("Starting analysis.");
        finished = false;
        moves    = _board.findStrictLegalMoves(moveAsWhite);
        if (moves.Count > 1)
        {
            movesLeftToAnalyze  = moves.Count;
            totalMovesToAnalyze = moves.Count;
            int alpha = int.MinValue;
            int beta  = int.MaxValue;
            for (int i = 0; i < moves.Count; i++)
            {
                cgSimpleMove possibleMove = moves[i];
                byte         depth        = (cgValueModifiers.AlphaBeta_Strong_Delineation < possibleMove.positionalVal ? searchDepthStrong : searchDepthWeak);
                if (moves.Count < 10)
                {
                    depth = searchDepthEndGame;
                }
                //determining whether move looks weak or strong based on positional value, if its strong we use strong depth otherwise we use weak depth.
                possibleMove.val = _alfaBeta(possibleMove, depth, alpha, beta, !moveAsWhite);
                movesLeftToAnalyze--;
                yield return(null);

                if (progressCallback != null)
                {
                    float progress = (float)((float)totalMovesToAnalyze - (float)movesLeftToAnalyze) / (float)totalMovesToAnalyze;
                    progressCallback(progress);
                }
                //UnityEngine.Debug.Log("Moves left: " + MovesLeftToAnalyze + " total: " + TotalMovesToAnalyze);
            }
            _sortMovesOnBoardValue(moves, moveAsWhite);
        }

        yield return(null);

        _analysisComplete();
    }
Beispiel #4
0
    /// <summary>
    /// Called when the analysis starts.
    /// </summary>
    /// <returns></returns>
    IEnumerator _startAnalysis()
    {
        yield return(null);

        Finished = false;
        Moves    = _board.findStrictLegalMoves(MoveAsWhite);
        if (Moves.Count > 1)
        {
            MovesLeftToAnalyze  = Moves.Count;
            TotalMovesToAnalyze = Moves.Count;
            if (LoadBar != null)
            {
                LoadBar.transform.localScale = new Vector3(0, LoadBar.transform.localScale.y, 1);
            }
            int alpha = int.MinValue;
            int beta  = int.MaxValue;
            for (int i = 0; i < Moves.Count; i++)
            {
                cgSimpleMove possibleMove = Moves[i];
                byte         depth        = (cgValueModifiers.AlphaBeta_Strong_Delineation < possibleMove.positionalVal ? SearchDepthStrong : SearchDepthWeak);
                if (Moves.Count < 10)
                {
                    depth = SearchDepthEndGame;
                }
                //determining whether move looks weak or strong based on positional value, if its strong we use strong depth otherwise we use weak depth.
                possibleMove.val = _alfaBeta(possibleMove, depth, alpha, beta, !MoveAsWhite);
                MovesLeftToAnalyze--;
                yield return(null);
            }
            _sortMovesOnBoardValue(Moves, MoveAsWhite);
        }

        yield return(null);

        _analysisComplete();
    }
    // Update is called once per frame
    void Update()
    {
        lock (_engineCallbackParams) {
            if (_engineCallbackParams.Count > 0)
            {
                UnityEngine.Debug.Log(": " + _engineCallbackFunctions);
                _engineCallbackFunctions[0](_engineCallbackParams[0]);
                _engineCallbackParams.RemoveAt(0);
                _engineCallbackFunctions.RemoveAt(0);
                //threadListener.Stop();
            }
        }
        lock (_engineProgress) {
            if (_engineProgress.Count > 0 && engineProgressBar != null)
            {
                float progress = _engineProgress[0];
                _engineProgress.RemoveAt(0);
                Vector3 nscale = engineProgressBar.transform.localScale;
                nscale.x = progress;
                engineProgressBar.transform.localScale = nscale;
            }
        }
        foreach (cgChessPieceScript cp in _livePieces)
        {
            if (cp.dead && !_deadPieces.Contains(cp))
            {
                _setDeadPiece(cp);
            }
        }

        for (int i = _deadPieces.Count; i > 0; i--)
        {
            if (_livePieces.Contains(_deadPieces[i - 1]))
            {
                _livePieces.Remove(_deadPieces[i - 1]);
            }
        }
        if (_downPiece != null)
        {
            Vector3    cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
            Ray        cursorRay   = Camera.main.ScreenPointToRay(cursorPoint);
            RaycastHit hit;
            if (Physics.Raycast(cursorRay, out hit, 100.0f))
            {
                _downPiece.transform.position = hit.point;
            }
        }

        if (Input.GetKey(KeyCode.C) && Input.GetKeyDown(KeyCode.LeftControl))
        {
            _copyGameToClipboard();
        }
        else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.C))
        {
            _copyGameToClipboard();
        }


        if (Input.GetKey(KeyCode.V) && Input.GetKeyDown(KeyCode.LeftControl))
        {
            _pasteGameFromClipboard();
        }
        else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.V))
        {
            _pasteGameFromClipboard();
        }

        if (Input.GetKey(KeyCode.F1))
        {
            UnityEngine.Debug.Break();
        }
        if (Input.GetKey(KeyCode.A))
        {
            //_abstractBoard.boardToString()
            UnityEngine.Debug.Log(_abstractBoard.boardToString());
            //UnityEngine.Debug.Log(_abstractBoard._blackKingStart);
        }
        if (Input.GetKey(KeyCode.U))
        {
            _abstractBoard.revert();
            UnityEngine.Debug.Log(_abstractBoard.boardToString());
        }
        if (Input.GetKey(KeyCode.V))  //Perform long castling move.
        {
            List <cgSimpleMove> moves = _abstractBoard.findStrictLegalMoves(_abstractBoard.whiteTurnToMove);
            cgSimpleMove        move  = null;
            foreach (cgSimpleMove mv in moves)
            {
                if (mv is cgCastlingMove)
                {
                    if (move == null)
                    {
                        move = mv;
                    }
                    else if (Math.Abs(move.to - move.from) < Math.Abs(mv.to - mv.to))
                    {
                        move = mv;
                    }
                }
            }
            _makeMove(move);
        }
    }