RE: Help with developing sudoku gui using GDI+

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Ok, here are some alternatives:

a) You are using regions on each square to perform hit-testing. This could
be done much easier. If you think about it, it is very easy to convert the
x/y mouse coordinates to indices in your array. Let's assume that your grid
is 400x400 pixels. There are 9 rows and columns. Let's also assume that your
grid is positioned on something like a panel, so your mouse coordinates are
relative to the grid: (0,0) is the left-top of your grid.
First, calculate the width and height of the rows and columns of your grid
in pixels, by dividing the grid size by the number of rows and columns. In
our case our grid width and height will both be 44.44 pixels. From there on,
simply divide the X & Y mouse coordinates by the row width and column height
and truncate the value by casting to an integer. This will get you the X
index and Y index into your array, assuming you are using a two-dimensional
array. It is something like this:

void OnMouseMove( object sender, MouseEventArgs e )
{
float colWidth = (float)this.ClientRectangle.Width / 9.0f;
int xIndex = (int)((float)e.X / colWidth);
// same for Y
}

In this way, it is not necessary for you to store view-related information
like its size in your array, which is clean anyway. Your array will only have
information like the number that is already filled in on the cell.

B) Ok, but let's look at another possible approach. You could also treat
each square in the grid as a control and place them on the form. The easy
thing about this is that hit testing is already automatically performed by
the framework. Simply subscribe to any Windows message like OnMouseOver. You
can do this by simply deriving a 'cell' control from
System.Windows.Forms.Control or from Panel. Since sudoko also has subgrids,
you can make a hierarchy of panels (3x3 main and 3x3 sub), so that you can
also outline multiple levels in the hierarchy.
The downside is that resizing the form will take a bit of work in setting
the panels locations and sizes correctly, since I don't think anchoring or
docking is going to help you here.

C) About the drawing: just draw your entire grid in one go. You can
calculate the rectangles simply in a similar fashion as I descibed in A. You
don't need to clear any rectangle, just draw them all as required, except for
the selected rectangle. It will be wise to enable double buffering on your
control.


Hope this helps,
Jelle





"devudu" wrote:

> Hi,
> I am trying to develop a sudoku solver, I have done in VB6 already(80%
> done).
> Now i want to develop it in vb.net 2005 (xpress edition) using the new
> powerfull features (mainly OOP).
>
> Those who already know how sudoku game works, I have created the big
> grid consists of 81 small cells (9X9)
>
> I am totally new to .net framework. I am not aware of full set of
> classes available to finish this project (and new to graphics too). I
> need you expertise to help me developing this small game programe.
>
>
> My questions will be maily on the windows forms (graphics related only,
> as i am done with the actual game logic). You suggestions and help are
> appriciated.
>
> Here is what I did.
>
> I used single form with out any controls (my initial work, buttons and
> menus will come later)
> And I need to draw a 9x9 cells on the form
> I used a Grid class here where i implemented the code for drawing the
> big grid. I used Rectangle Classes to create them and also Region
> classes to perform the Hit Test and stored the regions and rectangles
> in an array.
>
> I will draw them on onpaint event.
>
> Upto here is fine
>
> now i want the logic and code also for doing the hit test. When ever
> the mouse is moved over any cell, i want to highlight that cell with a
> thick border with differnt color.
>
> I am 30% done with that code also. But i feel there could be better
> code to accomplish that.
> I am doing a scanning on all the 81 regions (stored in array) when ever
> the mouse is moved on the form. I will do the hittest to all the
> regions to get the cell id and draw the rectangle on the cell.
>
> Now i need to clear the rectangl i drawn in my previous cell, and draw
> the rectangle on the new cell.
>
> I am redrawing the thick rectangle on the previous cell with form's
> back ground for clearing the previous rectangle and i am drawing the
> normal rectangle then I am drawing the new rectangle on the new
> location.
>
> Is this approach correct
>
> Thanks for your patience if at all you read the complete message :)
>
> I welcome and appriciate your suggestion/help
>
> Thanks & Regards
>
> Devender
>
>
.



Relevant Pages

  • Re: Dungeon via Particle Aggregation Algo
    ... 'Grid cell structure. ... 'Grid cell size. ... I build a 2d array based on how many cells can fit within the map: ... 'Clear the grid array. ...
    (rec.games.roguelike.development)
  • Re: Covering rectangles in 2D
    ... since it seems you only care about grid lines that can be ... crossing from a cell to an adjoining cell, ... chose for each rectangle to define a grid line, ...
    (sci.math)
  • Re: Covering rectangles in 2D
    ... insure uniqueness. ... In other words, if 2 cells share a grid line, you ... crossing from a cell to an adjoining cell, ... chose for each rectangle to define a grid line, ...
    (sci.math)
  • Re: Covering rectangles in 2D
    ... Sort the x and y bounds of each rectangle, ... associated with exactly two grid lines. ... crossing from a cell to an adjoining cell, ... Increment, decrement, or leave C unchanged accordingly. ...
    (sci.math)
  • Re: Generate a grid of cells, and give characteristics
    ... Is this a Ruby project? ... thread dealing with Array en Grid: ... I then get to have an array from 1..196 and those are divided in a grid ... these characteristics and a formula the cell ...
    (comp.lang.ruby)