Example #1
0
    //Tests if a goal node is achieved or if the max depth is reached.
    bool TerminalTest(ref int terminalValue, int depth)
    {
        // If AI can put anywhere, find the one that it has bext chance to win.
        if (currentBigGrid == 10)
        {
            currentBigGrid = FindBestBigGrid();
        }
        //Debug.Log(currentBigGrid);
        // Free to go any big grid
        if (GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[currentBigGrid].GetComponent <BigGridScript>().IsGridCompleted(AITurn))
        {
            terminalValue = MAX_VALUE;
            return(true);
        }
        if (GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[currentBigGrid].GetComponent <BigGridScript>().IsGridCompleted(PlayerTurn))
        {
            terminalValue = MIN_VALUE;
            return(true);
        }
        if (GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[currentBigGrid].GetComponent <BigGridScript>().IsDraw())
        {
            terminalValue = 0;
            return(true);
        }

        // Called when max depth is reached
        if (depth == 0)
        {
            BigGridScript go = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[currentBigGrid].GetComponent <BigGridScript>();
            terminalValue = EvaluationTest(go);
            //Debug.Log("ES: " + terminalValue);
            return(true);
        }
        return(false);
    }
Example #2
0
    void HighlightGrid_RPC(int bigID, int smallID)
    {
        BoardScript   board     = GameObject.Find("Board").GetComponent <BoardScript>();
        BigGridScript bigGrid   = board.bigGrids[bigID].GetComponent <BigGridScript>();
        GridScript    smallGrid = bigGrid.grids[smallID].GetComponent <GridScript>();

        smallGrid.HighlightGrid();
    }
Example #3
0
    void ConfirmPlacement_RPC(int bigID, int smallID, Defines.TURN turn, float time)
    {
        BoardScript      board     = GameObject.Find("Board").GetComponent <BoardScript>();
        BigGridScript    bigGrid   = board.bigGrids[bigID].GetComponent <BigGridScript>();
        GridScript       smallGrid = bigGrid.grids[smallID].GetComponent <GridScript>();
        GUIManagerScript guiScript = GameObject.FindGameObjectsWithTag("GUIManager")[0].GetComponent <GUIManagerScript>();

        smallGrid.ConfirmPlacement();
        guiScript.SetTimer(turn, time);
    }
Example #4
0
    int FindBestBigGrid()
    {
        //Debug.Log("COMEHERE");
        // The big grid that the AI has most chance to win.
        int[] vals;
        vals = new int[9];
        int bestID     = -1;
        int bestVal    = -1;
        int sameweight = 0;

        for (int i = 0; i < 9; ++i)
        {
            BigGridScript go = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[i].GetComponent <BigGridScript>();
            //Debug.Log(go.gridWinner);
            //this is so that we do not place into won already grids?
            if (go.gridWinner == 0)
            {
                int val = EvaluationTest(go, false);
                vals[i] = val;
                //Debug.Log(i + ": " + val);
                if (bestVal < val)
                {
                    bestID  = i;
                    bestVal = val;
                }
                else if (bestVal == val)
                {
                    ++sameweight;
                }
            }
            else
            {
                vals[i] = -1;                   // completed grids should have no chance.
            }
        }

        /*string s="";
         * for(int j =0 ; j <9 ; ++j)
         * {
         *      s += "[" + vals[j] + "] ";
         *      if(j%3 == 2)
         *              s+= "\n";
         * }
         * Debug.Log(s);*/
        //Debug.Log("Same weight: " + sameweight);
        //if this number is 8, it means all the board have the same chance of winning
        //so we will random a board between all of them
        if (sameweight == 8)
        {
            bestID = UnityEngine.Random.Range(0, 9);
        }

        return(bestID);
    }
Example #5
0
    int FindBestBigGrid()
    {
        // The big grid that the AI has most chance to win.
        int bestID = -1;

        for (int i = 0; i < 9; ++i)
        {
            BigGridScript go  = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[i].GetComponent <BigGridScript>();
            int           val = EvaluationTest(go);
            if (bestID < val)
            {
                bestID = val;
            }
        }
        return(bestID);
    }
Example #6
0
    void ConfirmPlacement_RPC(int bigID, int smallID, Defines.TURN turn, float time)
    {
        // If not correct turn
        if (GameObject.FindGameObjectWithTag("GUIManager").GetComponent <TurnHandler>().turn != turn)
        {
            return;
        }

        BoardScript      board     = GameObject.Find("Board").GetComponent <BoardScript>();
        BigGridScript    bigGrid   = board.bigGrids[bigID].GetComponent <BigGridScript>();
        GridScript       smallGrid = bigGrid.grids[smallID].GetComponent <GridScript>();
        GUIManagerScript guiScript = GameObject.FindGameObjectWithTag("GUIManager").GetComponent <GUIManagerScript>();

        smallGrid.ConfirmPlacement();
        //guiScript.SetTimer(turn, time);
        guiScript.ResetTimer();

        // Confirms p2 action and ask p2 to execute it.
        if (NetworkManager.IsPlayerOne() && turn == Defines.TURN.P2)
        {
            GetNetworkGameLogic().ConfirmPlacement(bigID, smallID, turn, time);
        }
    }
Example #7
0
    // Heuristic function that calculates the number of lines that are still available for Computer to use and win.
    int EvaluationTest(BigGridScript go, bool terminal = true)
    {
        int noofLines = 0;

        // Checks for horizontal wins
        if (go.grids[0].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[1].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[2].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[3].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[5].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[6].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[7].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[8].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[0].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[3].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[6].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[1].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[7].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[2].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[5].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[8].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[0].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[8].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[2].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[6].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (terminal == false)
        {
            for (int i = 0; i < 9; ++i)
            {
                if (go.grids[i].GetComponent <GridScript>().gridState == (int)AITurn)
                {
                    if (UnityEngine.Random.Range(1, 101) <= 60)
                    {
                        ++noofLines;
                    }
                }
                //add a chance to add to the no of lines
            }
        }

        return(noofLines);
    }
Example #8
0
    // Heuristic function that calculates the number of lines that are still available for Computer to use and win.
    int EvaluationTest(BigGridScript go)
    {
        int noofLines = 0;

        // Checks for horizontal wins
        if (go.grids[0].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[1].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[2].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[3].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[5].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[6].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[7].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[8].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[0].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[3].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[6].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[1].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[7].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[2].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[5].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[8].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[0].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[8].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        if (go.grids[2].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[4].GetComponent <GridScript>().gridState != (int)PlayerTurn &&
            go.grids[6].GetComponent <GridScript>().gridState != (int)PlayerTurn)
        {
            noofLines += 1;
        }

        return(noofLines);
    }
Example #9
0
    void PrePlaceBoard()
    {
        GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().UpdateActiveGridBG(0, false);

        BigGridScript currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[0].GetComponent <BigGridScript>();

        currBigGrid.grids[1].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[3].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[4].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[6].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[8].GetComponent <GridScript>().PlaceOnGrid(2);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[1].GetComponent <BigGridScript>();
        currBigGrid.grids[0].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[1].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[2].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[5].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[7].GetComponent <GridScript>().PlaceOnGrid(2);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[2].GetComponent <BigGridScript>();
        currBigGrid.grids[0].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[2].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[4].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[5].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[6].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[8].GetComponent <GridScript>().PlaceOnGrid(2);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[3].GetComponent <BigGridScript>();
        currBigGrid.grids[4].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[6].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[7].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[8].GetComponent <GridScript>().PlaceOnGrid(1);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[4].GetComponent <BigGridScript>();
        currBigGrid.grids[1].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[2].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[3].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[7].GetComponent <GridScript>().PlaceOnGrid(2);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[5].GetComponent <BigGridScript>();
        currBigGrid.grids[1].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[2].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[4].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[8].GetComponent <GridScript>().PlaceOnGrid(2);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[6].GetComponent <BigGridScript>();
        currBigGrid.grids[3].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[4].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[6].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[8].GetComponent <GridScript>().PlaceOnGrid(2);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[7].GetComponent <BigGridScript>();
        currBigGrid.grids[1].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[3].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[4].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[5].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[7].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.ProcessBigGridCompleted(Defines.TURN.P1);

        currBigGrid = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardScript>().bigGrids[8].GetComponent <BigGridScript>();
        currBigGrid.grids[0].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[4].GetComponent <GridScript>().PlaceOnGrid(1);
        currBigGrid.grids[5].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[6].GetComponent <GridScript>().PlaceOnGrid(2);
        currBigGrid.grids[7].GetComponent <GridScript>().PlaceOnGrid(1);
    }