
The Game
This is a C# implementation of the famous Pentominos Puzzle game. The game objective is to combine the 12 pieces into the rectangle. In the easiest mode (6x10 rectangle) there are 2339 possible solutions. You must be thinking... 2339 possible solutions... easy! If you have that thought (and never had played this game before)... I CHALLENGE YOU TO FINISH THE PUZZLE IN LESS THEN 5 MINUTES! Have fun!
The Program
The game is a simple Windows Forms application. In the main form structure we have declared an array of cPiece class instances that represent the instances lying around the window and two int variables that represent the dimensions of the main rectangle.
public int iCols;
public int iRows;
cPiece[] piece = new cPiece[12];
At the begging of the game, we have a call to the InitForm() function that is responsible for the creation of the 12 pieces and resetting the time counter. The time controlled by a DateTime variable that stores the start time of the game.
for(int i=0;i<12;i++)
{
piece[i] = new cPiece();
piece[i].CreatePiece(i+1);
piece[i].SetPos(20 + ( (i%6) *80), i>=6 ? 280 : 200);
}
dtStart = DateTime.Now;
The cPiece class
The cPiece class has all the functionality need to work with the pieces around the screen. The piece structure is stored in a int 5x5 matrix. When we call the CreatePiece method of the class we populate the matrix with 0 and 1 to describe where the piece is filled and where its not filled.
public void CreatePiece(int iPieceType)
{
switch(iPieceType)
{
case 1:
iMatrix[0,0] = 0;
iMatrix[1,0] = 1;
iMatrix[2,0] = 0;
iMatrix[3,0] = 0;
iMatrix[4,0] = 0;
...
case 12:
iMatrix[0,0] = 1;
iMatrix[1,0] = 0;
iMatrix[2,0] = 0;
iMatrix[3,0] = 0;
iMatrix[4,0] = 0;
iMatrix[0,1] = 1;
iMatrix[1,1] = 0;
iMatrix[2,1] = 0;
iMatrix[3,1] = 0;
iMatrix[4,1] = 0;
iMatrix[0,2] = 1;
iMatrix[1,2] = 0;
iMatrix[2,2] = 0;
iMatrix[3,2] = 0;
iMatrix[4,2] = 0;
iMatrix[0,3] = 1;
iMatrix[1,3] = 0;
iMatrix[2,3] = 0;
iMatrix[3,3] = 0;
iMatrix[4,3] = 0;
iMatrix[0,4] = 1;
iMatrix[1,4] = 0;
iMatrix[2,4] = 0;
iMatrix[3,4] = 0;
iMatrix[4,4] = 0;
piecebrush = new SolidBrush(Color.LightGreen);
break;
}
}
If you notice in the last piece of code, we have a piecebrush variable that holds a SolidBrush instance. This variable is used in the piece painting process. When we create the pieces, we assign a different color to each one. The drawing process of the piece is done with the Draw() method, that receives an instance of the Graphics object and paints the piece in its current position.
public void Draw( Graphics oGraph)
{
Pen blackpen = new Pen(Color.Black, 2);
Point[] tmpPoints = (Point[]) thePoints.Clone();
for(int i=0;i < iNumPoints;i++)
{
tmpPoints[i].X += iPosX;
tmpPoints[i].Y += iPosY;
}
oGraph.DrawPolygon(blackpen, tmpPoints);
oGraph.FillPolygon(piecebrush, tmpPoints, FillMode.Winding);
}
The Draw() method uses a Point array that represents the points the form the piece polygon. This points are stored in the thePoints variable that is an array created dynamically in the CreatePoints method. The CreatePoints method gets a starting point on the matrix and starts a tracing process around the piece, to find the polygon vertices. The other methods available in the cPiece class are SetPos, that sets the position of the piece in the screen, RotatePiece() that rotates and shifts the matrix that describes the piece structure, and FlipPiece that flips the piece matrix. There is an extra method called Hitted that is used to check if the mouse current position is inside inside a piece.
The main form painting
At the OnPaint event, we draw the puzzle box and all the pieces in their current positions, calling the Draw function of each piece.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics grPaint = e.Graphics;
SolidBrush brushWhite = new SolidBrush(Color.White);
Pen blackpen = new Pen(Color.Black,2);
grPaint.FillRectangle(brushWhite, e.ClipRectangle);
grPaint.DrawString("Left-Click on a piece do drag or drop it",
new Font("Arial",12),
new SolidBrush(Color.Black),20,10);
grPaint.DrawString("Right-Click to rotate and flip " +
"it (some pieces doesn�t flip)",
new Font("Arial",12),
new SolidBrush(Color.Black),20,30);
for(int i=0;i < iCols;i++)
{
for(int j=0;j < iRows;j++)
{
grPaint.DrawRectangle(blackpen, 100 + (i*20), 60 + (j*20),20,20);
}
}
for(int i=0;i < 12;i++)
{
piece[i].Draw(grPaint);
}
}
The MouseDown and MouseMove event implementations of the form is where the game logic is. We have a variable called draggedpiece that holds the piece that the user is dragging. When the user clicks in a piece we assign the instance of the piece to the dragged piece variable. In the MouseMove event we use the draggedpiece variable to set the piece's new position and redraw it.
I know this article is not TOPIC SPECIFIC, but it shows a lot of things about the drawing process of C# and Windows Forms. Hope you enjoy it !
Updates
05/26/2002: I�ve corrected some resource leak problems so that users running the game on Win 9x doesn�t have any problems. The main difference is that the cursor variables are at form scope an are loaded just once. Special thanks to PJ Arens for the bug report and testing, and James T. Johnson for his great explanation about the problem.
| You must Sign In to use this message board. |
|
|
 |
|
 |
Hello Mauricio. You haven't use any license in your software, and I would like to know whether I can use your code for some educational purpose, maybe add some AI stuff to the game. If the result is not bad, I will send you the version that I modified.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hi, i was showing your program.
There day i am making puzzle like you making.
and can i ask you something?
do you have solving algoriths?
i mean, i want to know~
if you put one button, and There is display that show you whole puzzle which is moving automatically.
i am learning c and make Pentominos by c.
sorry, my english is very pool.
if you have some tip, or source to buriburs@naver.com ~ please help me~ have a good day.
hi
modified on Sunday, May 25, 2008 5:11 AM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
wasimhasan wrote: i want the address of speaker port and mouse port and the architecture of both
 
Mauricio Ritter - Brazil Sonorking now: 100.13560 MRitter
Life is a mixture of painful separations from your loved ones and joyful reunions, without those two we'd just be animals I guess. The more painful the separation, that much more wonderful will be the reunion - Nish
"Th@ langwagje is screwed! It has if's but no end if's!! Stupid php cant even do butuns on forms! VISHAUL BASICS ARE THE FUTSHURE!" - Simon Walton
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I just tried running your Pentominos game. I moved 5 pieces onto the main board when I get a dialog that says:
-------------------------- PENTOMINOSHARP.EXE - Common Language Runtime Debugging Service --------------------------
Application has generated an exception that could not be handled blah, blah, blah --------------------------
My resource meter complains about running low on GDI resources. I am running Win98.
--- CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Resource Leak ? A strange behavior in a language that has garbage colectors...
Thanks for the tip PJ... I´ll check it when I´ve some time...
Mauricio Ritter - Brazil Sonorking now: 100.13560 Trank
I've gone sending to outer space, to find another race 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
It doesn't run for me . I get a dialog that reads:
--------------------------- PentominosSharp.exe - Application Error --------------------------- The application failed to initialize properly (0xc0000135). Click on OK to terminate the application. --------------------------- OK ---------------------------
ps: did you know that hitting Control+C in WinXp copies the dialog text into the clipboard!?! Wooohhooo
anyways.
swine
Check out Aephid Photokeeper, the powerful digital photo album solution at www.aephid.com.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I don't have .net. I was just trying to run the supplied exe on my Windows Xp system. Was I doing something wrong ? :p
cheers,
swinefeaster
Check out Aephid Photokeeper, the powerful digital photo album solution at www.aephid.com.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Yes, you need to install the .NET framework to run software written in C#. It's kind of like the Java Virtual Machine, and it's called the CLR - the Common Language Runtime.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Ooops then that was just my dumbass . Unfortunately, I refuse to install .net, as I cannot risk messing up my development environment. I will have to wait until the .net stuff gets worn in a bit before jumping onto that boat.
Out of curiosity, would the end user have to install .net to run the program too? (chuckle, chuckle)
cheers,
swinefeaster
Check out Aephid Photokeeper, the powerful digital photo album solution at www.aephid.com.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
swinefeaster wrote: Unfortunately, I refuse to install .net, as I cannot risk messing up my development environment
A wise decision. I use Partition Magic and I have VC ^ under XP and .NET under W2000. I am using .NET almost exclusively at home now.
swinefeaster wrote: Out of curiosity, would the end user have to install .net to run the program too? (chuckle, chuckle)
Yes, of course. In this case, you are an end user, and installing the CLR will not mess with your dev environment, only the .NET compiler would do that. That is Microsoft's biggest hurdle with .NET, once people have the CLR they could care less how their program run from the POV of the end user, but the 22 odd meg download to make them work the first time is a pain. .NET needs a killer app, on CD, that gives everyone the CLR. I suspect it will be bundled in every program and service pack out of Microsoft from here on in.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
OUCH! I already suspect my customers will be bitchin and complaining about the extra 1.6 megs that the gdiplus dll will add to the download. 22 megs is just insane!
Check out Aephid Photokeeper, the powerful digital photo album solution at www.aephid.com.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
*grin* In essence, the CLR is part of the OS, it will most certainly come with future versions of the OS, in the same way that your code probably uses stuff provided by IE, which is also there by default. But having to sell the 22 meg on the basis of the first program that uses it is still a hassle, and as I said, Microsofts next challenge. I reckon they will do an AOL and put a CD in every mailbox.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Is there already the Final version??? Where??? Please post a link!
[ITA] Tozzi ha ragione: Gaia si sta liberando di noi. [ENG] Tozzi is right: Gaia is getting rid of us.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
 | Update  Mauricio Ritter | 15:48 22 Mar '02 |
|
|
 |
 | Wow  James T. Johnson | 20:57 16 Mar '02 |
|
 |
Another fun game 
My only problem with it is the flickering; which you should be able to eliminate if you use a double buffering technique to draw the gameboard (first draw to the Graphics object for a memory bitmap, then draw that bitmap on the Graphics object representing the window). Only a minor annoyance though 
James
Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | Re: Wow  Mauricio Ritter | 2:08 17 Mar '02 |
|
|
 |
 | Re: Wow  Nish [BusterBoy] | 14:07 17 Mar '02 |
|
 |
Mauricio Ritter wrote: My latest article:
Gosh! I really should have patented that one 
Nish
My miniputt high is now 29 I do not think I can improve on that My temperament won't hold
www.busterboy.org
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | Re: Wow  Mauricio Ritter | 15:56 17 Mar '02 |
|
|
 |
|
|