Beispiel #1
0
    private void OnValidate()
    {
        _grid = new OptimizedGrid(gridWidth, gridHeight, nodeWidth, nodeHeight, spacing);

        _grid.GenerateGrid();
        _grid.LoadMeshData(_mesh);
    }
Beispiel #2
0
    public IEnumerator GetRandomMove(OptimizedGrid optiGrid, System.Action <Move> toDo)
    {
        yield return(new WaitForEndOfFrame());

        grid = optiGrid;

        List <Move> newGameMoves = grid.GetAvailableMoves((CellColor)aiColor);

        toDo(newGameMoves[Random.Range(0, newGameMoves.Count - 1)]);
    }
Beispiel #3
0
 private void CreateGrid()
 {
     modelGrid = new ModelGrid(5, 9, FindObjectsOfType <Cell>().ToList());
     optiGrid  = new OptimizedGrid(5, 9);
     optiGrid.SetPatternData(aiEvaluationData);
 }
Beispiel #4
0
    public IEnumerator GetBestMove(OptimizedGrid optiGrid, System.Action <Move> toDo)
    {
        grid = optiGrid;

        positionCount = 0;
        timeSpent     = 0;
        float actualTime = Time.realtimeSinceStartup;

        Move bestMove = new Move();
        bool done     = false;

        yield return(new WaitForEndOfFrame());

        List <Vector2> canWinCells = new List <Vector2>();

        // if AI has a 4-0 pattern
        if (grid.CanColorWin(aiColor, out canWinCells))
        {
            // if AI can move to win
            if (grid.CanColorMoveToWin(aiColor, canWinCells, out bestMove))
            {
                grid.DoMove(bestMove);

                if (IsScoreEnoughToWin(aiColor, canWinCells))
                {
                    done = true;
                }

                grid.UndoMove(bestMove);
            }
        }

        // if Player can win next turn
        if (grid.CanColorWin(opponentColor, out canWinCells) && IsScoreEnoughToWin(opponentColor, canWinCells) && !done)
        {
            // if player can move to win next turn
            if (grid.CanColorMoveToWin(opponentColor, canWinCells, out bestMove))
            {
                grid.DoMove(bestMove);
                bool canPlayerWin = IsScoreEnoughToWin(opponentColor, canWinCells);
                grid.UndoMove(bestMove);

                // can AI def it ???
                if (canPlayerWin && grid.CanColorMoveToWin(aiColor, canWinCells, out bestMove))
                {
                    grid.DoMove(bestMove);

                    if (IsScoreEnoughToWin(aiColor, canWinCells))
                    {
                        done = true;
                    }

                    grid.UndoMove(bestMove);
                }
            }
        }

        if (!done)
        {
            int depth = 3;
            bestMove = MiniMaxRoot(depth, true);
        }

        float newTime = Time.realtimeSinceStartup;

        timeSpent = newTime - actualTime;

        Debug.Log(positionCount + " in " + timeSpent);

        toDo(bestMove);
    }