Ejemplo n.º 1
0
    public void computeFitness(ref AI candidate, int numberOfGames, int maxNumberOfMoves)
    {
        var ai = new AI(candidate);

        var totalScore = 0;

        for (var j = 0; j < numberOfGames; j++)
        {
            var grid          = new Grid(22, 10);
            var rpg           = new RandomPieceGenerator();
            var workingPieces = new List <Piece>(new Piece[2] {
                rpg.nextPiece(), rpg.nextPiece()
            });
            var workingPiece  = workingPieces[0];
            var score         = 0;
            var numberOfMoves = 0;
            while ((numberOfMoves++) < maxNumberOfMoves && !grid.isGridFull())
            {
                if (numberOfMoves % howManyMovesBetweenGarbageLines == (howManyMovesBetweenGarbageLines - garbageAdvancedWarningTurns) && numberOfMoves > startupDelay)
                {//a few turns before dumping the garbage, add it to the grid so they have time to defend themself
                    grid.incomingDangerousPieces = garbageLinesToGive;
                }
                if (numberOfMoves % howManyMovesBetweenGarbageLines == 0)
                {
                    while (grid.incomingDangerousPieces > 5)
                    {
                        grid.AddGarbageLines(5);
                        grid.incomingDangerousPieces -= 5;
                    }
                    grid.AddGarbageLines(grid.incomingDangerousPieces);
                    grid.incomingDangerousPieces = 0;
                }
                float scoreTest;
                bool  shouldSwap;//DON'T FORGET ABOUT ME!!! *thanks past self*
                workingPiece = ai.best(grid, workingPieces, out scoreTest, out shouldSwap);
                if (shouldSwap)
                {
                    grid.storedPiece = workingPieces[0];//store what is currently the 0th piece
                }
                while (workingPiece.moveDown(grid))
                {
                    ;
                }
                grid.addPiece(workingPiece);

                //Instead of giving out points based on line count
                score += grid.mappedLineCount(grid.clearLines());
                //We are gonna give out a point for each piece you place, and just make sure that everyone dies eventually
                //grid.clearLines();
                //score++;

                for (var k = 0; k < workingPieces.Count - 1; k++)
                {
                    workingPieces[k] = workingPieces[k + 1];              //shuffle each working piece over by 1
                }
                workingPieces[workingPieces.Count - 1] = rpg.nextPiece(); //get the next working piece
                workingPiece = workingPieces[0];
            }
            totalScore += score;
        }
        candidate.fitness = UnityEngine.Mathf.Max(totalScore, .01f);
    }
Ejemplo n.º 2
0
 // QUIZ: What does the `= null` assignment mean in the parameter list?
 // LEARN MORE at https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments
 public Board(IBoardContent content, RandomPieceGenerator pieceGenerator = null)
 {
     BoardContent   = content;
     PieceGenerator = pieceGenerator;
 }
Ejemplo n.º 3
0
 void Start()
 {
     generationScript = GameObject.Find("ImageTarget").GetComponent("RandomPieceGenerator") as RandomPieceGenerator;
     levelsComplete = 0;
     levelsWereDestroyed = false;
 }