javascripthtmlactionscript-3game-developmentstarling-framework

how to calculate the cells gap in a grid like Sudoku game?


this is what i'm using now and it's working

var cellSize:Number = 36;
var cellGap:Number = 4;
var row:Number;
var col:Number;

for (var a:int = 0 ; a < puzzleSTR.length ; a++)
{
        col = a % 9;
        row = Math.floor(a / 9);

    var cell:Cell = new Cell(puzzleSTR.charAt(a));
    cell.x = col * (cellSize + cellGap);
    cell.y = row * (cellSize + cellGap);                
    container.addChild(cell);
    cells.push(cell);
}

this result in 9*9 grid with 4 pixels gap between them, now i want the same result but the gap should be 8 every 3 cells horizontally and vertically, exactly like a sudoku.

i use actionscript3 but i only need the concept in any language thank you


Solution

  • Since you're positioning the elements absolutely, you'll need an extra 4px gap (in addition to the default 4px gap, equalling 8px), from the fourh col and for every subsequent col in that row. From col 7 an onwards, you'll need to add 8px, to account for both gaps.

    The same logic applies to row spacing. Note that I'm comparing with 3 and 6 to get the fourth and seventh col, as the variables are zero-based.

    var extraColPadding = col >= 6 ? 8 : col >= 3 ? 4 : 0;
    var extraRowPadding = row >= 6 ? 8 : row >= 3 ? 4 : 0;
    
    cell.x = col * (cellSize + cellGap) + extraColPadding;
    cell.y = row * (cellSize + cellGap) + extraRowPadding;