Memory Game

hi i know ive asked a lot of questions regarding this topic, but the other solutions havent worked regarding this topic. i basically need to make a memory game in delphi pascal, using a drawgrid and 20 bitmap images. i know that there is already a website out there that covers this topic but as it is for a school project i cannot use the exact same code. all the other codes that i have made eithr dont load the image into the drawgrid or have problem matching teh images. Please i am actually begging you to help me with this topic.
OutOfLuckAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OutOfLuckAuthor Commented:
okay well so far i have the following code, for the moment im doing it so it outputs the name of the image file however it isnt doing that. i do not understand why it isnt loading the picture names. eventually i hope to load the pictures onto the drawgrid rather than just the game.

  frmRooms: TfrmRooms;
  Images : array [0..19] of TImage;
  fFirstCell, fSecondCell : boolean;
  ShowPicture: Integer;
  xArray: array[0..4, 0..3,1..4] of string;
  ImagePaths : array [0..19] of string

=('bathroom.bmp', 'hallway.bmp' ,
  'untitled3.bmp' , 'kitchen.bmp' , 'pantry.bmp' ,
  'study room.bmp' , 'living room.bmp' , 'dining room.bmp' ,'toilet.bmp' ,
   'House.bmp','Fbathroom.bmp', 'Fhall.bmp' , 'Fbedroom.bmp' , 'Fkitchen.bmp' ,
  'Fpantry.bmp' ,'Fstudy.bmp' , 'Fliving room.bmp' , 'Fdining room.bmp' ,
  'Ftoilet.bmp' ,'FHouse.bmp');



const
  c_name = 1;
  c_pic = 2;
  c_type = 3;
  c_status = 4;


procedure InitializeCellModes;
var
ARow, ACol: integer;
Begin
For ACol:= 0 to 4 do
For ARow:= 0 to 3 do
Begin;
xArray[ACol, ARow, c_type]:='NotVisible';
end;
end;

procedure RedrawCell;
var ACol, ARow: integer;
begin
frmRooms.DrawGrid1DrawCell(frmRooms,Acol,ARow, frmRooms.DrawGrid1.CellRect(ACol,ARow), [])
end;

procedure TfrmRooms.FormCreate(Sender: TObject);


var

begin


ClickedRow := -1;
ClickedCol := -1;
DrawGrid1.Options := DrawGrid1.Options - [goRangeSelect,goRowSelect];
fFirstCell:=True;
fSecondCell:=true;
InitializeCellModes;
ShowPicture := 0;


  xArray[0, 0, c_name] := 'bedroom';
  xArray[0, 0, c_pic] := 'Fbedroom.bmp';
  xArray[0, 0, c_type] := '';
  xArray[0, 0, c_status] := '';
  xArray[0, 1, c_name] := 'bedroom';
  xArray[0, 1, c_pic] := 'untitled3.bmp';
  xArray[0, 1, c_type] := '';
  xArray[0, 1, c_status] := '';

  xArray[0, 2, c_name] := 'bathroom';
  xArray[0, 2, c_pic] := 'Fbathroom.bmp';
  xArray[0, 2, c_type] := '';
  xArray[0, 2, c_status] := '';
  xArray[0, 3, c_name] := 'bathroom';
  xArray[0, 3, c_pic] := 'bathroom.bmp';
  xArray[0, 3, c_type] := '';
  xArray[0, 3, c_status] := '';

  xArray[1, 0, c_name] := 'hallway';
  xArray[1, 0, c_pic] := 'Fhallway.bmp';
  xArray[1, 0, c_type] := '';
  xArray[1, 0, c_status] := '';
  xArray[1, 1, c_name] := 'hallway';
  xArray[1, 1, c_pic] := 'hallway.bmp';
  xArray[1, 1, c_type] := '';
  xArray[1, 1, c_status] := '';


  xArray[1, 2, c_name] := 'kitchen';
  xArray[1, 2, c_pic] := 'Fkitchen.bmp';
  xArray[1, 2, c_type] := '';
  xArray[1, 2, c_status] := '';
  xArray[1, 3, c_name] := 'kitchen';
  xArray[1, 3, c_pic] := 'kitchen.bmp';
  xArray[1, 3, c_type] := '';
  xArray[1, 3, c_status] := '';

  xArray[2, 0, c_name] := 'pantry';
  xArray[2, 0, c_pic] := 'Fpantry.bmp';
  xArray[2, 0, c_type] := '';
  xArray[2, 0, c_status] := '';
  xArray[2, 1, c_name] := 'pantry';
  xArray[2, 1, c_pic] := 'pantry.bmp';
  xArray[2, 1, c_type] := '';
  xArray[2, 1, c_status] := '';

  xArray[2, 2, c_name] := 'study room';
  xArray[2, 2, c_pic] := 'Fstudy room.bmp';
  xArray[2, 2, c_type] := '';
  xArray[2, 2, c_status] := '';

  xArray[2, 3, c_name] := 'study room';
  xArray[2, 3, c_pic] := 'study room.bmp';
  xArray[2, 3, c_type] := '';
  xArray[2, 3, c_status] := '';

  xArray[3, 0, c_name] := 'living room';
  xArray[3, 0, c_pic] := 'Fbedroom.bmp';
  xArray[3, 0, c_type] := '';
  xArray[3, 0, c_status] := '';
  xArray[3, 1, c_name] := 'bedroom';
  xArray[3, 1, c_pic] := 'untitled3.bmp';
  xArray[3, 1, c_type] := '';
  xArray[3, 1, c_status] := '';

  xArray[3, 2, c_name] := 'dining room';
  xArray[3, 2, c_pic] := 'Fbedroom.bmp';
  xArray[3, 2, c_type] := '';
  xArray[3, 2, c_status] := '';
  xArray[3, 3, c_name] := 'bedroom';
  xArray[3, 3, c_pic] := 'untitled3.bmp';
  xArray[3, 3, c_type] := '';
  xArray[3, 3, c_status] := '';

  xArray[4, 0, c_name] := 'toilet';
  xArray[4, 0, c_pic] := 'Fbedroom.bmp';
  xArray[4, 0, c_type] := '';
  xArray[4, 0, c_status] := '';
  xArray[4, 1, c_name] := 'bedroom';
  xArray[4, 1, c_pic] := 'untitled3.bmp';
  xArray[4, 1, c_type] := '';
  xArray[4, 1, c_status] := '';

  xArray[4, 2, c_name] := 'house';
  xArray[4, 2, c_pic] := 'Fbedroom.bmp';
  xArray[4, 2, c_type] := '';
  xArray[4, 2, c_status] := '';
  xArray[4, 3, c_name] := 'house';
  xArray[4, 3, c_pic] := 'untitled3.bmp';
  xArray[4, 3, c_type] := '';
  xArray[4, 3, c_status] := '';

end;

procedure TfrmRooms.DrawGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  NewRow, NewCol : integer;
begin
NewRow := DrawGrid1.Row;
  NewCol := DrawGrid1.Col;



  if ShowPicture = 0 then
  begin
    if ((ACol= ClickedCol) and (ARow=ClickedRow))then
    xArray[ACol, ARow, c_type]:='Visible';
    RedrawCell;
   inc(ShowPicture);
   fFirstCell:= False;
  end
  else if (ShowPicture = 1) and (fFirstCell = False) then
  begin
    if ((NewCol= ClickedCol) and (NewRow=ClickedRow))then
    xArray[ACol, ARow, c_type]:='Visible';
    RedrawCell;
    fSecondCell:= False;

    if (xArray[ARow, ACol, c_name] )= (xArray[NewCol, NewRow, c_name]) then
    begin
      Sleep(100);

      xArray[ACol, ARow, c_type]:='Matched';

     xArray[NewCol, NewRow, c_type]:='Matched';
     RedrawCell;
      ShowPicture := 0
    end
    else
    inc(ShowPicture)
  end
  else begin
   if (xArray[ARow, ACol, c_name] )<> (xArray[NewCol, NewRow, c_name]) then
   begin
     xArray[NewCol, NewRow, c_type]:='Visible';
    RedrawCell;
   end;

   if fFirstCell=False then
   begin
     xArray[ACol, ARow, c_type]:='NotVisible';
     RedrawCell;
   end;

   if fSecondCell= False then
   begin
     xArray[NewCol, NewRow, c_type]:='NotVisible';
     RedrawCell;
   end;

  ShowPicture := 1
  end
end;


procedure TfrmRooms.DrawGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);

 var
  i,j : integer;
begin

DrawGrid1.Canvas.Brush.Color := clBlack;
DrawGrid1.Canvas.FillRect(Rect) ;
    if (ACol =ClickedCol) and (ARow=ClickedRow)  then
  if xArray[i, j, c_type]='NotVisible'
then
  begin
    DrawGrid1.Canvas.Brush.Color := clBlack;
    DrawGrid1.Canvas.FillRect(Rect)
  end
  else if xArray[i, j, c_type]='Matched'
then
  begin
    DrawGrid1.Canvas.Brush.Color := clRed;
    DrawGrid1.Canvas.FillRect(Rect)
  end
  else begin
    DrawGrid1.Canvas.TextOut(Rect.Left,Rect.Top,xArray[ARow, ACol, c_pic]);

  end
end;
Geert GOracle dbaCommented:
do you have to use a drawgrid ?
there are lots of other possible solutions ...
OutOfLuckAuthor Commented:
well as im using images i thought a DrawGrid would be the most suitable option.
Ioannis AnifantakisProgramming InstructorCommented:
I sent you the memory game link in a previous question of yours.

However you must focus on the subject of programming and try to understand what is going on, and why everything exists where it is.

You have to make plan in your head about the code you will write.  What parts is comprised of.

This site is about people who are stuck on something they do, and need a "push" by other people who might direct them to SOLVING their problem.

Please ask specific questions to problems you encounter.  Don't just ask us to make your homework on your behalf.  Its something I won't presonally do, and if I did I would charge for it.
Geert GOracle dbaCommented:
not completely finished ...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Contnrs, Grids, StdCtrls;

const
  BitmapCount = 8; // 4*4 grid (divide by 2)

type
  TBitmapItem = class(TObject)
  private
    bA: TBitmap;
    bB: TBitmap;
    Data: Integer;
  protected
  public
  end;

  TBitmapList = class(TObjectList)
  private
  protected
  public
  end;

  TGridItem = record
    data: integer;
    status: integer;
    bitmaptype: integer;
  end;

  TGrid = array[1..BitmapCount * 2] of TGridItem;

  TfrmMemory = class(TForm)
    dg: TDrawGrid;
    btnAssignBitmaps: TButton;
    procedure btnAssignBitmapsClick(Sender: TObject);
    procedure dgDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
      State: TGridDrawState);
  private
    fBitmaps: TBitmapList;
    Grid: TGrid;
    procedure CreateBitmaps;
  protected
    property Bitmaps: TBitmapList read fBitmaps;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  frmMemory: TfrmMemory;

implementation

{$R *.dfm}

{ TfrmMemory }

procedure TfrmMemory.btnAssignBitmapsClick(Sender: TObject);
var I, X: Integer;
begin
  for I := 1 to BitmapCount*2 do
  begin
    Grid[I].data := 0;
    Grid[I].status := 0;
  end;
  for I := 1 to BitmapCount do
  begin
    repeat
      X := Random(BitmapCount*2)+1;
    until Grid[X].status = 0;
    Grid[X].status := 100;
    Grid[X].bitmaptype := 1;
    Grid[X].data := I-1;
    repeat
      X := Random(BitmapCount*2)+1;
    until Grid[X].status = 0;
    Grid[X].status := 200;
    Grid[X].bitmaptype := 2;
    Grid[X].data := I-1;
  end;
end;

constructor TfrmMemory.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fBitmaps := TBitmapList.Create;
  CreateBitmaps;
end;

procedure TfrmMemory.CreateBitmaps;
  function CreateBitmap(x, bitmaptype: integer): TBitmap;
  const
    BitmapWidth = 100;
    BitmapHeight = 100;
  var bitmaptext: string;
  begin
    Result := TBitmap.Create;
    Result.Width := BitmapWidth;
    Result.Height := BitmapHeight;
    Result.Canvas.Brush.Color := clWhite;
    Result.Canvas.FillRect(Rect(0, 0, BitmapWidth, BitmapHeight));
    case bitmaptype of
      2: bitmaptext := IntToStr(x);
    else
      bitmaptext := Chr(Ord('A')+x -1);
    end;
    Result.Canvas.TextOut(10, 10, bitmaptext);
  end;
var
  I, J, n, W, H: Integer;
  Bi: TBitmapItem;
begin
  n := 0;
  for I := 1 to BitmapCount do
  begin
    Bi := TBitmapItem.Create;
    Bi.bA := CreateBitmap(I, 1);
    Bi.bB := CreateBitmap(I, 2);
    Bi.data := I;
    Bitmaps.Add(bi);
  end;
end;

destructor TfrmMemory.Destroy;
begin
  FreeAndNil(fBitmaps);
  inherited Destroy;
end;

procedure TfrmMemory.dgDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var X: Integer;
  item: TBitmapItem;
begin
  X := 4 * ARow + ACol+1;
  if (X >= 1) and (X <= bitmapCount * 2) then
  begin
    item := TBitmapItem(Bitmaps.Items[Grid[X].Data]);
    case Grid[X].status of
      0: ; // blank
      100: TDrawGrid(Sender).Canvas.Draw(Rect.Left, Rect.Top, item.bA);
      200: TDrawGrid(Sender).Canvas.Draw(Rect.Left, Rect.Top, item.bB);
    end;
  end;
end;

end.

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.