|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
The GameThis 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 ProgramThe game is a simple Windows Forms application. In the main form structure we have declared an array of public int iCols;
public int iRows;
cPiece[] piece = new cPiece[12];
At the begging of the game, we have a call to the 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 classThe 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 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;
}
//gradbrush.InterpolationColors = oGradColor;
oGraph.DrawPolygon(blackpen, tmpPoints);
oGraph.FillPolygon(piecebrush, tmpPoints, FillMode.Winding);
}
The The main form paintingAt the 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);
// Clear the screen
grPaint.FillRectangle(brushWhite, e.ClipRectangle);
// Draw the grid
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 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 ! Updates05/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.
|
||||||||||||||||||||||