# Programming Challenge: Sudoku Grid

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have one challenge, that i could not solve :( I have those grids: http://img62.imageshack.us/my.php?image=sudokubg4x4jx9.png http://img218.imageshack.us/my.php?image=sudokubg6x6ib7.jpg http://img80.imageshack.us/my.php?image=sudokubg9x9zm6.jpg And i want to make a loop that read the number from the grid[x][y] and put it in the correct position (centralized) in the grid square... Naturally we could make this: theX * cellSize... but we have the thin lines and the big lines, that influences the position... The position has to be: sudokuBoardX + (gridX * cellSize) + bigLinesOffset + thinLinesOffset; so it will be exactly in the square position, thats what i want =) So my problem is: how to calculate the lines offset so i can draw the numbers in the correct position in the grid? I have been talking to many programmer friends as i could, but i did not understand how to solve it (its my third day trying =/ )... I need a solution to use on images, not to draw the lines or stuff like that The grid have some variables: cellSize, lineInterval (number of blocks in each division, in the case of the 9 x 9 grid, the interval is 3, because it is divided in a series of 3 x 3 blocks), the big line width and the thin line width, and i have all of these in my SudokuGrid class. I just need some clues about how to calculate the line offset so i can draw the number perfectly (depending on the position in the grid). Thanks a lot!

I am using C++

##### Share on other sites
I'll assume that it's in a nested loop like this:

for (int i = 0; i < horizontalCells; i++){    for (int j = 0; j < verticalCells; i++)    {        ...    }}

Where horizontalCells and verticalCells are the number of cells hotizontally and vertically, respectively.

xTotalBigLineOffset = bigLineOffset * (1 + i / lineInterval);yTotalBigLineOffset = bigLineOffset * (1 + j / lineInterval);xTotalThinLineOffset = thinLineOffset * (i - i / lineInterval);yTotalThinLineOffset = thinLineOffset * (j - j / lineInterval);xFinal = xTotalBigLineOffset + xTotalThinLineOffset + (i * cellSize) + margin;yFinal = yTotalBigLineOffset + yTotalThinLineOffset + (j * cellSize) + margin;

Where margin is the spacing / padding between the lines and the numbers (so the numbers aren't touching the lines).

NOTE: The code like this (i / lineInterval) is meant to round down, like an int.
(integer division doesn't have any fractional component).

So if you're calculating the 3rd block on a 9x9 grid...

i = 2 (because the first block is 0, not 1)

2 / 3 = 0

(even though 2/3 is not 0 when dealing with floats or doubles, with integers it is 0.... even 0.999999999 is 0)

If ya get what I mean? :P

Good luck! :)

##### Share on other sites

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628728
• Total Posts
2984418

• 25
• 11
• 10
• 16
• 14