예제 #1
0
 private bool CheckLine(Marker[][] board, Marker player, Vector2 c1, Vector2 c2, Vector2 c3)
 {
     if (board[(int)c1.x][(int)c1.y] == player &&
         board[(int)c2.x][(int)c2.y] == player &&
         board[(int)c3.x][(int)c3.y] == player)
     {
         WinningCoordinates = new WinVectors(c1, c2, c3);
         return(true);
     }
     return(false);
 }
예제 #2
0
    public void PlayTicTacToe( )
    {
        if (IsGameover == false)
        {
            if (p1_X == null || p2_O == null)
            {
                return;
            }

            if (p1_X.IsTurnActive == true)
            {
                if (timed == false)
                {
                    t     = turnDelay + Time.time;
                    timed = true;
                }

                if (Time.time < t)
                {
                    return;
                }
                timed = false;

                TicTacToeMove bestMove = null;

                if (isSearching == false)
                {
                    bestMove    = p1_X.NegaMaxMove(board); // NEGAMAX tree-search
                    isSearching = true;
                }

                if (bestMove != null)
                {
                    FireRandomScribbleSFX( );
                    boardState[(int)bestMove.Cell.x][(int)bestMove.Cell.y] = Marker.X;

                    cells[bestMove.Cell].GetComponent <SpriteRenderer>().sprite = Resources.Load <Sprite> (ResourcePath.x);

                    p1_X.IsTurnActive = false;
                    p2_O.IsTurnActive = true;

                    if (board.CheckForWinner(boardState, Marker.X))
                    {
                        GameWinner         = "You lost!";
                        WinningCoordinates = board.WinningCoordinates;
                        IsGameover         = true;
                    }
                    if (board.CheckForDraw(boardState))
                    {
                        GameWinner         = "A draw - better than losing.";
                        WinningCoordinates = null;
                        IsGameover         = true;
                    }

                    isSearching = false;
                }
                else
                {
                    Debug.Log("AI is searching for a move ... ");
                    return;
                }
            }
            else if (p2_O.IsTurnActive == true)
            {
                GameObject clicked = p2_O.ClickHandler <Grid2DVertexComponent> ( );

                if (clicked == null)
                {
                    return;
                }

                Vector2 m = clicked.transform.position;
                if (boardState[(int)m.x][(int)m.y] == Marker.Blank)       //move success
                {
                    FireRandomScribbleSFX( );
                    boardState[(int)m.x][(int)m.y] = Marker.O;

                    clicked.GetComponent <SpriteRenderer> ( ).sprite = Resources.Load <Sprite> (ResourcePath.o);

                    if (board.CheckForWinner(boardState, Marker.O))
                    {
                        GameWinner         = "You won!";
                        WinningCoordinates = board.WinningCoordinates;
                        IsGameover         = true;
                    }
                    if (board.CheckForDraw(boardState))
                    {
                        GameWinner         = "A draw - better than losing.";
                        WinningCoordinates = null;
                        IsGameover         = true;
                    }

                    p2_O.IsTurnActive = false;
                    p1_X.IsTurnActive = true;
                }
            }
        }
        else
        {
            Debug.Log("GAMEOVER");
        }
    }