Hi everyone,

So I've decided to get deeper into using Object Oriented programming, this is first of many Object Oriented programming tests I'll be making for the sake of learning it...

What I created now is a TMonster class which has a few parameters, procedures ... I create 10 Ogres from this which randomly attack each other, excluding themselves and dead ogres...

Please take a look at my code , and tell me what not to do , somehow I feel I'm using parameters incorrectly...or the naming is not correct , even though it is working....

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.

Next step would be maybe to create some sorta very simple window app, where I would have multiple monster types :

Ogre = Red Box
Troll = Blue Box
Imp = Green Box
Human = Black Box

then Ogre's would be sworn enemies of Troll's and Imp's
Imp's would hate Humans
Troll's would be enemies of Humans
and Humans of each three, Ogre, Troll, Imp...

I would be able to add to window per button new monster type with specific starting position , it would move in one way...if hit's wall , or some sort of other obstruction would randomly
choose between available directions...
if enemies would go to close to each other less then one block (32x32) , they would fight till one dies...

Naturally I'm right now a bit far from this... but will start working on this immediately.... this way it's fun to learn...


Greetings
Robert