Code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Math,
Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Memo1: TMemo;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMonster = class
private
myname : string; // monster name
health : smallint;
myarmor : byte; // 0-255
myattack : byte; // 0-255
mytargetID : byte; // targetID
isDead : boolean;
published
property name : string // monsters name
read myname;
property life : smallint // get's life for monster
read health;
property armor : byte // get's armor for monster
read myarmor;
property attack : byte // get's attack for monster
read myattack;
property target : byte // actual target (random)
read mytargetID;
property Dead : boolean // am I dead
read isDead;
constructor Create (name : string ; life : byte ; armor : byte ; attack : byte);
procedure TargetID( id : byte);
procedure OnHit( attack : byte );
procedure Died();
end;
var
Form1: TForm1;
Ogre : array of TMonster;
implementation
{$R *.dfm}
constructor TMonster.Create(name : string ; life: Byte ; armor : byte ; attack : byte);
begin
myname := name;
health := life;
myarmor := armor;
myattack :=attack;
isDead := false; // by default alive
end;
procedure TMonster.TargetID(id: Byte);
begin
myTargetID := id;
end;
procedure TMonster.OnHit(attack: Byte);
begin
if (attack - armor) > 0 then
health := health - ( attack - armor );
// check if monster went to Himmel (heaven in germany)
Died();
end;
procedure TMonster.Died;
begin
if health <= 0 then
isDead := true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
var i : integer;
begin
// we have 10 little ogre's
SetLength(Ogre,10);
// each of them has a name , and healtpoints , armor, attack
Ogre[0] := TMonster.Create('William',100,1,1);
Ogre[1] := TMonster.Create('William 2nd',120,1,1);
Ogre[2] := TMonster.Create('Robert the Grumpy',145,2,3);
Ogre[3] := TMonster.Create('Antal the SpaceOgre',150,4,3);
Ogre[4] := TMonster.Create('Sebastian the Unstopable',160,1,4);
Ogre[5] := TMonster.Create('Blorb the Blorb',99,5,6);
Ogre[6] := TMonster.Create('Shrek',49,2,4);
Ogre[7] := TMonster.Create('Devid the Serbian Ogre',180,4,6);
Ogre[8] := TMonster.Create('Keen the Wise Ogre',167,6,9);
Ogre[9] := TMonster.Create('Ogre the Ogre',98,6,11);
StringGrid1.RowCount:=11;
StringGrid1.Cells[0,0]:='Name';
StringGrid1.Cells[1,0]:='Health';
StringGrid1.Cells[2,0]:='Armor';
StringGrid1.Cells[3,0]:='Attack';
for I := 0 to 9 do
begin
StringGrid1.Cells[0,i+1]:=Ogre[i].name;
StringGrid1.Cells[1,i+1]:=inttostr(Ogre[i].life);
StringGrid1.Cells[2,i+1]:=inttostr(Ogre[i].armor);
StringGrid1.Cells[3,i+1]:=inttostr(Ogre[i].attack);
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i,j,target: integer;
AvailableTarget : array of integer;
begin
randomize();
Memo1.Lines.Clear;
// fill array with alive monsters
SetLength(AvailableTarget,0);
for i := 0 to 9 do
begin
if Ogre[i].Dead = false then
begin
SetLength(AvailableTarget, length(AvailableTarget)+1);
AvailableTarget[High(AvailableTarget)]:=i;
end;
end;
for I := 0 to 9 do
begin
if Ogre[i].Dead = false then // can only attack if alive
begin
target := AvailableTarget[random(length(AvailableTarget))]; // target is choosen from array of alive monsters
if i <> target then // will not attack self
begin
Ogre[i].mytargetID:=target;
Ogre[target].OnHit(Ogre[i].attack);
Memo1.Lines.Add(Ogre[i].name+' attacks '+inttostr(Ogre[i].target)+' '+Ogre[Ogre[i].target].name);
end;
end;
end;
// update ogre stats
for I := 0 to 9 do
begin
StringGrid1.Cells[0,i+1]:=Ogre[i].name;
StringGrid1.Cells[1,i+1]:=inttostr(Ogre[i].life);
StringGrid1.Cells[2,i+1]:=inttostr(Ogre[i].armor);
StringGrid1.Cells[3,i+1]:=inttostr(Ogre[i].attack);
end;
// if only one target is available , means we have a winner, everyone else is dead
if length(AvailableTarget) = 1 then
begin
Timer1.Enabled:=false;
for i := 0 to 9 do
begin
if Ogre[i].isDead = false then
Memo1.Lines.Add('The Winner is : '+ Ogre[i].name);
end;
end;
end;
end.
Bookmarks