They have unique names and they are all global variables. I didn't originally post code because it is long and uses lot of objects and I hoped someone would be familiar with this specific behavior, but as I see I was wrong, so here it is (with added comments for you; it is a chess):
Form 1:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
// Images: Image1 - one big image in the back, serves to show the board
// pieces are images above above the board, named as acronyms (e.g. BR# - a black rook)
// sel - serves to highlight the selected piece
// the top layer are 64 fields, named as they are in chess (A1-H8), 64x64 in size
BT1: TImage;
sel: TImage;
WP1: TImage;
WT1: TImage;
BP1: TImage;
BH1: TImage;
WP2: TImage;
WH1: TImage;
BP2: TImage;
BB1: TImage;
WP3: TImage;
WB1: TImage;
BP3: TImage;
BK: TImage;
WP4: TImage;
WK: TImage;
BP4: TImage;
BQ: TImage;
WP5: TImage;
WQ: TImage;
BP5: TImage;
BB2: TImage;
WP6: TImage;
WB2: TImage;
BP6: TImage;
BH2: TImage;
WP7: TImage;
WH2: TImage;
BP7: TImage;
BT2: TImage;
WP8: TImage;
WT2: TImage;
H7: TImage;
A2: TImage;
F2: TImage;
B2: TImage;
C2: TImage;
D2: TImage;
E2: TImage;
G2: TImage;
A1: TImage;
B1: TImage;
C1: TImage;
G7: TImage;
D1: TImage;
E1: TImage;
BP8: TImage;
Image1: TImage;
H1: TImage;
G1: TImage;
F1: TImage;
A8: TImage;
C8: TImage;
D8: TImage;
B8: TImage;
F7: TImage;
E8: TImage;
H8: TImage;
G8: TImage;
F8: TImage;
A3: TImage;
B3: TImage;
C3: TImage;
D3: TImage;
E3: TImage;
F3: TImage;
E7: TImage;
G3: TImage;
H3: TImage;
A4: TImage;
B4: TImage;
C4: TImage;
D4: TImage;
E4: TImage;
F4: TImage;
G4: TImage;
H4: TImage;
D7: TImage;
A5: TImage;
B5: TImage;
C5: TImage;
D5: TImage;
E5: TImage;
F5: TImage;
G5: TImage;
H5: TImage;
A6: TImage;
B6: TImage;
C7: TImage;
C6: TImage;
D6: TImage;
E6: TImage;
F6: TImage;
G6: TImage;
H6: TImage;
A7: TImage;
B7: TImage;
H2: TImage;
procedure A1Click(Sender: TObject);
procedure A2Click(Sender: TObject);
procedure A3Click(Sender: TObject);
procedure A4Click(Sender: TObject);
procedure A5Click(Sender: TObject);
procedure A6Click(Sender: TObject);
procedure A7Click(Sender: TObject);
procedure A8Click(Sender: TObject);
procedure B1Click(Sender: TObject);
procedure B2Click(Sender: TObject);
procedure B3Click(Sender: TObject);
procedure B4Click(Sender: TObject);
procedure B5Click(Sender: TObject);
procedure B6Click(Sender: TObject);
procedure B7Click(Sender: TObject);
procedure B8Click(Sender: TObject);
procedure C1Click(Sender: TObject);
procedure C2Click(Sender: TObject);
procedure C3Click(Sender: TObject);
procedure C4Click(Sender: TObject);
procedure C5Click(Sender: TObject);
procedure C6Click(Sender: TObject);
procedure C7Click(Sender: TObject);
procedure C8Click(Sender: TObject);
procedure D1Click(Sender: TObject);
procedure D2Click(Sender: TObject);
procedure D3Click(Sender: TObject);
procedure D4Click(Sender: TObject);
procedure D5Click(Sender: TObject);
procedure D6Click(Sender: TObject);
procedure D7Click(Sender: TObject);
procedure D8Click(Sender: TObject);
procedure E1Click(Sender: TObject);
procedure E2Click(Sender: TObject);
procedure E3Click(Sender: TObject);
procedure E4Click(Sender: TObject);
procedure E5Click(Sender: TObject);
procedure E6Click(Sender: TObject);
procedure E7Click(Sender: TObject);
procedure E8Click(Sender: TObject);
procedure F1Click(Sender: TObject);
procedure F2Click(Sender: TObject);
procedure F3Click(Sender: TObject);
procedure F4Click(Sender: TObject);
procedure F5Click(Sender: TObject);
procedure F6Click(Sender: TObject);
procedure F7Click(Sender: TObject);
procedure F8Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure G1Click(Sender: TObject);
procedure G2Click(Sender: TObject);
procedure G3Click(Sender: TObject);
procedure G4Click(Sender: TObject);
procedure G5Click(Sender: TObject);
procedure G6Click(Sender: TObject);
procedure G7Click(Sender: TObject);
procedure G8Click(Sender: TObject);
procedure H1Click(Sender: TObject);
procedure H2Click(Sender: TObject);
procedure H3Click(Sender: TObject);
procedure H4Click(Sender: TObject);
procedure H5Click(Sender: TObject);
procedure H6Click(Sender: TObject);
procedure H7Click(Sender: TObject);
procedure H8Click(Sender: TObject);
procedure vyberpanaka(x,y:Integer);
procedure posunpanaka(x,y:Integer);
procedure presunpanaka(x,y:Integer);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
a:array [1..8,1..8] of Integer;
tah, ox, oy: Integer;
rosada: Boolean;
//a - keeps track where units are. Its value is a 3-digit number, first digit signifying color, second type, third
//distinguishes between individual pieces (1-white, 2-black; 1-rook,2-tower,3-horse,4-bishop,5-king,
//6-queen) e.g - 216 - a black pawn (king and queen end with 0)
//tah - means "turn" 0,1-white, 2,3-black, 0,2-has not selected piece yet, 1,3-has selected a piece
//ox,oy - the current coordinates of the piece (x and y, the destinetion where the piece is to move, are
//used as local variables)
//rosada - means "castling"
implementation
uses
Unit2;
{$R *.lfm}
{ TForm1 }
function pathcheck(x,y:integer):Boolean;
var
i:integer;
begin
Result:=False;
//pawn
if a[ox,oy] div 10=11 then begin
if (ox=x)and(oy=7)and(y=oy-2)and(a[x,y]=0)and(a[x+1,y+1]=0)
then Result:=True;
if (ox=x)and(a[x,y]=0)and(y=oy-1) then Result:=True;
if ((x=ox-1)or(x=ox+1))and(y=oy-1)and(a[x,y]>200) then Result:=True;
end;
if a[ox,oy] div 10=21 then begin
if (ox=x)and(oy=2)and(y=oy+2)and(a[x,y]=0)and(a[x-1,y-1]=0)
then Result:=True;
if (ox=x)and(a[x,y]=0)and(y=oy+1) then Result:=True;
if ((x=ox-1)or(x=ox+1))and(y=oy+1)and(a[x,y]div 100=1) then Result:=True;
end;
//tower
if (((a[ox,oy] mod 100) div 10)=2) and ((ox=x)or(oy=y)) then begin
Result:=True;
if oy=y then begin
if ox<x then for i:=ox+1 to x-1 do if a[i,y]>0 then Result:=False;
if ox>x then for i:=x+1 to ox-1 do if a[i,y]>0 then Result:=False;
end;
if ox=x then begin
if oy<y then for i:=oy+1 to y-1 do if a[x,i]>0 then Result:=False;
if oy>y then for i:=y+1 to oy-1 do if a[x,i]>0 then Result:=False;
end;
end;
//horse
if (((a[ox,oy] mod 100) div 10)=3)and
(((abs(ox-x)=2)and(abs(oy-y)=1))or((abs(oy-y)=2)and(abs(ox-x)=1)))
then Result:=True;
//bishop
if (((a[ox,oy] mod 100) div 10)=4) and (abs(ox-x)=abs(oy-y)) then begin
Result:=True;
if (ox>x)and(oy>y) then for i:=x+1 to ox-1 do if a[i,oy-ox+i]>0
then Result:=False;
if (ox<x)and(oy>y) then for i:=ox+1 to x-1 do if a[i,oy-i+ox]>0
then Result:=False;
if (ox>x)and(oy<y) then for i:=ox-1 downto x+1 do if a[i,oy+ox-i]>0
then Result:=False;
if (ox<x)and(oy<y) then for i:=ox+1 to x-1 do if a[i,oy+i-ox]>0
then Result:=False;
end;
//king
if ((a[ox,oy] mod 100)=50)and((abs(x-ox)<=1)and(abs(y-oy)<=1))
then Result:=True;
//queen
if (((a[ox,oy] mod 100) div 10)=6) and (abs(ox-x)=abs(oy-y)) then begin
Result:=True;
if (ox>x)and(oy>y) then for i:=x+1 to ox-1 do if a[i,oy-ox+i]>0
then Result:=False;
if (ox<x)and(oy>y) then for i:=ox+1 to x-1 do if a[i,oy-i+ox]>0
then Result:=False;
if (ox>x)and(oy<y) then for i:=ox-1 downto x+1 do if a[i,oy+ox-i]>0
then Result:=False;
if (ox<x)and(oy<y) then for i:=ox+1 to x-1 do if a[i,oy+i-ox]>0
then Result:=False;
end;
if (((a[ox,oy] mod 100) div 10)=6) and ((ox=x)or(oy=y)) then begin
Result:=True;
if oy=y then begin
if ox<x then for i:=ox+1 to x-1 do if a[i,y]>0 then Result:=False;
if ox>x then for i:=x+1 to ox-1 do if a[i,y]>0 then Result:=False;
end;
if ox=x then begin
if oy<y then for i:=oy+1 to y-1 do if a[x,i]>0 then Result:=False;
if oy>y then for i:=y+1 to oy-1 do if a[x,i]>0 then Result:=False;
end;
end;
//cant go to place occupied by friendly piece
if ((a[x,y] div 100=1) and (tah=1)) or ((a[x,y] div 100=2) and (tah=3))
then Result:=False;
//castling (not exactly as it should be according to rules; a working PH script)
if (((a[x,y]mod 100=21)or(a[x,y]mod 100=22))and(a[ox,oy]mod 100=50))or
(((a[ox,oy]mod 100=21)or(a[ox,oy]mod 100=22))and(a[x,y]mod 100=50))
and((ox=x)or(oy=y))and(a[x,y]div 100=a[ox,oy]div 100) then
begin
Result:=True;
if oy=y then begin
if ox<x then for i:=ox+1 to x-1 do if a[i,y]>0 then Result:=False;
if ox>x then for i:=x+1 to ox-1 do if a[i,y]>0 then Result:=False;
end;
if ox=x then begin
if oy<y then for i:=oy+1 to y-1 do if a[x,i]>0 then Result:=False;
if oy>y then for i:=y+1 to oy-1 do if a[x,i]>0 then Result:=False;
end;
if Result then rosada:=True;
end;
end;
procedure TForm1.vyberpanaka(x,y:Integer); //means "picking of a piece"
begin
if ((tah=0) and (a[x,y]div 100=1))
or ((tah=2) and (a[x,y]>200)) then begin
sel.visible:=True;
sel.left:=64*(x-1);
sel.top:=64*(y-1);
ox:=x;
oy:=y;
tah:=tah+1;
end;
end;
procedure TForm1.posunpanaka(x,y:Integer); //means "move a piece"
begin
//deselect if the original field is clicked again
if (x=ox) and (y=oy) then begin sel.visible:=false; tah:=tah-1; end else
if pathcheck(x,y) then presunpanaka(x,y);
end;
procedure TForm1.presunpanaka(x,y:Integer); //synonymous meaning with^
var
p,r:Integer;
begin
if a[x,y]=0 then p:=0 else p:=1;
case a[ox,oy] of
111:begin WP1.Left:=(x-1)*64; WP1.Top:=(y-1)*64; end;
112:begin WP2.Left:=(x-1)*64; WP2.Top:=(y-1)*64; end;
113:begin WP3.Left:=(x-1)*64; WP3.Top:=(y-1)*64; end;
114:begin WP4.Left:=(x-1)*64; WP4.Top:=(y-1)*64; end;
115:begin WP5.Left:=(x-1)*64; WP5.Top:=(y-1)*64; end;
116:begin WP6.Left:=(x-1)*64; WP6.Top:=(y-1)*64; end;
117:begin WP7.Left:=(x-1)*64; WP7.Top:=(y-1)*64; end;
118:begin WP8.Left:=(x-1)*64; WP8.Top:=(y-1)*64; end;
121:begin WT1.Left:=(x-1)*64; WT1.Top:=(y-1)*64; end;
122:begin WT2.Left:=(x-1)*64; WT2.Top:=(y-1)*64; end;
131:begin WH1.Left:=(x-1)*64; WH1.Top:=(y-1)*64; end;
132:begin WH2.Left:=(x-1)*64; WH2.Top:=(y-1)*64; end;
141:begin WB1.Left:=(x-1)*64; WB1.Top:=(y-1)*64; end;
142:begin WB2.Left:=(x-1)*64; WB2.Top:=(y-1)*64; end;
150:begin WK.Left:=(x-1)*64; WK.Top:=(y-1)*64; end;
160:begin WQ.Left:=(x-1)*64; WQ.Top:=(y-1)*64; end;
211:begin BP1.Left:=(x-1)*64; BP1.Top:=(y-1)*64; end;
212:begin BP2.Left:=(x-1)*64; BP2.Top:=(y-1)*64; end;
213:begin BP3.Left:=(x-1)*64; BP3.Top:=(y-1)*64; end;
214:begin BP4.Left:=(x-1)*64; BP4.Top:=(y-1)*64; end;
215:begin BP5.Left:=(x-1)*64; BP5.Top:=(y-1)*64; end;
216:begin BP6.Left:=(x-1)*64; BP6.Top:=(y-1)*64; end;
217:begin BP7.Left:=(x-1)*64; BP7.Top:=(y-1)*64; end;
218:begin BP8.Left:=(x-1)*64; BP8.Top:=(y-1)*64; end;
221:begin BT1.Left:=(x-1)*64; BT1.Top:=(y-1)*64; end;
222:begin BT2.Left:=(x-1)*64; BT2.Top:=(y-1)*64; end;
231:begin BH1.Left:=(x-1)*64; BH1.Top:=(y-1)*64; end;
232:begin BH2.Left:=(x-1)*64; BH2.Top:=(y-1)*64; end;
241:begin BB1.Left:=(x-1)*64; BB1.Top:=(y-1)*64; end;
242:begin BB2.Left:=(x-1)*64; BB2.Top:=(y-1)*64; end;
250:begin BK.Left:=(x-1)*64; BK.Top:=(y-1)*64; end;
260:begin BQ.Left:=(x-1)*64; BQ.Top:=(y-1)*64; end;
end;
//eliminating
if (p=1)and(not rosada) then
case a[x,y] of
111:WP1.Visible:=False;
112:WP2.Visible:=False;
113:WP3.Visible:=False;
114:WP4.Visible:=False;
115:WP5.Visible:=False;
116:WP6.Visible:=False;
117:WP7.Visible:=False;
118:WP8.Visible:=False;
121:WT1.Visible:=False;
122:WT2.Visible:=False;
131:WH1.Visible:=False;
132:WH2.Visible:=False;
141:WB1.Visible:=False;
142:WB2.Visible:=False;
150:WK.Visible:=False;
160:WQ.Visible:=False;
211:BP1.Visible:=False;
212:BP2.Visible:=False;
213:BP3.Visible:=False;
214:BP4.Visible:=False;
215:BP5.Visible:=False;
216:BP6.Visible:=False;
217:BP7.Visible:=False;
218:BP8.Visible:=False;
221:BT1.Visible:=False;
222:BT2.Visible:=False;
231:BH1.Visible:=False;
232:BH2.Visible:=False;
241:BB1.Visible:=False;
242:BB2.Visible:=False;
250:BK.Visible:=False;
260:BQ.Visible:=False;
end;
if rosada then
//again PH, I know it is not supposed to work like this
case a[x,y] of
121:begin WT1.Left:=(ox-1)*64; WT1.Top:=(oy-1)*64; WK.Top:=(y-1)*64;
WK.Left:=(x-1)*64 end;
122:begin WT2.Left:=(ox-1)*64; WT2.Top:=(oy-1)*64; WK.Top:=(y-1)*64;
WK.Left:=(x-1)*64 end;
150:begin WK.Left:=(ox-1)*64; WK.Top:=(oy-1)*64;
if a[ox,oy]=121 then begin WT1.Top:=(y-1)*64;
WT1.Left:=(x-1)*64 end;
if a[ox,oy]=122 then begin WT2.Top:=(y-1)*64;
WT2.Left:=(x-1)*64 end;
end;
221:begin BT1.Left:=(ox-1)*64; BT1.Top:=(oy-1)*64; BK.Top:=(y-1)*64;
BK.Left:=(x-1)*64 end;
222:begin BT2.Left:=(ox-1)*64; BT2.Top:=(oy-1)*64; BK.Top:=(y-1)*64;
BK.Left:=(x-1)*64 end;
250:begin BK.Left:=(ox-1)*64; BK.Top:=(oy-1)*64;
if a[ox,oy]=121 then begin BT1.Top:=(y-1)*64;
BT1.Left:=(x-1)*64 end;
if a[ox,oy]=122 then begin BT2.Top:=(y-1)*64;
BT2.Left:=(x-1)*64 end;
end;
end;
sel.visible:=False;
//Form2 shows here for testing purposes, it is not used for anything yet, but the placement is where it
//should be
Form2.Showmodal;
//resolve the new arrangment of pieces and turns
if rosada then r:=a[x,y];
a[x,y]:=a[ox,oy];
if rosada then begin a[ox,oy]:=r; Rosada:=False; end else a[ox,oy]:=0;
if tah=3 then tah:=0; if tah=1 then tah:=2;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i,j:Integer;
begin
rosada:=False;
//drawing the board
for i:=0 to 7 do for j:=0 to 7 do begin
if ((i+j) mod 2=1) then Image1.Canvas.Brush.Color:=clBlack else
Image1.Canvas.Brush.Color:=clWhite;
Image1.Canvas.FillRect(i*64,j*64,(i+1)*64,(j+1)*64);
end;
//filling the array with the pieces' initial positions
for i:=1 to 8 do a[i,2]:=210+i;
for i:=1 to 8 do a[i,7]:=110+i;
a[1,1]:=221;a[2,1]:=231;a[3,1]:=241;a[4,1]:=260;a[5,1]:=250;a[6,1]:=242;
a[7,1]:=232;a[8,1]:=222;a[1,8]:=121;a[2,8]:=131;a[3,8]:=141;a[4,8]:=160;
a[5,8]:=150;a[6,8]:=142;a[7,8]:=132;a[8,8]:=122;
for i:=3 to 6 do for j:=1 to 8 do a[j,i]:=0;
//the white starts
tah:=0;
end;
//on-click actions of the fields follow, 64 identical procedures, with different parameters (only showing one for character limit)
procedure TForm1.G1Click(Sender: TObject);
begin
if (tah=0) or (tah=2) then vyberpanaka(7,1) else posunpanaka(7,1);
end;
end.
Form2:
(for now just show pieces in the color of whatever player made the move, is supposed to be used for transforming pawns once they reach the end; currently always shows white ones, unless I assign values to ox and oy in the Form1.Formcreate as said before - then it can also show black, but again, will continue showing the same every time)
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
type
{ TForm2 }
TForm2 = class(TForm)
Image1: TImage;
Image2: TImage;
Image3: TImage;
Image4: TImage;
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form2: TForm2;
implementation
uses
Unit1;
{$R *.lfm}
{ TForm2 }
procedure TForm2.FormCreate(Sender: TObject);
begin
if a[ox,oy]>200 then begin
Image1.Picture.LoadFromFile('figurky/cierna_dama.png');
Image2.Picture.LoadFromFile('figurky/cierna_veza.png');
Image3.Picture.LoadFromFile('figurky/cierny_strelec.png');
Image4.Picture.LoadFromFile('figurky/cierny_kon.png');
end else begin
Image1.Picture.LoadFromFile('figurky/biela_dama.png');
Image2.Picture.LoadFromFile('figurky/biela_veza.png');
Image3.Picture.LoadFromFile('figurky/biely_strelec.png');
Image4.Picture.LoadFromFile('figurky/biely_kon.png');
end;
end;
end.