Example #1
0
    //50 Copy Solve to an IEnumerator
    /// <summary>
    /// Animates the solution of the tower
    /// </summary>
    /// <returns>Each step of the solution</returns>
    IEnumerator AnimatedSolution()
    {
        //51 Add the Y iteration to the begining
        for (int y = 0; y < _voxelGrid.GridSize.y; y++)
        {
            for (int x = 0; x < _voxelGrid.GridSize.x; x++)
            {
                for (int z = 0; z < _voxelGrid.GridSize.z; z++)
                {
                    SudokuVoxel sVoxel = (SudokuVoxel)_voxelGrid.Voxels[x, y, z];
                    if (sVoxel.State == 0)
                    {
                        for (int i = 1; i < 10; i++)
                        {
                            if (Possible(sVoxel, i))
                            {
                                sVoxel.ChangeState(i);

                                //52 Add yield return after changing the state
                                yield return(new WaitForSeconds(0.1f));

                                //53 Start recursion with Coroutine
                                StartCoroutine(AnimatedSolution());

                                if (GameAsList(y).Count(v => v.State == 0) != 0)
                                {
                                    sVoxel.ChangeState(0);

                                    //54 Add yield return after backtracking
                                    yield return(new WaitForSeconds(0.1f));
                                }
                            }
                        }
                        //55 Switch return to yield return
                        yield return(new WaitForSeconds(0.1f));
                    }
                }
            }
            //56 Switch return to yield return
            yield return(new WaitForSeconds(0.15f));
        }
    }
Example #2
0
    //38 Create the Solve method for a single layer
    /// <summary>
    /// Solves the a layer(game) of the tower
    /// </summary>
    /// <param name="y">The y coordinate of the game to be solved</param>
    private void Solve(int y)
    {
        //39 Iterate through X and Z indexes of layer Y
        for (int x = 0; x < _voxelGrid.GridSize.x; x++)
        {
            for (int z = 0; z < _voxelGrid.GridSize.z; z++)
            {
                //40 Get the SudokuVoxel to be evalauted, casting from Voxel
                SudokuVoxel sVoxel = (SudokuVoxel)_voxelGrid.Voxels[x, y, z];

                //41 Test if state of SudokuVoxel is 0 (empty cell)
                if (sVoxel.State == 0)
                {
                    //42 Try to change to the numbers between 1 and 9
                    for (int i = 1; i < 10; i++)
                    {
                        if (Possible(sVoxel, i))
                        {
                            //43 If it is possible, change it
                            sVoxel.ChangeState(i);

                            //44 Initiate the process again, moving to the next empty cell, recursively
                            Solve(y);

                            //45 This step is only reached if the solution succedded or failed
                            //Count the amount of empty spaces to see if solution succeeded
                            if (GameAsList(y).Count(v => v.State == 0) != 0)
                            {
                                //47 If solution failed, change the state of this voxel back to 0, backtracking
                                sVoxel.ChangeState(0);
                            }
                        }
                    }
                    //48 Exit method if all numbers have been tested and failed
                    return;
                }
            }
        }
        return;
    }