Esempio n. 1
0
    void Update()
    {
        if (updateSceneCoroutine != null || board.status != BoardStatus.Playing || resigned)
        {
            return;
        }

        if (playerAI != null && playerAI.color == board.whoseTurn)
        {
#if UNITY_WEBGL || UNITY_EDITOR
            if (!findingMove)
            {
                playerAI.FindMove(new Board(board));
                findingMove = true;
            }
            else if (!playerAI.isCalculating)
            {
                findingMove = false;
#else
            if (playerAIThread == null)
            {
                playerAIThread = new Thread(new ParameterizedThreadStart(playerAI.FindMove));
                playerAIThread.Start(new Board(board));
            }
            else if (!playerAIThread.IsAlive)
            {
                playerAIThread = null;
#endif
                Move move    = playerAI.foundMove;
                bool success = board.MakeMove(move);
                Debug.Assert(success);

                UpdateScene((board.sideEffect != null) ? new List <Move>()
                {
                    move, board.sideEffect.Value
                } : new List <Move>()
                {
                    move
                });
            }
        }
        else
        {
            mouseSquare = GetMouseSquare();

            if (Input.GetMouseButtonDown(0))
            {
                if (mouseSquare != null)
                {
                    if (selectedSquare == null) // clicked on the board, but no previously selected square
                    {
                        GamePiece g = Get(mouseSquare);
                        if (g != null && g.color == board.whoseTurn) // only select if it's one of the current player's pieces
                        {
                            Square temp = mouseSquare;
                            mouseSquare    = null; // remove highlight before selecting
                            selectedSquare = temp;
                        }
                    }
                    else if (mouseSquare == selectedSquare)
                    {
                        selectedSquare = null;
                        mouseSquare    = null; // ensures the piece at mouseSquare can be re-highlighted on next update (after piece is finished moving)
                    }
                    else // the player has attempted to make a move
                    {
                        Move move = new Move(selectedSquare, mouseSquare);
                        if (board.MakeMove(move))
                        {
                            selectedSquare = null;
                            mouseSquare    = null; // ensures the piece at mouseSquare can be re-highlighted on next update (after piece is finished moving)

                            GamePiece g = Get(move.from), h = Get(move.to);
                            g.transform.position = GetSquareCenter(move.to);
                            if (h != null)
                            {
                                Take(move.to);
                                waitForPiecesCoroutine = StartCoroutine(WaitForPiecesRoutine(new List <GamePiece>()
                                {
                                    g, h
                                }));
                            }
                            else
                            {
                                waitForPiecesCoroutine = StartCoroutine(WaitForPiecesRoutine(new List <GamePiece>()
                                {
                                    g
                                }));
                            }
                            Set(move.from, null);
                            Set(move.to, g);

                            UpdateScene((board.sideEffect != null) ? new List <Move>()
                            {
                                board.sideEffect.Value
                            } : null);
                        }
                    }
                }
            }
        }
    }