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
| implementation
var ColSelect, RowSelect : Integer;
premierClic, joueur1, joueur2 : Boolean;
tabDamier : array [0..9,0..9] of integer;
// Le damier en (colonne, ligne).
// Les indices vont de 0 à 9 pour correspondre au DrawGrid.
// Les valeurs possibles dans le tableau sont
// 0 : case vide
// 1 : case noire
// 2 : pion blanc
// 3 : pion noir
x1, y1, x2, y2:integer;
blanc : Boolean;
{$R *.dfm}
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var pbitmap:timage;
begin
// Dessiner le damier.
// Les pions et les cases doivent être dessinés par une procédure spéciale (AFFICHE)
// qui se servira du tableau tabDamier.
if tabDamier [acol,arow] = 0 then
begin
pbitmap:=timage.create(self);
pbitmap.picture.loadfromfile('Casevide.bmp');
drawgrid1.canvas.StretchDraw(rect,pbitmap.picture.graphic);
end;
if tabDamier [acol,arow] = 1 then
begin
pbitmap:=timage.create(self);
pbitmap.picture.loadfromfile('noirvide.bmp');
drawgrid1.canvas.StretchDraw(rect,pbitmap.picture.graphic);
end;
if tabDamier [acol,arow] = 2 then
begin
pbitmap:=timage.create(self);
pbitmap.picture.loadfromfile('pionblanc.bmp');
drawgrid1.canvas.StretchDraw(rect,pbitmap.picture.graphic);
end;
if tabDamier [acol,arow] = 3 then
begin
pbitmap:=timage.create(self);
pbitmap.picture.loadfromfile('pionfonce.bmp');
drawgrid1.canvas.StretchDraw(rect,pbitmap.picture.graphic);
end;
end;
procedure TForm1.DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
//Pourvoir déplacer les pions
//Après le premier clic, la case deviens toute noire et après le deuxième
//clic, la case devient un pion noir ou blanc.
if premierClic = true then //Premier clic
begin
x1:=(x div 41);
y1:=(y div 41);
if joueur1=false then
if (tabDamier[x1,y1] = 3) then
Abort;
if joueur2=false then
if (tabDamier[x1,y1] = 2) then //Ne fais rien comme c'est au premier joueur de jouer
Abort;
if (tabDamier[x1,y1] = 0) or (tabDamier[x1,y1] = 1) then //Ne fais rien si on clic sur une case (vide) blanche ou noir
Abort;
if tabDamier[x1,y1] = 2 then
blanc := True
else
blanc := False;
end;
if premierClic = false then //Deuxième clic
begin
x2:=(x div 41);
y2:=(y div 41);
if (tabDamier[x2,y2] <> 1) then // Ne fait rien si on essaye de déplacer un pion sur une case autre que noir
Abort;
(* if (tabDamier[x1+1,y1+1] = 2) or (tabDamier[x1-1,y1+1] = 2) then
begin
tabDamier[x1+1,y1+1]:=1; //Pour quand on passe au dessus d'un pion (image), que l'autre pion (image) se supprime
tabDamier[x2,y2]:=3; *)
// Jeu changement de couleur "physique".
if blanc then
begin
tabDamier[x1,y1] := 1;
tabDamier[x2,y2] := 2;
end
else
begin
tabDamier[x1,y1] := 1;
tabDamier[x2,y2] := 3;
end
end;
//end;
joueur2:= true; //Pour que le changement de joueur s'effectue
joueur1:= false;
premierClic := not premierClic; //Change la valeur du premier clic
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
premierClic := True;
end;
procedure TForm1.Nouvellepartie1Click(Sender: TObject);
begin
premierClic:=true;
joueur1:=true;
joueur2:=false;
// Remplissage de la première ligne du damier avec 0 ou 3
tabDamier[0,0]:=0;
tabDamier[1,0]:=3;
tabDamier[2,0]:=0;
tabDamier[3,0]:=3;
tabDamier[4,0]:=0;
tabDamier[5,0]:=3;
tabDamier[6,0]:=0;
tabDamier[7,0]:=3;
tabDamier[8,0]:=0;
tabDamier[9,0]:=3;
//Ainsi de suite pour tout le damier
// Affiche le tableau dans la grille
DrawGrid1.Repaint;
end;
end. |
Partager