Example #1
0
    private void FindBest()
    {
        float bestScore    = -99999;
        int   bestRotation = 0;
        int   bestColumn   = 0;

        for (int rotation = 0; rotation < 4; rotation++)
        {
            for (int column = 0; column < TetrisGrid.w; column++)
            {
                int x = 5;
                if (column < 5)
                {
                    while (activePiece.MoveLeft() && x > column)
                    {
                        x--;
                    }
                }
                else if (column > 5)
                {
                    while (activePiece.MoveRight() && x < column)
                    {
                        x++;
                    }
                }

                int rowsDeleted = activePiece.MoveDown(false);
                while (rowsDeleted == -1)
                {
                    rowsDeleted = activePiece.MoveDown(false);
                }

                // Calculate Score
                float lineDel = rowsDeleted * rowsWeight;
                float height  = activeGrid.AggregateHeight() * heightWeight;
                float bumps   = activeGrid.Bumpiness() * bumpinessWeight;
                float holes   = activeGrid.Holes() * holesWeight;

                float score = lineDel - height - bumps - holes;

                if (score > bestScore)
                {
                    bestScore    = score;
                    bestRotation = rotation;
                    bestColumn   = column;
                }

                Debug.Log("Check rot: " + rotation + " col: " + column + " height: " + height + " bumps: " + bumps + " lines deleted: " + lineDel + " holes: " + holes + " Score: " + score);

                // Return grid to privouse state
                activeGrid.grid = CopyGrid(originalGrid, TetrisGrid.w, TetrisGrid.h);
                // Return piece to origin
                activePiece.transform.position = pieceOrigin;
            }
            activePiece.RotateUp();
        }

        // Rotate to origin
        activePiece.transform.rotation = Quaternion.Euler(Vector3.zero);
        ExecuteBest(bestColumn, bestRotation);
    }