//=========================================================================== // MINESWEEPER // Nick Gessler // // The array is declared for the Expert game, only portions of it // are used for the Beginner and Intermediate games. //=========================================================================== #include #pragma hdrstop #include "Unit1.h" #include "math.h" #include "time.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DoubleBuffered = true; } //--------------------------------------------------------------------------- //=========================================================================== // VARIABLES //=========================================================================== #define BEGINNER 0 #define INTERMEDIATE 1 #define EXPERT 2 #define CLEAR 0 #define MINE 1 #define LEFT 0 #define RIGHT 1 class clsGrid { public: int whatsHere; int whatsNear; } grid[16][30]; int mines; int row, col; int nRow, nCol; int minesNearby; int clearedSquares; //=========================================================================== // FUNCTIONS //=========================================================================== //--------------------------------------------------------------------- BEGIN void begin (void) { Form1->Color = clGray; // Set Grid to Clear for (row = 0; row < 16; row++) { for (col = 0; col < 30; col++) { grid[row][col].whatsHere = CLEAR; } } // Lay Mines switch (Form1->ComboBoxLevel->ItemIndex) { case BEGINNER: { mines = 0; while (mines < 10) { row = random(8); col = random(8); if (grid[row][col].whatsHere == CLEAR) { grid[row][col].whatsHere = MINE; mines++; } } break; } case INTERMEDIATE: { mines = 0; while (mines < 40) { row = random(16); col = random(16); if (grid[row][col].whatsHere == CLEAR) { grid[row][col].whatsHere = MINE; mines++; } } break; } case EXPERT: { mines = 0; while (mines < 99) { row = random(16); col = random(30); if (grid[row][col].whatsHere == CLEAR) { grid[row][col].whatsHere = MINE; mines++; } } break; } } // Count Nearby Mines for (row = 0; row < 16; row++) { for (col = 0; col < 30; col++) { minesNearby = 0; for (nRow = row - 1; nRow <= row + 1; nRow++) { if (nRow < 0) continue; if (nRow >= 16) continue; for (nCol = col - 1; nCol <= col + 1; nCol++) { if (nCol < 0) continue; if (nCol >= 30) continue; if (grid[nRow][nCol].whatsHere == MINE) minesNearby++; } } grid[row][col].whatsNear = minesNearby; } } } //---------------------------------------------------------------- RENDER ALL void renderAll (void) { Application->ProcessMessages(); switch (Form1->ComboBoxLevel->ItemIndex) { case BEGINNER: { Form1->PaintBox->Height = 160; Form1->PaintBox->Width = 160; for (row = 0; row < 8; row++) { for (col = 0; col < 8; col++) { if (grid[row][col].whatsHere == MINE) { Form1->PaintBox->Canvas->Brush->Color = clRed; } else { Form1->PaintBox->Canvas->Brush->Color = clAqua; } Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); Form1->PaintBox->Canvas-> TextOut(col*20+6, row*20+3, grid[row][col].whatsNear); } } break; } case INTERMEDIATE: { Form1->PaintBox->Height = 320; Form1->PaintBox->Width = 320; for (row = 0; row < 16; row++) { for (col = 0; col < 16; col++) { if (grid[row][col].whatsHere == MINE) { Form1->PaintBox->Canvas->Brush->Color = clRed; } else { Form1->PaintBox->Canvas->Brush->Color = clAqua; } Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); Form1->PaintBox->Canvas-> TextOut(col*20+6, row*20+3, grid[row][col].whatsNear); } } break; } case EXPERT: { Form1->PaintBox->Height = 320; Form1->PaintBox->Width = 600; for (row = 0; row < 16; row++) { for (col = 0; col < 30; col++) { if (grid[row][col].whatsHere == MINE) { Form1->PaintBox->Canvas->Brush->Color = clRed; } else { Form1->PaintBox->Canvas->Brush->Color = clAqua; } Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); Form1->PaintBox->Canvas-> TextOut(col*20+6, row*20+3, grid[row][col].whatsNear); } } break; } } } //---------------------------------------------------------- RENDER BEGINNING void renderBeginning (void) { clearedSquares = 0; switch (Form1->ComboBoxLevel->ItemIndex) { case BEGINNER: { Form1->PaintBox->Height = 160; Form1->PaintBox->Width = 160; for (row = 0; row < 8; row++) { for (col = 0; col < 8; col++) { Form1->PaintBox->Canvas->Brush->Color = clGray; Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); } } break; } case INTERMEDIATE: { Form1->PaintBox->Height = 320; Form1->PaintBox->Width = 320; for (row = 0; row < 16; row++) { for (col = 0; col < 16; col++) { Form1->PaintBox->Canvas->Brush->Color = clGray; Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); } } break; } case EXPERT: { Form1->PaintBox->Height = 320; Form1->PaintBox->Width = 600; for (row = 0; row < 16; row++) { for (col = 0; col < 30; col++) { Form1->PaintBox->Canvas->Brush->Color = clGray; Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); } } break; } } } //----------------------------------------------------------------- GAME OVER void youLoose (void) { Form1->Color = clRed; renderAll(); } //----------------------------------------------------------------- YOU LOOSE void youWin (void) { Form1->Color = clLime; renderAll(); } //---------------------------------------------------------------------- PLAY void play (int col, int row, int button) { switch (button) { // You guessed that it's clear case LEFT: { // You guessed wrong if (grid[row][col].whatsHere == MINE) { Form1->PaintBox->Canvas->Brush->Color = clRed; Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); } // You guessed right else { Form1->PaintBox->Canvas->Brush->Color = clAqua; clearedSquares++; } Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); if (grid[row][col].whatsNear != 0) { Form1->PaintBox->Canvas-> TextOut(col*20+6, row*20+3, grid[row][col].whatsNear); } switch (Form1->ComboBoxLevel->ItemIndex) { case BEGINNER: { if (clearedSquares == 54) youWin(); // You win break; } case INTERMEDIATE: { if (clearedSquares == 216) youWin(); // You win break; } case EXPERT: { if (clearedSquares == 381) youWin(); // You win break; } } if (grid[row][col].whatsHere == MINE) youLoose(); // You loose break; } // You guessed that it's a mine case RIGHT: { Form1->PaintBox->Canvas->Brush->Color = clYellow; Form1->PaintBox->Canvas-> Rectangle(col*20, row*20, col*20+20, row*20+20); break; } } } //=========================================================================== // EVENTS //=========================================================================== //----------------------------------------------------------------- Form Show void __fastcall TForm1::FormShow(TObject *Sender) { randomize(); begin(); renderBeginning(); } //------------------------------------------------------- ComboBoxLevelChange void __fastcall TForm1::ComboBoxLevelChange(TObject *Sender) { begin(); renderBeginning(); } //---------------------------------------------------------- ButtonBeginClick void __fastcall TForm1::ButtonBeginClick(TObject *Sender) { begin(); renderBeginning(); } //------------------------------------------------------ ButtonRenderAllClick void __fastcall TForm1::ButtonRenderAllClick(TObject *Sender) { renderAll(); } //--------------------------------------------------------- PaintBoxMouseDown void __fastcall TForm1::PaintBoxMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { play(X/20, Y/20, Button); } //------------------------------------------------ ButtonRenderBeginningClick void __fastcall TForm1::ButtonRenderBeginningClick(TObject *Sender) { renderBeginning(); } //=========================================================================== // THE END //===========================================================================