unit Unit5;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TFeldInfo=record
Farbe:TColor;
Figur:Integer;
end;
TFeldArray=Array[0..7,0..7]
of TFeldInfo;
TForm5 =
class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
FFeldArray:TFeldArray;
FLaufer:Tpoint;
FTurm:Tpoint;
FSelectedFigure:Integer;
procedure MoveFromTo(vx, vy, tx, ty:Integer);
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
const
C_FeldGroesse=50;
C_Laufer=1;
C_Turm=2;
var
Form5: TForm5;
implementation
{$R *.dfm}
procedure TForm5.FormCreate(Sender: TObject);
var
x,y:Integer;
begin
ClientWidth := 8 * C_FeldGroesse ;
Clientheight := 8 * C_FeldGroesse;
For y := 0
to 7
do
for x := 0
to 7
do
if (((y
and 1)= 1 )
and ((X
AND 1)=0))
or (((y
and 1)= 0 )
and ((X
AND 1)=1))
then FFeldArray[x,y].Farbe := clBlack
else FFeldArray[x,y].Farbe := clWhite;
FLaufer.x := Random(7);
FLaufer.y := Random(7);
FFeldArray[FLaufer.x,FLaufer.y].Figur := C_Laufer;
FTurm.x := Random(7);
FTurm.y := Random(7);
while (FTurm.x = Flaufer.x)
and (FTurm.y = Flaufer.y)
do
begin
FTurm.x := Random(7);
FTurm.y := Random(7);
end;
FFeldArray[FTurm.x,FTurm.y].Figur := C_Turm;
end;
procedure TForm5.MoveFromTo(vx,vy,tx,ty:Integer);
begin
if FFeldArray[vx,vy].Figur = C_Laufer
then
begin
Flaufer.X := tx;
Flaufer.Y := ty;
end
else if FFeldArray[vx,vy].Figur = C_Turm
then
begin
FTurm.X := tx;
FTurm.Y := ty;
end;
FFeldArray[tx,ty].Figur := FFeldArray[vx,vy].Figur;
FFeldArray[vx,vy].Figur := 0;
Invalidate;
end;
procedure TForm5.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
NowSelectedFigure:Integer;
cx,cy:Integer;
begin
cx := x
DIV C_FeldGroesse;
cy := y
DIV C_FeldGroesse;
NowSelectedFigure := FFeldArray[cx,cy].Figur;
if NowSelectedFigure <> 0
then FSelectedFigure := NowSelectedFigure
else
begin
if FSelectedFigure=C_Turm
then
begin
if (cx= FTurm.X)
or (cy= FTurm.y)
then MoveFromTo(FTurm.X,FTurm.y,cx,cy);
end
else if FSelectedFigure=C_Laufer
then
if ABS(cx - Flaufer.X) = ABS(cy - Flaufer.y)
then MoveFromTo(Flaufer.X,Flaufer.y,cx,cy);
end;
end;
procedure TForm5.FormPaint(Sender: TObject);
var
x,y:Integer;
R:TRect;
begin
For y := 0
to 7
do
for x := 0
to 7
do
begin
r.Left := x * C_FeldGroesse;
r.Top := y * C_FeldGroesse;
r.Right := x * C_FeldGroesse + C_FeldGroesse;
r.Bottom := y * C_FeldGroesse + C_FeldGroesse;
canvas.Brush.Color := FFeldArray[x,y].Farbe ;
Canvas.FillRect (R);
if FFeldArray[x,y].Figur=C_turm
then
begin
Canvas.Brush.Color := clBlue;
Canvas.Ellipse(r.Left,r.Top,r.Right,r.Bottom);
end;
if FFeldArray[x,y].Figur=C_laufer
then
begin
Canvas.Brush.Color := clred;
Canvas.Ellipse(r.Left,r.Top,r.Right,r.Bottom);
end;
end;
end;
end.