Recent

Author Topic: Memory card matching game PLEASE PLEASE HELP  (Read 2868 times)

Todor

  • Newbie
  • Posts: 5
Memory card matching game PLEASE PLEASE HELP
« on: June 08, 2018, 07:34:27 pm »
This is how code looks like,i need to remake this, to use stringgrid instead of drawgrid and to put strings into cells instead of images.
unit MainUnit;


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, ExtCtrls, Menus;

type
  TfrmMain = class(TForm)
    DrawGrid: TDrawGrid;
    MainMenu1: TMainMenu;
    itemNewGame: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure itemNewGameClick(Sender: TObject);
    procedure DrawGridDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure DrawGridSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
  end;
type
  TCellMode = (ALREADY_MATCHED, CELL_VISIBLE, CELL_INVISIBLE);

const
        MAX_IMAGES = 10;
var
  frmMain: TfrmMain;

  Images : array [0..MAX_IMAGES-1] of TImage;
  RandomPermutationArray : array [0..19] of integer;
  PartnerOf : array [0..19] of integer;
  ImageOfCell : array [0..19] of integer;
  ModeOfCell : array [0..19] of TCellMode;
  FirstCell, SecondCell : integer;
  NumberOfVisibleCells : integer;

  ImagePaths : array [0..MAX_IMAGES-1] of string

  = ('img0.bmp', 'img1.bmp', 'img2.bmp', 'img3.bmp', 'img4.bmp', 'img5.bmp',
        'img6.bmp', 'img7.bmp', 'img8.bmp', 'img9.bmp');


implementation

{$R *.dfm}

procedure RedrawCell (index : integer);
var
  Col, Row : integer;
begin
  Row := index div 5;
  Col := index mod 5;

  frmMain.DrawGridDrawCell(frmMain, Col, Row, frmMain.DrawGrid.CellRect(Col,Row), [])
end;

procedure LoadImages;
var
  i : integer;
begin
  for i := 0 to MAX_IMAGES-1 do
  begin
    Images := TImage.Create(nil);
    Images.Picture.LoadFromFile(ImagePaths)
  end
end;

procedure TfrmMain.FormCreate(Sender: TObject);
var
  i : integer;
begin
  LoadImages;

  for i := 0 to 19 do
    RandomPermutationArray := i;

  Randomize;

  itemNewGame.Click
end;

function LinearIndexOf (Row, Column : integer) : integer;
begin
  Result := 5 * Row + Column
end;

procedure RandomizeThePermutationArray;
var
  i, RandomPosition, Temp : integer;
begin
  for i := 0 to 18 do
  begin
    RandomPosition := i + Random(19 - i) + 1;

    Temp := RandomPermutationArray;
    RandomPermutationArray := RandomPermutationArray[RandomPosition];
    RandomPermutationArray[RandomPosition] := Temp
  end;
end;

procedure AssignPartnerships;
var
  i : integer;
begin
  for i := 0 to 19 do
    if i mod 2 = 0 then
      PartnerOf[RandomPermutationArray] := RandomPermutationArray[i + 1]
    else
      PartnerOf[RandomPermutationArray] := RandomPermutationArray[i - 1]
end;

procedure AssignImagesToCells;
var
  i : integer;
begin
  for i := 0 to 19 do
    ImageOfCell[RandomPermutationArray] := i div 2
end;

procedure InitializeCellModes;
var
  i : integer;
begin
  for i := 0 to 19 do
    ModeOfCell := CELL_INVISIBLE
end;

procedure TfrmMain.itemNewGameClick(Sender: TObject);
var
  i : integer;
begin
  RandomizeThePermutationArray;
  AssignPartnerships;
  AssignImagesToCells;
  InitializeCellModes;

  NumberOfVisibleCells := 0;

  for i := 0 to 19 do
    RedrawCell(i);
end;

procedure TfrmMain.DrawGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  index : integer;
begin
  index := LinearIndexOf(ARow, ACol);

  if ModeOfCell[index] = CELL_INVISIBLE then
  begin
    DrawGrid.Canvas.Brush.Color := clBlack;
    DrawGrid.Canvas.FillRect(Rect)
  end
  else if ModeOfCell[index] = ALREADY_MATCHED then
  begin
    DrawGrid.Canvas.Brush.Color := clWhite;
    DrawGrid.Canvas.FillRect(Rect)
  end
  else begin
    DrawGrid.Canvas.StretchDraw(Rect, Images[ImageOfCell[index]].Picture.Graphic);
  end
end;

procedure TfrmMain.DrawGridSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  index : integer;
begin
  index := LinearIndexOf(ARow, ACol);

  if ModeOfCell[index] = ALREADY_MATCHED then
    exit;

  if NumberOfVisibleCells = 0 then
  begin
    FirstCell := index;
    ModeOfCell[FirstCell] := CELL_VISIBLE;
    RedrawCell(FirstCell);
    inc(NumberOfVisibleCells)
  end
  else if (NumberOfVisibleCells = 1) and (FirstCell <> index) then
  begin
    SecondCell := index;

    ModeOfCell[SecondCell] := CELL_VISIBLE;
    RedrawCell(SecondCell);

    if PartnerOf[SecondCell] = FirstCell then
    begin
      Sleep(100);

      ModeOfCell[FirstCell] := ALREADY_MATCHED;
      RedrawCell(FirstCell);

      ModeOfCell[SecondCell] := ALREADY_MATCHED;
      RedrawCell(SecondCell);

      NumberOfVisibleCells := 0
    end
    else
    inc(NumberOfVisibleCells)
  end
  else begin
   if (FirstCell <> index) and (SecondCell <> index) then
   begin
     ModeOfCell[index] := CELL_VISIBLE;
     RedrawCell(index);
   end;

   if FirstCell <> index then
   begin
     ModeOfCell[FirstCell] := CELL_INVISIBLE;
     RedrawCell(FirstCell);
   end;

   if SecondCell <> index then
   begin
     ModeOfCell[SecondCell] := CELL_INVISIBLE;
     RedrawCell(SecondCell);
   end;


   FirstCell := index;

   NumberOfVisibleCells := 1
  end
end;

end.                                                   

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Memory card matching game PLEASE PLEASE HELP
« Reply #1 on: June 08, 2018, 07:53:45 pm »
Hello Todor,
Welcome to the forum.

What is the thing you don't understand about using StringGrid?

Both StringGrid and DrawGrid have OnSelectCell and OnDrawCell events, and the both have canvas.

Todor

  • Newbie
  • Posts: 5
Re: Memory card matching game PLEASE PLEASE HELP
« Reply #2 on: June 08, 2018, 08:07:18 pm »
Okey i understand it now, but i still need to use string instead of images,it can be any string A-Z, i am not very good in pascal and i need it for my school project

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Memory card matching game PLEASE PLEASE HELP
« Reply #3 on: June 08, 2018, 11:02:58 pm »
The CANVAS has this...


Canvas.TextOut(left,Top, YourStringData);

Do that after you draw something so the text will appear.
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Memory card matching game PLEASE PLEASE HELP
« Reply #4 on: June 09, 2018, 04:35:55 am »
« Last Edit: June 09, 2018, 04:38:00 am by Handoko »

Todor

  • Newbie
  • Posts: 5
Re: Memory card matching game PLEASE PLEASE HELP
« Reply #5 on: June 09, 2018, 08:53:11 am »
Thanks  :D

 

TinyPortal © 2005-2018