1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Demineur.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
NbMine = 20;
Initialisation();
}
void __fastcall TForm1::Initialisation()
{
int x, y;
PasFini = true ;
ZeroMemory(TabCase, sizeof(TabCase));
//Poser les mines aléatoirement
ZeroMemory(TabVal, sizeof(TabVal));
randomize();
for (int z=0 ; z < NbMine ; z++)
{
x = rand() % 16 ;
y = rand() % 16 ;
TabVal[x][y] = 10;
}
//Remplir le tableau de valeur
for (x=0 ; x < 16; x++)
for (y=0 ; y < 16; y++)
{
if (TabVal[x][y] >= 10)
{
if(y<=14)TabVal[x][y+1]++;
if(y>0)TabVal[x][y-1]++;
if(x<=14)TabVal[x+1][y]++;
if(x<=14 && y<=14)TabVal[x+1][y+1]++;
if(x<=14 && y>0)TabVal[x+1][y-1]++;
if(x>0)TabVal[x-1][y]++;
if(x>0 && y<=14)TabVal[x-1][y+1]++;
if(x>0 && y>0)TabVal[x-1][y-1]++;
}
}
//Rafraîchir la grille
DrawGrid1->Invalidate();
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Initialisation() ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DrawGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
//Texte en Gras
DrawGrid1->Canvas->Font->Style = DrawGrid1->Canvas->Font->Style << fsBold;
if(TabCase[ACol][ARow]==0) //Dessiner les cases cachées.
{
DrawGrid1->Canvas->Brush->Color = clBtnFace;
DrawGrid1->Canvas->FillRect(Rect);
DrawEdge(DrawGrid1->Canvas->Handle,&Rect,EDGE_RAISED,BF_RECT);
}
else //Dessiner les cases découvertes.
{
DrawGrid1->Canvas->Brush->Color = clWindow;
DrawGrid1->Canvas->FillRect(Rect);
if (TabVal[ACol][ARow] >= 10) DrawGrid1->Canvas->
TextRect(Rect, Rect.Left+7, Rect.Top+7, "M");
if(TabVal[ACol][ARow]==1) DrawGrid1->Canvas->Font->Color = clGreen;
if(TabVal[ACol][ARow]==2) DrawGrid1->Canvas->Font->Color = clBlue;
if(TabVal[ACol][ARow]>2 && TabVal[ACol][ARow]<9)
DrawGrid1->Canvas->Font->Color = clRed;
if (TabVal[ACol][ARow] >= 0 && TabVal[ACol][ARow] < 9) DrawGrid1->Canvas->
TextRect(Rect, Rect.Left+7, Rect.Top+7,
IntToStr(TabVal[ACol][ARow]));
if (TabVal[ACol][ARow] == 0 ) DrawGrid1->Canvas->
TextRect(Rect, Rect.Left+7, Rect.Top+7, "");
//Cette ligne n'écris rien, mais sans elle il y a des problémes
//pour éffacer le rectangle de focus.
}
//Effacer le rectangle de focus.
if(State.Contains(gdFocused)) DrawGrid1->Canvas->DrawFocusRect(Rect);
}
void __fastcall TForm1::DrawGrid1Click(TObject *Sender)
{
if (PasFini == false) return;
bool fin=true;
int x=DrawGrid1->Col;
int y=DrawGrid1->Row;
TabCase[x][y]=1 ;
//Tout découvrir si mine
if (TabVal[x][y] >=10)
{
for (int x=0 ; x < 16; x++)
for (int y=0 ; y < 16; y++)
TabCase[x][y]=1 ;
PasFini = false;
DrawGrid1->Invalidate();
ShowMessage("Vous avez perdu");
return;
}
//Découvrir les case vides et adjacente
if (TabVal[x][y] == 0)
{
Decouvre(x,y);
}
do
{
fin = false;
x=0;
while (x<=15)
{
y=0;
while (y<=15)
{
if (TabCase[x][y] == 2)
{
TabCase[x][y] = 1;
Decouvre(x,y);
fin = true;
}
y++;
}
x++;
}
}
while (fin);
//Rafréchir la grille
DrawGrid1->Invalidate();
//Gagné
PasFini=false;
for (int x=0 ; x < 16; x++)
for (int y=0 ; y < 16; y++)
if(TabCase[x][y]==0 && TabVal[x][y]<10) PasFini = true ;
if (PasFini == false) ShowMessage("Vous avez gagné");
}
void __fastcall TForm1::Decouvre(int x,int y)
{
if(y<=14)
{
if (TabVal[x][y+1]>0 || TabCase[x][y+1]==1) TabCase[x][y+1]=1;
else TabCase[x][y+1]=2;
}
if(y>0)
{
if (TabVal[x][y-1]>0 || TabCase[x][y-1]==1) TabCase[x][y-1]=1;
else TabCase[x][y-1]=2;
}
if(x<=14)
{
if (TabVal[x+1][y]>0 || TabCase[x+1][y]==1) TabCase[x+1][y]=1;
else TabCase[x+1][y]=2;
}
if(x<=14 && y<=14)
{
if (TabVal[x+1][y+1]>0 || TabCase[x+1][y+1]==1) TabCase[x+1][y+1]=1;
else TabCase[x+1][y+1]=2;
}
if(x<=14 && y>0)
{
if (TabVal[x+1][y-1]>0 || TabCase[x+1][y-1]==1) TabCase[x+1][y-1]=1;
else TabCase[x+1][y-1]=2;
}
if(x>0)
{
if (TabVal[x-1][y]>0 || TabCase[x-1][y]==1) TabCase[x-1][y]=1;
else TabCase[x-1][y]=2;
}
if(x>0 && y<=14)
{
if (TabVal[x-1][y+1]>0 || TabCase[x-1][y+1]==1) TabCase[x-1][y+1]=1;
else TabCase[x-1][y+1]=2;
}
if(x>0 && y>0)
{
if (TabVal[x-1][y-1]>0 || TabCase[x-1][y-1]==1) TabCase[x-1][y-1]=1;
else TabCase[x-1][y-1]=2;
}
}
void __fastcall TForm1::DrawGrid1MouseWheelDown(TObject *Sender,
TShiftState Shift, TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DrawGrid1MouseWheelUp(TObject *Sender, TShiftState Shift,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------- |
Partager