Windows Develop Bookmark and Share   
 index > Windows Forms General > game of life kinda grid...
 

game of life kinda grid...

Hi,

What do you think is the best way to represent a grid similar to ones used in cellular automaton simulation (like conway's game of life), in c#?
Use hundreds of panels (doesn't seem a very good solution)? draw gdi rectangles? How would I handle each small rectangle?

Thanks
MigrationUser 1  Saturday, January 22, 2005 12:03 PM
I'm not familiar with the examples you reference, but I have been designing a tile-based grid which your post has convinced me to develop into a user control.

The basic idea is a grid of squares that can be clicked on or accessed progamatically by tile.  here's the basic logic behind it:

Define a Tile size in pixels (say 32x32)
Define a Grid Size in tiles (say 64x64)
Define a View Size in tiles (say 9x9)
Define a CurrentPosition as a Tile Index

Create an Array of TilesObjects (min props of Index, PositionX - in tiles, PositionY - in tiles)
Create an Array of Rectanges for each in ViewSize

Size a PictureBox to ViewSize * TileSize

Have the Paint Event for the picture box draw TileSize rectangles for each tile in ViewSize

In the click event for picture box, enumerate through the Rectange Array and see if a 1 pixel rectange at mouse.position intersects the current rectange.  when it does, you found the one that the user clicked.  now you can calculate which tile that is in the underlying TileObject array by working off of current position and the location of the intersecting rectangle.

I'm going to put this into a user control with a picture box and some scroll bars.  If you want to brainstorm this some more, just post!
MigrationUser 1  Tuesday, January 25, 2005 2:13 PM
Last night I had a chance to write a proof of concept control for the above logic.  It is by no means complete (it's not even designer friendly yet; you can only instansiate it in code at runtime) but it does provide the following funcitonality:

Define a TileSize (default of 32x32 pixels)
Define a Underlying Grid of Tiles (default 9x9 tiles)
Define a Viewable Grid of Tiles (default 3x3 tiles)
Define a Upper Left Point of Viewable Grid (default 0,0)
Define an Array of ImageLists to store images for tiles

Each Tile has an ImageListIndex and an ImageIndex for the particular image in the list.

The Tiles are drawn in a PictureBox that has scroll bars attached.  
The image of each tile is drawn along with a bright border.

When the PictureBox is clicked an event (which can be handled by the parent form) is raised which contains the Tile Object the user clicked.


I plan to do more with this and make it a true control.  But in the meantime, anyone wanting to implement this themselves may find the source usefull (ignore commented code - that was from development).  Most of the code is pretty short and concise so even without documentation or comments it should be fairly readable:

http://rkimble.brinkster.net/files/tilegrid.zip

There's a test project as well as the TileGrid project.  If you modify TileGrid, you need to rebuild and then update the reference to TileGrid in the test app (just remove it and add it again).  Also the test app has almost no code but I think I caught most of the possible exceptions before I crashed.  :)  Don't get click happy though; every click adds another solid color bitmap to the imagelist using the color of the picturebox on the form.  This was purely lazyness in my test app.  :)

I'll post again when the source is updated, if anyone is interested.
MigrationUser 1  Thursday, January 27, 2005 3:56 PM

You can use google to search for other answers

Custom Search

More Threads

• How to change a (C# winform) Screen Saver's Title (friendly name) in Display Properties?
• Capitalization in VB.NET
• Browsing class members sometimes not working
• Is there a way run a http POST without WebBrowser control?
• Unable to expand child properties in PropertyGrid
• Get media position from axMediaPlayer object while playing
• Crystal Report
• Move Column in DataGrid
• Control.Invalidate()
• protection against date/time tempering