Beispiel #1
0
 public void OnFirstMove()
 {
     while (curMove > 0)
     {
         board.revert();
         curMove--;
     }
     UpdateInfos();
     Wnd_Board.UpdateBoard();
     AudioSource.PlayClipAtPoint(MovePiece_Sound, Vector3.zero, 1.0f);
 }
    /// <summary>
    /// Utilizing an AlphaBeta searching algorithm, we generate moves evaluate them, prune and decide which is best.
    /// https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning
    /// </summary>
    /// <param name="node">the move to analyze</param>
    /// <param name="depth">The max depth to search to, execution time increases exponentially the higher the depth</param>
    /// <param name="alpha"></param>
    /// <param name="beta"></param>
    /// <param name="maximizing"></param>
    /// <returns>The value of the provided node</returns>


    private int _alfaBeta(cgSimpleMove node, int depth, int alpha = int.MinValue, int beta = int.MaxValue, bool maximizing = true)
    {
        _board.move(node);

        if (depth == 0)
        {
            int val = _board.Evaluate();
            _board.revert();
            return(val);
        }

        if (maximizing)
        {
            int v = int.MinValue;
            List <cgSimpleMove> replies = _board.findLegalMoves(true);
            foreach (cgSimpleMove reply in replies)
            {
                int candidate = _alfaBeta(reply, depth - 1, alpha, beta, false);
                v = candidate > v ? candidate : v;

                alpha = alpha > v ? alpha : v;
                if (beta < alpha)
                {
                    break;
                }
            }
            _board.revert();
            return(v);
        }
        else
        {
            int v = int.MaxValue;
            List <cgSimpleMove> replies = _board.findLegalMoves(false);
            foreach (cgSimpleMove reply in replies)
            {
                int candidate = _alfaBeta(reply, depth - 1, alpha, beta, true);
                v = candidate < v ? candidate : v;
                if (beta > v)
                {
                    beta = v;
                    //node.bestResponse = reply;
                }
                if (beta < alpha)
                {
                    break;
                }
            }
            _board.revert();
            return(v);
        }
    }
Beispiel #3
0
 public void RevertLastMove()
 {
     Debug.Log("reverting");
     _abstractBoard.revert();
     _setBoardTo(_abstractBoard);
 }
Beispiel #4
0
 void revertMove()
 {
     m_Board.revert();
 }
    // 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);
        }
    }