unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls;
type
TGesture = (rock, scissor, paper);
{ TForm1 }
TForm1 = class(TForm)
btnRun: TButton;
Image1: TImage;
Image2: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure btnRunClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
FBitmaps : array[TGesture] of TBitmap;
public
{ public declarations }
end;
var
Form1: TForm1;
Gesture: array[TGesture] of string = ('Rock', 'Scissor', 'Paper');
Rule: array[TGesture,TGesture] of string = (
('Rocks.', //0=0',
'Rock breaks Scissor.', // 0~1', //
'Rock wrapped by Paper.' // 0~2'
),
('Scissor broken by Rock.', // 1~0',
'Scissors.', // 1=1',
'Scissor cuts Paper.' // 1~2'
),
('Paper wraps Rock.', // 2~0',
'Paper cutten by Scissor.', // 2~1',
'Papers.' // 2=2'
)
);
implementation
uses math;
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var i : integer;
begin
FBitmaps[rock] := TBitmap.Create();
FBitmaps[rock].LoadFromFile('images\Shape\rock.bmp');
FBitmaps[scissor] := TBitmap.Create();
FBitmaps[scissor].LoadFromFile('images\Shape\scissor.bmp');
FBitmaps[paper] := TBitmap.Create();
FBitmaps[paper].LoadFromFile('images\Shape\paper.bmp');
//Image1.Picture.LoadFromFile('images\Shape\paper.bmp');
//Image2.Picture.LoadFromFile('images\Shape\scissor.bmp');
end;
function Fight(a,b:TGesture):TGesture;
begin
if ( (a=rock) and (b=paper) ) or ( (b=rock) and (a=paper) ) then
result := paper
else
result := TGesture( min(ord(a),ord(b)));
end;
procedure TForm1.btnRunClick(Sender: TObject);
var player1,player2, winner : TGesture;
begin
randomize;
player1 := TGesture( random(3));
player2 := TGesture(random(3));
Image1.Picture.Assign(FBitmaps[player1]);
Image2.Picture.Assign(FBitmaps[player2]);
label1.Caption:= Gesture[player1];
label2.Caption:= Gesture[player2];
if player1 = player2 then
begin
label3.Caption:='Equal. Try again';
label4.Caption:='';
end
else
begin
winner := Fight(player1,player2);
label3.Caption:= 'The Winner is: ' +Gesture[winner];
end;
label4.Caption:=Rule[player1,player2];
end;
end.