Example #1
0
    IEnumerator ComputerTurn()
    {
        bool computerCapture = false;

        EnableAllButtons(false);
        GameInfo.text = "Computer thinking...";
        ChessMove computerMove = new ChessMove();
        var       thread       = new Thread(() =>
        {
            computerMove = AI.MakeMove();
            if (Game.board1d[computerMove.last].pieceColor != PieceColor.Non)
            {
                computerCapture = true;
            }
            Game.MakeMove(computerMove.first, computerMove.last);
        });

        thread.Start();
        while (thread.IsAlive)
        {
            yield return(null);
        }
        if (computerCapture)
        {
            VisualBoard[computerMove.last].Button.image.color = Color.red;
        }
        oldPosition = new Vector3(((computerMove.first % 8) - 4) * 80 + 40, -(((computerMove.first / 8) - 4) * 80 + 40), 0);
        newPosition = new Vector3(((computerMove.last % 8) - 4) * 80 + 40, -(((computerMove.last / 8) - 4) * 80 + 40), 0);
        ChessPieceAnimation.transform.localPosition = oldPosition;
        ChessPieceAnimation.sprite = VisualBoard[computerMove.first].Shape.sprite;
        VisualBoard[computerMove.first].Shape.sprite = Empty;
        ChessPieceAnimation.enabled = true;
        animating = true;
    }
Example #2
0
    void Update()
    {
        //DrawChessBoard();
        UpdateSelection();

        if (Input.GetMouseButtonDown(0))
        {
            if (selectionX >= 0 && selectionY >= 0)
            {
                if (selectedFigure == null)
                {
                    // Select Figure
                    SelectChessFigure(selectionX, selectionY);
                }
                else
                {
                    // Move Figure
                    MoveChessFigure(selectionX, selectionY);
                }
            }
        }

        //AI should be blue player
        if (!isRedTurn)
        {
            Vector2 aiMove = new Vector2();
            Debug.Log(String.Format("Initial Values: x = {0}, y = {1}", aiMove.x, aiMove.y));

            do
            {
                selectedFigure = ai.SelectChessFigure();
                aiMove         = ai.MakeMove(selectedFigure);
                Debug.Log("selectedFigure" + String.Format("{0} - {1}", selectedFigure.CurrentX, selectedFigure.CurrentY));
                //Debug.Log(String.Format("{0} - {1}", aiMove.x, aiMove.y));
            } while (aiMove.x < 0 && aiMove.y < 0);

            SelectChessFigure(selectedFigure.CurrentX, selectedFigure.CurrentY);
            Debug.Log("Selected :" + String.Format("{0} - {1}", selectedFigure.CurrentX, selectedFigure.CurrentY));
            MoveChessFigure((int)Math.Round(aiMove.x), (int)Math.Round(aiMove.y));
            Debug.Log("Move to :" + String.Format("{0} - {1}", aiMove.x, aiMove.y));
            //Debug.Log("Red turn: " + isRedTurn);
        }
    }