Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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!

Share this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this