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 238 239 240 241 242 243 244 245 246 247 248 249
| ---------------------------------------------------------------------
#include <vcl.h>
#include <stdlib.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
/****************************************************************************
Le principe de ce taquin repose sur la permutation de pointeurs vers char.
Le tableau Chaine contient les caractères et le tableau Taquin contient les
pointeurs vers les caractères du tableau Chaine.
Cette version a été développée à l'aide de BCB3 Pro, ce qui explique l'usage
d'un bitmap pour accélérer le graphisme et supprimer les scintillements.
****************************************************************************/
//---------------------------------------------------------------------------
// DECLARATIONS GLOBALES
//---------------------------------------------------------------------------
int Niveau = 4;
char Chaine[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXY";
char *Taquin[6][6];
char Buff[] = " ";
TPoint CaseLibre;
//---------------------------------------------------------------------------
Graphics::TBitmap *Bitmap = new Graphics::TBitmap;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Color = clGreen;
Bitmap->Canvas->Font->Size = 12;
Bitmap->Canvas->Font->Style = Bitmap->Canvas->Font->Style << fsBold;
Init();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Init()
{
int x, y;
for(y = 0; y < Niveau; y++)
{
for(x = 0; x < Niveau; x++)
{
Taquin[y][x] = &Chaine[(y * Niveau) + x];
}
}
y--; x--;
Taquin[y][x] = NULL;
CaseLibre = Point(y, x);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DrawJeu()
{
int i, j, h, w, x, y, we, he;
if(Bitmap != NULL)
{
h = ClientHeight;
w = ClientWidth;
if((Bitmap->Width != w) || (Bitmap->Height != h))
{
Bitmap->Width = w; Bitmap->Height = h;
}
TCanvas *C = Bitmap->Canvas;
C->Pen->Style = psSolid;
C->Pen->Mode = pmCopy;
C->Brush->Style = bsSolid;
C->Pen->Color = Color;
C->Brush->Color = Color;
C->Rectangle(0, 0, w, h);
for(i = 0; i < Niveau; i++)
{
y = 20 + (i * 32);
for(j = 0; j < Niveau; j++)
{
x = 20 + (j * 32);
if(Taquin[i][j] != NULL)
{
Buff[0] = *Taquin[i][j];
C->Pen->Color = clBlack;
C->Brush->Color = (TColor)0x0046A3FF; //Orange
C->Rectangle(x, y, x + 32, y + 32);
C->Pen->Color = clWhite;
C->MoveTo(x, y + 31); C->LineTo(x, y); C->LineTo(x + 31, y);
we = (32 - C->TextWidth(Buff)) / 2;
he = (32 - C->TextHeight(Buff)) / 2;
C->TextOut(x + we, y + he, Buff);
}
else
{
C->Pen->Color = clGray;
C->Brush->Color = (TColor)0x00B7DBFF; //Orange pastel
C->Rectangle(x, y, x + 32, y + 32);
}
}
}
//Effets 3D pour la fenêtre
C->Pen->Color = clBlack;
C->MoveTo(w,0);C->LineTo(0,0);C->LineTo(0,h);
C->Pen->Color = clWhite;
w--; h--;
C->MoveTo(w,0);C->LineTo(w,h);C->LineTo(0,h);
//On dessine le bitmap dans la fenêtre
Canvas->Draw(0,0, Bitmap);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
DrawJeu();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
int hw = 40 + (32 * Niveau);
ClientWidth = hw;
ClientHeight = hw;
DrawJeu();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Nouveau1Click(TObject *Sender)
{
int i, j, x, y;
char *p;
TMenuItem *M = (TMenuItem*)Sender;
M->Checked = true;
Niveau = M->Tag;
Init();
for(i = 0; i < Niveau; i++)
{
for(j = 0; j < Niveau; j++)
{
x = random(Niveau);
y = random(Niveau);
p = Taquin[i][j]; Taquin[i][j] = Taquin[y][x]; Taquin[y][x] = p;
}
}
for(i = 0; i < Niveau; i++)
{
for(j = 0; j < Niveau; j++)
{
if(Taquin[i][j] == NULL)
{
CaseLibre = Point(j,i);
FormResize(this);
return;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Quitter1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete Bitmap;
Bitmap = NULL;
Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Glisser(int X, int Y)
{
int s;
char *p;
if((X != CaseLibre.x) || (Y != CaseLibre.y)) // si l'abs de la coordonnée sur laquellese trouve la souris est diff de l'abs de la case vide
// ou de même pour Y
{
if(CaseLibre.x == X) // si on se trouve sur la même ligne que la case vide
{
s = Y - CaseLibre.y; // calcul de la différence entre les deux ordonnés
s = s / abs(s); // on prend sa valeur absolue pour avoir la vraie distance qui sépare les deux cases
p = Taquin[CaseLibre.y][X]; ?
while(CaseLibre.y != Y) // tant que l'ord de la case libre n'est pas = à l'ordonnée sur lequel se trouve la souris
{
Taquin[CaseLibre.y][X] = Taquin[CaseLibre.y + s][X]; //?
CaseLibre.y = CaseLibre.y + s; //?
}
Taquin[CaseLibre.y][X] = p; //?
DrawJeu();
}
else
{
if(CaseLibre.y == Y) // si on se trouve sur la meme colonne que la case vide
{
s = X - CaseLibre.x;
s = s / abs(s);
p = Taquin[Y][CaseLibre.x];
while(CaseLibre.x != X)
{
Taquin[Y][CaseLibre.x] = Taquin[Y][CaseLibre.x + s];
CaseLibre.x = CaseLibre.x + s;
}
Taquin[Y][CaseLibre.x] = p;
}
DrawJeu();
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
int x = X - 20;
int y = Y - 20;
if((x >= 0) && (y >= 0))
{
x = x / 32; y = y / 32;
if((x < Niveau) && (y < Niveau))
{
Glisser(x,y);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
int x = X - 20;
int y = Y - 20;
if((x >= 0) && (y >= 0))
{
x = x / 32;
y = y / 32;
if((x < Niveau) && (y < Niveau))
{
if(Taquin[y][x] != NULL)
{
Cursor = crHandPoint; return;
}
}
}
Cursor = crArrow;
}
//--------------------------------------------------------------------------- |
Partager