Welcome to Dream.In.Code
Become an Expert!

Join 136,930 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,832 people online right now. Registration is fast and FREE... Join Now!




sudoku game in actionscript

 
Reply to this topicStart new topic

sudoku game in actionscript

bsatya_satya@yahoo.com
21 Jan, 2008 - 05:16 AM
Post #1

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 1

sudoku game creation in actionscript
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Sudoku Game In Actionscript
21 Jan, 2008 - 08:23 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,267



Thanked: 14 times
Dream Kudos: 650
My Contributions
Here at dreamincode, we like to see a good effort on your part(or at least some code you've written) before we'll help you out - we won't just do your homework for you.

Have you got some code or an attempt you could show us?
User is offlineProfile CardPM
+Quote Post

Sryn
RE: Sudoku Game In Actionscript
18 Sep, 2008 - 04:11 AM
Post #3

New D.I.C Head
*

Joined: 18 Sep, 2008
Posts: 1

Hi all,

I've just stumbled across this page while searching for something else and it reminded me of a sudoku flash program I did a while ago. It isn't finished. It's just that I need help with thinking about how to fill an empty 9x9 sudoku grid correctly. My prog gets stuck after about 20 numbers filled. Basically, I think that a backtracking when stuck algorithm might work, but I haven't given it any time since I left it ages ago. What might jolt me is some idea of the 'proper' way to fill in the grid. My .fla and .swf files aren't allowed to be uploaded, so I've just copied over my AS below. Any pointers is greatly appreciated. Cheers.


Sryn
sryn.blogspot.com

CODE


// constants declaration
// NB: ActionScript 2.0 don't have built-in support for constants

_global.GridSize = 9;

_global.GridSizeSquare = GridSize * GridSize;
_global.GridSizeSqrt = Math.sqrt(GridSize);

// variables declaration
// Block or Grid or Box configuration
//  012
//  345
//  678

_global.BoxArray = new Array(); // declare global box array
_global.Block = new Array(); // block of 1-9
_global.Horizontal = new Array(); // horizontal row of 1-9
_global.Vertical = new Array(); // vertical column of 1-9
_global.Chosen81Array = new Array(); // an 81X9 array to indicate chosen 1-9's in same row, column and block
_global.ReferenceArray = new Array(); // an 81X3 to indicate same column, row and block reference
/*
// a 4-Dimensional array to save instances of chosen 1-9 values in indirect rows, columns and blocks
_global.ChosenArray = new Array();
*/
_global.OneDBoxArray = new Array(GridSizeSquare); // declare global 1-Dimension box array

// test grid for calculation block, horizontal and vertical values
_global.TestArray = new Array(GridSizeSquare);
_global.HTestArray = new Array(GridSizeSquare);
_global.VTestArray = new Array(GridSizeSquare);
_global.BTestArray = new Array(GridSizeSquare);

// an array to show whether each small box is filled or not
_global.GridFilled = new Array(GridSizeSquare);

/*
_global.WinString = new String; // the string that indicates which row, column or diagonal is the winning one
*/

// function declarations start

_global.GetRandom = function(max:Number, min:Number):Number {
// returns an random integer value between max and min inclusive
    return Math.round(Math.random()*(max-min))+min;
}

_global.FillBoxesRand1to9 = function() {
// fill all boxes with a random number between 1 and 9
    var count:Number=0;
    
    for (var i:Number = 0; i < GridSize; i++) // Medium Boxes
        for (var i:Number = 0; i < GridSize; i++) // Small Boxes
            count++;
            
    return count;
}

_global.traceWholeGrid = function() {
// show BoxArray in output panel
    trace("");
    // Medium Top
    trace(BoxArray[0][0] +""+ BoxArray[0][1] +""+ BoxArray[0][2] + " " + BoxArray[1][0] +""+ BoxArray[1][1] +""+ BoxArray[1][2] + " " + BoxArray[2][0] +""+ BoxArray[2][1] +""+ BoxArray[2][2]);
    trace(BoxArray[0][3] +""+ BoxArray[0][4] +""+ BoxArray[0][5] + " " + BoxArray[1][3] +""+ BoxArray[1][4] +""+ BoxArray[1][5] + " " + BoxArray[2][3] +""+ BoxArray[2][4] +""+ BoxArray[2][5]);
    trace(BoxArray[0][6] +""+ BoxArray[0][7] +""+ BoxArray[0][8] + " " + BoxArray[1][6] +""+ BoxArray[1][7] +""+ BoxArray[1][8] + " " + BoxArray[2][6] +""+ BoxArray[2][7] +""+ BoxArray[2][8]);
    trace("");
    // Medium Middle
    trace(BoxArray[3][0] +""+ BoxArray[3][1] +""+ BoxArray[3][2] + " " + BoxArray[4][0] +""+ BoxArray[4][1] +""+ BoxArray[4][2] + " " + BoxArray[5][0] +""+ BoxArray[5][1] +""+ BoxArray[5][2]);
    trace(BoxArray[3][3] +""+ BoxArray[3][4] +""+ BoxArray[3][5] + " " + BoxArray[4][3] +""+ BoxArray[4][4] +""+ BoxArray[4][5] + " " + BoxArray[5][3] +""+ BoxArray[5][4] +""+ BoxArray[5][5]);
    trace(BoxArray[3][6] +""+ BoxArray[3][7] +""+ BoxArray[3][8] + " " + BoxArray[4][6] +""+ BoxArray[4][7] +""+ BoxArray[4][8] + " " + BoxArray[5][6] +""+ BoxArray[5][7] +""+ BoxArray[5][8]);
    trace("");
    // Medium Bottom
    trace(BoxArray[6][0] +""+ BoxArray[6][1] +""+ BoxArray[6][2] + " " + BoxArray[7][0] +""+ BoxArray[7][1] +""+ BoxArray[7][2] + " " + BoxArray[8][0] +""+ BoxArray[8][1] +""+ BoxArray[8][2]);
    trace(BoxArray[6][3] +""+ BoxArray[6][4] +""+ BoxArray[6][5] + " " + BoxArray[7][3] +""+ BoxArray[7][4] +""+ BoxArray[7][5] + " " + BoxArray[8][3] +""+ BoxArray[8][4] +""+ BoxArray[8][5]);
    trace(BoxArray[6][6] +""+ BoxArray[6][7] +""+ BoxArray[6][8] + " " + BoxArray[7][6] +""+ BoxArray[7][7] +""+ BoxArray[7][8] + " " + BoxArray[8][6] +""+ BoxArray[8][7] +""+ BoxArray[8][8]);
    trace("");
}

_global.updateDisplay = function() {
// updates the main display grid

    // top
    // top 1st row
    Main.TopLeft.OneOne.SmallBox_Number     = BoxArray[0][0];
    Main.TopLeft.OneTwo.SmallBox_Number     = BoxArray[0][1];
    Main.TopLeft.OneThree.SmallBox_Number   = BoxArray[0][2];

    Main.TopMiddle.OneOne.SmallBox_Number   = BoxArray[1][0];
    Main.TopMiddle.OneTwo.SmallBox_Number   = BoxArray[1][1];
    Main.TopMiddle.OneThree.SmallBox_Number = BoxArray[1][2];

    Main.TopRight.OneOne.SmallBox_Number    = BoxArray[2][0];
    Main.TopRight.OneTwo.SmallBox_Number    = BoxArray[2][1];
    Main.TopRight.OneThree.SmallBox_Number  = BoxArray[2][2];

    // top 2nd row
    Main.TopLeft.TwoOne.SmallBox_Number     = BoxArray[0][3];
    Main.TopLeft.TwoTwo.SmallBox_Number     = BoxArray[0][4];
    Main.TopLeft.TwoThree.SmallBox_Number   = BoxArray[0][5];

    Main.TopMiddle.TwoOne.SmallBox_Number   = BoxArray[1][3];
    Main.TopMiddle.TwoTwo.SmallBox_Number   = BoxArray[1][4];
    Main.TopMiddle.TwoThree.SmallBox_Number = BoxArray[1][5];

    Main.TopRight.TwoOne.SmallBox_Number    = BoxArray[2][3];
    Main.TopRight.TwoTwo.SmallBox_Number    = BoxArray[2][4];
    Main.TopRight.TwoThree.SmallBox_Number  = BoxArray[2][5];

    // top 3rd row
    Main.TopLeft.ThreeOne.SmallBox_Number     = BoxArray[0][6];
    Main.TopLeft.ThreeTwo.SmallBox_Number     = BoxArray[0][7];
    Main.TopLeft.ThreeThree.SmallBox_Number   = BoxArray[0][8];

    Main.TopMiddle.ThreeOne.SmallBox_Number   = BoxArray[1][6];
    Main.TopMiddle.ThreeTwo.SmallBox_Number   = BoxArray[1][7];
    Main.TopMiddle.ThreeThree.SmallBox_Number = BoxArray[1][8];

    Main.TopRight.ThreeOne.SmallBox_Number    = BoxArray[2][6];
    Main.TopRight.ThreeTwo.SmallBox_Number    = BoxArray[2][7];
    Main.TopRight.ThreeThree.SmallBox_Number  = BoxArray[2][8];

    // centre
    // centre 1st row
    Main.CentreLeft.OneOne.SmallBox_Number     = BoxArray[3][0];
    Main.CentreLeft.OneTwo.SmallBox_Number     = BoxArray[3][1];
    Main.CentreLeft.OneThree.SmallBox_Number   = BoxArray[3][2];

    Main.CentreMiddle.OneOne.SmallBox_Number   = BoxArray[4][0];
    Main.CentreMiddle.OneTwo.SmallBox_Number   = BoxArray[4][1];
    Main.CentreMiddle.OneThree.SmallBox_Number = BoxArray[4][2];

    Main.CentreRight.OneOne.SmallBox_Number    = BoxArray[5][0];
    Main.CentreRight.OneTwo.SmallBox_Number    = BoxArray[5][1];
    Main.CentreRight.OneThree.SmallBox_Number  = BoxArray[5][2];

    // centre 2nd row
    Main.CentreLeft.TwoOne.SmallBox_Number     = BoxArray[3][3];
    Main.CentreLeft.TwoTwo.SmallBox_Number     = BoxArray[3][4];
    Main.CentreLeft.TwoThree.SmallBox_Number   = BoxArray[3][5];

    Main.CentreMiddle.TwoOne.SmallBox_Number   = BoxArray[4][3];
    Main.CentreMiddle.TwoTwo.SmallBox_Number   = BoxArray[4][4];
    Main.CentreMiddle.TwoThree.SmallBox_Number = BoxArray[4][5];

    Main.CentreRight.TwoOne.SmallBox_Number    = BoxArray[5][3];
    Main.CentreRight.TwoTwo.SmallBox_Number    = BoxArray[5][4];
    Main.CentreRight.TwoThree.SmallBox_Number  = BoxArray[5][5];

    // centre 3rd row
    Main.CentreLeft.ThreeOne.SmallBox_Number     = BoxArray[3][6];
    Main.CentreLeft.ThreeTwo.SmallBox_Number     = BoxArray[3][7];
    Main.CentreLeft.ThreeThree.SmallBox_Number   = BoxArray[3][8];

    Main.CentreMiddle.ThreeOne.SmallBox_Number   = BoxArray[4][6];
    Main.CentreMiddle.ThreeTwo.SmallBox_Number   = BoxArray[4][7];
    Main.CentreMiddle.ThreeThree.SmallBox_Number = BoxArray[4][8];

    Main.CentreRight.ThreeOne.SmallBox_Number    = BoxArray[5][6];
    Main.CentreRight.ThreeTwo.SmallBox_Number    = BoxArray[5][7];
    Main.CentreRight.ThreeThree.SmallBox_Number  = BoxArray[5][8];

    // bottom
    // bottom 1st row
    Main.BottomLeft.OneOne.SmallBox_Number     = BoxArray[6][0];
    Main.BottomLeft.OneTwo.SmallBox_Number     = BoxArray[6][1];
    Main.BottomLeft.OneThree.SmallBox_Number   = BoxArray[6][2];

    Main.BottomMiddle.OneOne.SmallBox_Number   = BoxArray[7][0];
    Main.BottomMiddle.OneTwo.SmallBox_Number   = BoxArray[7][1];
    Main.BottomMiddle.OneThree.SmallBox_Number = BoxArray[7][2];

    Main.BottomRight.OneOne.SmallBox_Number    = BoxArray[8][0];
    Main.BottomRight.OneTwo.SmallBox_Number    = BoxArray[8][1];
    Main.BottomRight.OneThree.SmallBox_Number  = BoxArray[8][2];

    // bottom 2nd row
    Main.BottomLeft.TwoOne.SmallBox_Number     = BoxArray[6][3];
    Main.BottomLeft.TwoTwo.SmallBox_Number     = BoxArray[6][4];
    Main.BottomLeft.TwoThree.SmallBox_Number   = BoxArray[6][5];

    Main.BottomMiddle.TwoOne.SmallBox_Number   = BoxArray[7][3];
    Main.BottomMiddle.TwoTwo.SmallBox_Number   = BoxArray[7][4];
    Main.BottomMiddle.TwoThree.SmallBox_Number = BoxArray[7][5];

    Main.BottomRight.TwoOne.SmallBox_Number    = BoxArray[8][3];
    Main.BottomRight.TwoTwo.SmallBox_Number    = BoxArray[8][4];
    Main.BottomRight.TwoThree.SmallBox_Number  = BoxArray[8][5];

    // bottom 3rd row
    Main.BottomLeft.ThreeOne.SmallBox_Number     = BoxArray[6][6];
    Main.BottomLeft.ThreeTwo.SmallBox_Number     = BoxArray[6][7];
    Main.BottomLeft.ThreeThree.SmallBox_Number   = BoxArray[6][8];

    Main.BottomMiddle.ThreeOne.SmallBox_Number   = BoxArray[7][6];
    Main.BottomMiddle.ThreeTwo.SmallBox_Number   = BoxArray[7][7];
    Main.BottomMiddle.ThreeThree.SmallBox_Number = BoxArray[7][8];

    Main.BottomRight.ThreeOne.SmallBox_Number    = BoxArray[8][6];
    Main.BottomRight.ThreeTwo.SmallBox_Number    = BoxArray[8][7];
    Main.BottomRight.ThreeThree.SmallBox_Number  = BoxArray[8][8];
}

_global.fillWholeGridRandomly = function() {
// fills BoxArray randomly with values 1-9

    for (var i:Number = 0; i < GridSize; i++) // Medium Boxes
        for (var j:Number = 0; j < GridSize; j++) // Small Boxes
            BoxArray[i][j] = GetRandom(GridSize, GridSize/GridSize);
}

_global.fillRandomlyThenUpdate = function() {
// fills the grid randomly with values 1-9 and then update display
    fillWholeGridRandomly();
    traceWholeGrid();
    updateDisplay();
}

_global.resetBHV = function() {
// reset arrays Block, Horizontal and Vertical
// means that for each of those arrays,
// it is still OK to choose values 1-9
// if that array number's position contains a 1,
// ok to choose that number, eitherwise, not ok

    for (var i:Number = 0; i < GridSize; i++) // BHV Count
        for (var j:Number = 0; j < GridSize; j++) { // 1's - 9's
            Block[i][j]       = 1;
            Horizontal[i][j]  = 1;
            Vertical[i][j]    = 1;
/*            
            for (var k:Number = 0; i < GridSize; k++)
            // initialise he 4-D ChosenArray
                ChosenArray[i][j][k] = 0;
*/                
        } // for
}

_global.makeMultiArrays = function() {
// put an array into an array, i.e. making multidimensional arrays
    var i:Number = 0;

    for ( i=0; i<GridSize; i++) {
        BoxArray.push(new Array(GridSize));
        Block.push(new Array(GridSize));
        Horizontal.push(new Array(GridSize));
        Vertical.push(new Array(GridSize));
//        ChosenArray.push(new Array(GridSize));
    }
//    finishMakeChosenArray();
    for ( i=0; i<GridSizeSquare; i++) {
        Chosen81Array.push(new Array(GridSize));
        ReferenceArray.push(new Array(GridSizeSqrt));
    }
}
/*
_global.finishMakeChosenArray = function() {
// put in the final 4-Dimension array of 9 into ChosenArray
    for (var i:Number = 0; i < GridSize; i++) // Horizontal reference
        for (var j:Number = 0; j < GridSize; j++) // Vertical reference
            ChosenArray.push(new Array(GridSize)); // in Small Box reference
}
*/
_global.resetGridFilled = function() {
// fill GridFilled array with 0's to indicate that
// main array's small boxes is not filled yet
    for (var i:Number=0; i<(GridSizeSquare); i++)
        GridFilled[i] = 0;
}

_global.resetChosen81Array = function() {
// reset Chosen81Array with 0's
    for (var i:Number=0; i<GridSizeSquare; i++)
        for (var j:Number=0; j<GridSize; j++)
            Chosen81Array[i][j] = 0;
}

_global.fillReferenceArray = function() {
// fill ReferenceArray with appropriate values of same columns, rows and blocks
    for (var i:Number=0; i<GridSizeSquare; i++) {
        for (var j:Number=0; j<GridSizeSqrt; j++)
            switch (j) {
                case 0:
                    ReferenceArray[i][j] = findHorizontalNumber(i);
                    trace("Case = " + i + " " + j + " " + ReferenceArray[i][j]);
                    break;
                case 1:
                    ReferenceArray[i][j] = findVerticalNumber(i);
                    trace("Case = " + i + " " + j + " " + ReferenceArray[i][j]);
                    break;
                case 2:
                    ReferenceArray[i][j] = findBlockNumber(i);
                    trace("Case = " + i + " " + j + " " + ReferenceArray[i][j]);
                    break;
                default: trace("No Case");
            } // switch
        trace ("");
    }
}

_global.resetArray = function(ThisArray:Array, resetValue:Number):Array {
// fill ThisArray with resetValues's
    for (var i:Number=0; i<ThisArray.length; i++)
        ThisArray[i] = resetValue;
    
    return ThisArray;
}

_global.resetWholeGrid = function() {
// fills BoxArray with value 0

    for (var i:Number = 0; i < GridSize; i++) // Medium Boxes
        for (var j:Number = 0; j < GridSize; j++) // Small Boxes
            BoxArray[i][j] = 0;
}

_global.resetWholeGridThenUpdate = function() {
// fills the grid randomly with value 0 and then update display
    resetWholeGrid();
    traceWholeGrid();
    updateDisplay();
}

_global.resetWholeGridBlank = function() {
// fills BoxArray with ""
    for (var i:Number = 0; i < GridSize; i++) // Medium Boxes
        for (var j:Number = 0; j < GridSize; j++) // Small Boxes
            BoxArray[i][j] = "";
}

_global.resetWholeGridBlankThenUpdate = function() {
// fills the grid randomly with "" and then update display
    resetWholeGridBlank();
    resetGridFilled();
    resetBHV();
    resetChosen81Array();
    traceWholeGrid();
    updateDisplay();
}

_global.findOneEmptySmallBox = function():Number {
// chooses a blank small box randomly
// returns 0 if no empty small box found
// returns 1-81 if found, meaning the location in a 9X9 grid

    var howManyFilledSmallBoxes:Number = 0;
    var i:Number = 0;
    var j:Number = 0;

    for (i=0; i<GridSizeSquare; i++)
        howManyFilledSmallBoxes = howManyFilledSmallBoxes + GridFilled[i];
        
    if (howManyFilledSmallBoxes < GridSizeSquare) {
//        j = GetRandom(GridSizeSquare-howManyFilledSmallBoxes, GridSize/GridSize);
        j = 1; // fill one by one from start to end
        i = 0;
        while ((j > 0) && (i < GridSizeSquare))
            if (GridFilled[i++] == 0)
                j--;
        trace ("");
        trace ("Chosen Small Box = " + i);
        return i;
    } else {
        return 0;
    }        
}

_global.fillOneEmptySmallBox = function():Number {
// fills a blank small box randomly with 1-9 where appropriate
    var chosenSmallBox:Number = 0;
    var foundAppropriateNumber:Number = 0;
    
    chosenSmallBox = findOneEmptySmallBox();
    
    if (chosenSmallBox == 0) {
        trace ("No empty small box found");
        return 0;
    } else {
        foundAppropriateNumber = findAppropriateNumber(chosenSmallBox - 1);
        // less one because we start count from 0 until 80 for 81 locations
        trace ("Appropriate Number = " + foundAppropriateNumber);
        trace ("Solvability = " + checkSolvability());
        return foundAppropriateNumber;
    }
}

_global.findAppropriateNumber = function(chosenSmallBox:Number):Number {
// finds an appropriate 1-9 to fill in selected box
// appropriate means choosing a number that is not already chosen in
// the same block of 9 (3X3), horizontal row nor vertical column
    var AnswerArray:Array = Array(GridSize);
//    var AnswerArray2:Array = Array();

    var AppropriateNumber:Number = 0;
    
    var HorizontalNumber:Number = 0;
    var VerticalNumber:Number = 0;
    var BlockNumber:Number = 0;
    
    var QuanOfPossibleHorizontalValue:Number = 0;
    var QuanOfPossibleVerticalValue:Number = 0;
    var QuanOfPossibleBlockValue:Number = 0;
    
    AnswerArray = resetArray(AnswerArray, 0);
//    trace ("AnswerArray[" + AnswerArray + "]");
    
    HorizontalNumber = findHorizontalNumber(chosenSmallBox);
    VerticalNumber = findVerticalNumber(chosenSmallBox);
    BlockNumber = findBlockNumber(chosenSmallBox);
/*    
    trace ("HorizontalNumber = " + HorizontalNumber);
    trace ("VerticalNumber = " + VerticalNumber);
    trace ("BlockNumber = " + BlockNumber);
*/    
    AnswerArray = updateAnswerArray(AnswerArray, HorizontalNumber, VerticalNumber, BlockNumber);
    AnswerArray = updateAnswerArray2(AnswerArray, chosenSmallBox);
//    AnswerArray = updateAnswerArray3(AnswerArray, chosenSmallBox);
    AnswerArray = updateAnswerArray4(AnswerArray, chosenSmallBox);

    trace ("AnswerArray  [" + AnswerArray + "]");
    trace ("Horizontal[" + HorizontalNumber + "][" + Horizontal[HorizontalNumber] + "]");
    trace ("Vertical  [" + VerticalNumber + "][" + Vertical[VerticalNumber] + "]");
    trace ("Block     [" + BlockNumber + "][" + Block[BlockNumber] + "]");
    
    QuanOfPossibleValues = findQuanOfPossibleValues(AnswerArray, GridSize);
    trace ("QuanOfPossibleValues = " + QuanOfPossibleValues);
    /*
    QuanOfPossibleHorizontalValue = findQuanOfPossibleValues(Horizontal, HorizontalNumber);
    QuanOfPossibleVerticalValue = findQuanOfPossibleValues(Vertical, VerticalNumber);
    QuanOfPossibleBlockValue = findQuanOfPossibleValues(Block, BlockNumber);
    
    trace ("QuanOfPossibleHorizontalValue = " + QuanOfPossibleHorizontalValue);
    trace ("QuanOfPossibleVerticalValue = " + QuanOfPossibleVerticalValue);
    trace ("QuanOfPossibleBlockValue = " + QuanOfPossibleBlockValue);
    */
    if (QuanOfPossibleValues != 0) {
        AppropriateNumber = pickOneValue(AnswerArray, QuanOfPossibleValues);
        if (AppropriateNumber != 0) {
//            updateArrays(AppropriateNumber, HorizontalNumber, VerticalNumber, BlockNumber, chosenSmallBox);
            updateArrays(AppropriateNumber, chosenSmallBox);
            updateDisplay();
//            trace ("Solvability = " + checkSolvability());
            return AppropriateNumber;
        } else
            return 0; // error
    } else
        return 0; // error
}

_global.findHorizontalNumber = function(SmallBoxNumber:Number):Number {
//finds the horizontal row value from a small box location
    return Math.floor(SmallBoxNumber / GridSize);
}

_global.findVerticalNumber = function(SmallBoxNumber:Number):Number {
//finds the vertical column value from a small box location
    return Math.floor(SmallBoxNumber % GridSize);
}

_global.findBlockNumber = function(SmallBoxNumber:Number):Number {
//finds the block number value from a small box location
    return (Math.floor(SmallBoxNumber / (GridSize * GridSizeSqrt)) * GridSizeSqrt + Math.floor((SmallBoxNumber % GridSize) / GridSizeSqrt));
}

_global.findQuanOfPossibleValues = function(ThisArray:Array, index:Number):Number {
//    adds up all the 1's in ThisArray[index][] to find the number of possible 1-9's to choose from
    var QuanOfPossibleValues:Number = 0;
    
    if (index == GridSize) {
        for (var i:Number=0; i<ThisArray.length; i++)
            QuanOfPossibleValues = QuanOfPossibleValues + ThisArray[i];
    } else {
        for (var i:Number=0; i<ThisArray.length; i++)
            QuanOfPossibleValues = QuanOfPossibleValues + ThisArray[index][i];
    }
    return QuanOfPossibleValues;
}

_global.updateAnswerArray = function(AnswerArray:Array, HorizontalNum:Number, VerticalNum:Number, BlockNum:Number):Array {
// updates AnswerArray to reflect all the possible 1-9 values it can have
// based on the Horizontal, Vertical and Block numbers
// signifying the location in their respective arrays
//    var AnswerArray:Array = Array(GridSize);
/*
    trace (HorizontalNum + " " + VerticalNum + " " + BlockNum);
    trace ("Horizontal[" + HorizontalNum + "][" + Horizontal[HorizontalNum] + "]");
    trace ("Vertical  [" + VerticalNum + "][" + Vertical[VerticalNum] + "]");
    trace ("Block     [" + BlockNum + "][" + Block[BlockNum] + "]");
*/
    for (var i:Number=0; i<AnswerArray.length; i++) {
        if ((Horizontal[HorizontalNum][i] + Vertical[VerticalNum][i] + Block[BlockNum][i]) == 3) {
//        if (((Horizontal[HorizontalNum][i]==1) && (Vertical[VerticalNum][i] == 1)) && (Block[BlockNum][i] == 1)) {
            AnswerArray[i] = 1;
//            trace ("if   i = " + i);
        } else {
            AnswerArray[i] = 0;
//            trace ("else i = " + i);
        }
    }
    
    return AnswerArray;
}

_global.updateAnswerArray2 = function(AnswerArray:Array, chosenSmallBox:Number):Array {
// updates AnswerArray to reflect all the cumulative total possible 1-9 values it can have based
// on all the indirect Horizontal, Vertical and Block small boxes associated with chosenSmallBox

    var AnswerArray2:Array = Array(GridSize);
    var i:Number = 0;
    var j:Number = 0;
    var H_Num:Number = 0;
    var V_Num:Number = 0;
    var B_Num:Number = 0;
    
//    trace ("AnswerArray [" + AnswerArray + "]");
    
    for (i=0; i<GridSize; i++)
        AnswerArray2[i] =0;
    
//    trace ("AnswerArray2[" + AnswerArray2 + "]");
    
    H_Num = ReferenceArray[chosenSmallBox][0];
    V_Num = ReferenceArray[chosenSmallBox][1];
    B_Num = ReferenceArray[chosenSmallBox][2];

    for ( i=0; i<GridSizeSquare; i++) { // add up for each small box once only
        if (ReferenceArray[i][0] == H_Num)
            for (j=0; j<GridSize; j++)
                AnswerArray2[j] = AnswerArray2[j] + Chosen81Array[i][j];
        else if (ReferenceArray[i][1] == V_Num)
            for (j=0; j<GridSize; j++)
                AnswerArray2[j] = AnswerArray2[j] + Chosen81Array[i][j];
        else if (ReferenceArray[i][2] == B_Num)
            for (j=0; j<GridSize; j++)
                AnswerArray2[j] = AnswerArray2[j] + Chosen81Array[i][j];
    }
    
    trace ("AnswerArray2 [" + AnswerArray2 + "]");
                        
    for (i=0; i<GridSize; i++)
        if (AnswerArray2[i] != 0)
            AnswerArray[i] = AnswerArray[i] * AnswerArray2[i];
    
//    trace ("AnswerArray [" + AnswerArray + "]");
                        
    return AnswerArray;
}

_global.updateAnswerArray3 = function(AnswerArray:Array, chosenSmallBox:Number):Array {
// prevents picking a 1-9 value that has been chosen in either
// the same column in another block along the same horizontal, or
// the same row in another block along the same vertical
    var AnArray:Array = Array(GridSize);
    var i:Number = 0;
    var j:Number = 0;
    var k:Number = 0;
    var l:Number = 0;
    var temp_Num:Number = 0; // a temporary number holder
    var temp1_Num:Number = 0; // a temporary number holder
    var temp2_Num:Number = 0; // a temporary number holder
    var HV_Num:Number = 0; // a temporary number holder
    var HV1_Num:Number = 0; // a temporary number holder
    var HV2_Num:Number = 0; // a temporary number holder
    var B_RowCol:Number = 0; // a temporary number holder
    var B_RowCol1:Number = 0; // a temporary number holder
    var B_RowCol2:Number = 0; // a temporary number holder
    var tempArray:Array = Array(GridSizeSqrt); // a temporary array number holder
    var H_Num:Number = 0;
    var V_Num:Number = 0;
    var B_Num:Number = 0;
    var H1_Num:Number = 0; // first same row
    var H2_Num:Number = 0; // second same row
    var V1_Num:Number = 0; // first same column
    var V2_Num:Number = 0; // second same column
/*
    var H_Start:Number = 0; // same row start number
    var H_End:Number = 0; // same row end number
    var V_Start:Number = 0; // same column start number
    var V_End:Number = 0; // same column end number
*/
    var B_Row1:Number = 0; // 1st other block in the same vertical
    var B_Row2:Number = 0; // 2nd other block in the same vertical
    var B_Col1:Number = 0; // 1st other block in the same horizontal
    var B_Col2:Number = 0; // 2nd other block in the same horizontal
    
    AnArray = resetArray(AnArray, 1);    
    tempArray = resetArray(tempArray, 0);
    
    H_Num = ReferenceArray[chosenSmallBox][0];
    V_Num = ReferenceArray[chosenSmallBox][1];
    B_Num = ReferenceArray[chosenSmallBox][2];
    
    switch (B_Num) { // find the other blocks in the same horizontal and vertical
        case 0: B_Row1 = 3; B_Row2 = 6; B_Col1 = 1; B_Col2 = 2; break;
        case 1: B_Row1 = 4; B_Row2 = 7; B_Col1 = 0; B_Col2 = 2; break;
        case 2: B_Row1 = 5; B_Row2 = 8; B_Col1 = 0; B_Col2 = 1; break;
        case 3: B_Row1 = 0; B_Row2 = 6; B_Col1 = 4; B_Col2 = 5; break;
        case 4: B_Row1 = 1; B_Row2 = 7; B_Col1 = 3; B_Col2 = 5; break;
        case 5: B_Row1 = 2; B_Row2 = 8; B_Col1 = 3; B_Col2 = 4; break;
        case 6: B_Row1 = 0; B_Row2 = 3; B_Col1 = 7; B_Col2 = 8; break;
        case 7: B_Row1 = 1; B_Row2 = 4; B_Col1 = 6; B_Col2 = 8; break;
        case 8: B_Row1 = 2; B_Row2 = 5; B_Col1 = 6; B_Col2 = 7; break;
    } // switch (B_Num)

    for(i=0; i<2; i++) {
        if (i == 0)
            temp_Num = H_Num; // do rows first
        else
            temp_Num = V_Num; // then do columns
        
        switch (temp_Num) {
            case 0: temp1_Num = 3; temp2_Num = 6; break;
            case 1: temp1_Num = 4; temp2_Num = 7; break;
            case 2: temp1_Num = 5; temp2_Num = 8; break;
            case 3: temp1_Num = 0; temp2_Num = 6; break;
            case 4: temp1_Num = 1; temp2_Num = 7; break;
            case 5: temp1_Num = 2; temp2_Num = 8; break;
            case 6: temp1_Num = 0; temp2_Num = 3; break;
            case 7: temp1_Num = 1; temp2_Num = 4; break;
            case 8: temp1_Num = 2; temp2_Num = 5; break;
        } // switch

        if (i == 0) {
            H1_Num = temp1_Num; // 1st same row
            H2_Num = temp2_Num; // 2nd same row
        } else {
            V1_Num = temp1_Num; // 1st same column
            V2_Num = temp2_Num; // 2nd same column
        } // if
        
/*        
        if (i == 0)
            temp_Num = Math.floor(H_Num / GridSizeSqrt); // find whether start, middle or end of column
        else
            temp_Num = Math.floor(V_Num / GridSizeSqrt); // find whether start, middle or end of row
        
        switch (temp_Num) {
            case 0: temp1_Num = 0; temp2_Num = 2; break;
            case 1: temp1_Num = 3; temp2_Num = 5; break;
            case 2: temp1_Num = 6; temp2_Num = 8; break;
        } // switch
            
        if (i == 0) {
            V_Start = temp1_Num; // start number for same column
            V_End = temp2_Num; // end number for same column
        } else {
            H_Start = temp1_Num; // start number for same row
            H_End = temp2_Num; // end number for same row
        } // if
*/

    } // for i
    
    for(i=0; i<2; i++) { // do rows first then do columns
        if (i == 0) {
            HV1_Num = H1_Num;
            HV2_Num = H2_Num;
//            temp1_Num = H_Start;
//            temp2_Num = H_End;
            B_RowCol1 = B_Row1;
            B_RowCol2 = B_Row2;
        } else {
            HV1_Num = V1_Num;
            HV2_Num = V2_Num;
//            temp1_Num = V_Start;
//            temp2_Num = V_End;
            B_RowCol1 = B_Col1;
            B_RowCol2 = B_Col2;
        }
        
        for (j=0; j<2; j++) { // do the first same row/column first, then the second
            if (j == 0) {
                HV_Num = HV1_Num;
                B_RowCol = B_RowCol1;
            } else {
                HV_Num = HV2_Num;
                B_RowCol = B_RowCol2;
            }
            
            if (j == 0) {
                switch (HV_Num) {
                    case 0: tempArray[0] = 0; tempArray[1] = 1; tempArray[2] = 2; break;
                    case 1: tempArray[0] = 3; tempArray[1] = 4; tempArray[2] = 5; break;
                    case 2: tempArray[0] = 6; tempArray[1] = 7; tempArray[2] = 8; break;
                    case 3: tempArray[0] = 0; tempArray[1] = 1; tempArray[2] = 2; break;
                    case 4: tempArray[0] = 3; tempArray[1] = 4; tempArray[2] = 5; break;
                    case 5: tempArray[0] = 6; tempArray[1] = 7; tempArray[2] = 8; break;
                    case 6: tempArray[0] = 0; tempArray[1] = 1; tempArray[2] = 2; break;
                    case 7: tempArray[0] = 3; tempArray[1] = 4; tempArray[2] = 5; break;
                    case 8: tempArray[0] = 6; tempArray[1] = 7; tempArray[2] = 8; break;
                } // switch
            } else {
                switch (HV_Num) {
                    case 0: tempArray[0] = 0; tempArray[1] = 3; tempArray[2] = 6; break;
                    case 1: tempArray[0] = 1; tempArray[1] = 4; tempArray[2] = 7; break;
                    case 2: tempArray[0] = 2; tempArray[1] = 5; tempArray[2] = 8; break;
                    case 3: tempArray[0] = 0; tempArray[1] = 3; tempArray[2] = 6; break;
                    case 4: tempArray[0] = 1; tempArray[1] = 4; tempArray[2] = 7; break;
                    case 5: tempArray[0] = 2; tempArray[1] = 5; tempArray[2] = 8; break;
                    case 6: tempArray[0] = 0; tempArray[1] = 3; tempArray[2] = 6; break;
                    case 7: tempArray[0] = 1; tempArray[1] = 4; tempArray[2] = 7; break;
                    case 8: tempArray[0] = 2; tempArray[1] = 5; tempArray[2] = 8; break;
                } // switch
            } // else
                
            for (k = 0; k < GridSizeSqrt; k++) {
                temp_Num = tempArray[k];
                
                for (l=0; l<GridSize; l++) {
                    if (BoxArray[B_RowCol][temp_Num] == (l+1))
                        AnArray[l] = 0;
                        
                } // for l
                
            } // for k
            
        } // for j
        
    } // for i

    trace ("AnArray      [" + AnArray + "]");
    
    for(i=0; i<GridSize; i++)
        AnswerArray[i] = AnswerArray[i] * AnArray[i];

    trace ("AnswerArray3 [" + AnswerArray + "]");
    
    return AnswerArray;
} // updateAnswerArray3

_global.updateAnswerArray4 = function(AnswerArray:Array, chosenSmallBox:Number):Array {
// same as updateAnswerArray3 but using OneDBoxArray
    var i:Number = 0;
    var temp_Num:Number = 0;
    var iH_Num:Number = 0;
    var iV_Num:Number = 0;
    var iB_Num:Number = 0;
    var AnArray:Array = Array(GridSize);
    
    AnArray = resetArray(AnArray, 1);
    
    var H_Num:Number = findHorizontalNumber(chosenSmallBox);
    var V_Num:Number = findVerticalNumber(chosenSmallBox);
    var B_Num:Number = findBlockNumber(chosenSmallBox);

    for(i=0; i<GridSizeSquare; i++) {
        iH_Num = findHorizontalNumber(i);
        iV_Num = findVerticalNumber(i);
        iB_Num = findBlockNumber(i);
        
        if (B_Num != iB_Num) { // ignore if within the same small box
//            if(((same modulus column) && (same block row)) || ((same modulus row) && (same block col))) {
            if(((V_Num%3 == iV_Num%3) && (Math.floor(H_Num/3) == Math.floor(iH_Num/3))) || ((H_Num%3 == iH_Num%3) && (Math.floor(V_Num/3) == Math.floor(iV_Num/3)))) {
                if(OneDBoxArray[i] != 0) {
                    temp_Num = O
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Sudoku Game In Actionscript
18 Sep, 2008 - 04:47 AM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,032



Thanked: 82 times
Dream Kudos: 1175
My Contributions
THe basic layout for Soduko is something like so:

CODE
+---+---+---+
|123|456|789|
|456|789|123|
|789|123|456|
+---+---+---+
|912|345|678|
|678|912|345|
|345|678|912|
+---+---+---+
|891|234|567|
|567|891|234|
|234|567|891|
+---+---+---+


After that point you just choose a certain number of times to change out numbers, choose randomly between 1 and 9 and swap all of the numbers on the screen.

Say you got 1 and 4, all the 1s would be changed to a holder like 0 and all the 4s to 1s then all the 0s to 4s (this makes sure that you don't get more of the single number than you are supposed to have.

While this is obviously not the only way to generate a soduko, it does take a lot of the brute force out of the equation; all you have to do is randomize it. (granted after a while people will catch on to the pattern used above).

Hope that helps.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:32PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month