Recent

Author Topic: Memoriam - A memory game  (Read 1028 times)

Wesbat

  • New Member
  • *
  • Posts: 22
Memoriam - A memory game
« on: November 09, 2024, 01:14:15 pm »
Hello.

My first Lazarus project and decades since I last compiled anything in Pascal - here is a very basic game where you match up pairs of cards.

Since I am starting on this journey, I have no experience with cross platform development yet. As of now this game works on Windows only. Linux and Mac is untested and unknown.

The game keeps high scores in a file (scores.dat) in the same path as the executable, created on first run.

There is definitely room for improvement, one step at a time  :)

Edit: Forgot the link - https://codeberg.org/wesbot/memoriam

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Memoriam - A memory game
« Reply #1 on: November 09, 2024, 05:16:40 pm »
Hi, Wesbat (not the name you were given at birth, I'll warrant ;)),
I'm happy to report that on macOS 14.6.1 the game loads, runs, and looks very nice indeed. One small spot of bother; when I start the game (I haven't managed to finish a game yet 😉), the program gives a TInOutError when it tries to create a new copy of scores.dat. This is the code in question is...
Code: Pascal  [Select][+][-]
  1. procedure TScoresForm.CreateNewScoresFile();
  2. Var
  3.   ScoresFile: File of TScore;
  4.   Entry: TScore;
  5.   i: Byte;
  6. begin
  7.   If FileExists(SCORE_FILE_PATH) then Exit();
  8.   AssignFile(ScoresFile, SCORE_FILE_PATH);
  9.   Rewrite(ScoresFile, SizeOf(TScore));             // <=== CODE CRASHES HERE
  10.   Entry.Name := 'Computer';
  11.   for i := 0 to High(TopScores) do
  12.   begin
  13.       Entry.Moves := 100 + i * 10;
  14.       Entry.Clock := (i*30+5*60);
  15.       Entry.Date := Now;
  16.       Entry.Score := CalculateScore(Entry.Moves, Entry.Clock);
  17.       Write(ScoresFile, Entry);
  18.   end;
  19.   CloseFile(ScoresFile);
  20. end;
  21.  

In the interests of testing (and because it's a strangly addictive game ;)) I persevered and finished the game, whereupon the program crashed again when it tried to write a score file. This time here...
Code: Pascal  [Select][+][-]
  1. procedure TScoresForm.WriteScores();
  2. Var
  3.   ScoresFile: File of TScore;
  4.   i: Byte;
  5. begin
  6.   AssignFile(ScoresFile, SCORE_FILE_PATH);
  7.   Rewrite(ScoresFile, SizeOf(TScore));          // <=== CODE CRASHES HERE
  8.   for i := 0 to High(TopScores) do
  9.   begin
  10.       Write(ScoresFile, TopScores[i]);
  11.   end;
  12.   CloseFile(ScoresFile);
  13. end;
  14.  

Those glitches apart, it's a really great example of how one can get back in the Pascal groove after a long absence! Kudos to you :D
« Last Edit: November 09, 2024, 05:19:21 pm by carl_caulkett »
"It builds... ship it!"

Dzandaa

  • Sr. Member
  • ****
  • Posts: 393
  • From C# to Lazarus
Re: Memoriam - A memory game
« Reply #2 on: November 09, 2024, 06:39:43 pm »
Hi,

It reminds me of one of my first programs when I was learning Pascal Lazarus

B->
Regards,
Dzandaa

CM630

  • Hero Member
  • *****
  • Posts: 1197
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Memoriam - A memory game
« Reply #3 on: November 09, 2024, 07:30:05 pm »
Heh, this reminded me of an Apple II game, that I used to play some 30 years ago:
Police Artist
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

ackarwow

  • Jr. Member
  • **
  • Posts: 82
    • Andrzej Karwowski's Homepage
Re: Memoriam - A memory game
« Reply #4 on: November 09, 2024, 08:05:45 pm »
Hi @Wesbat,
I tried to test your code on Linux but with errors (unknown property Images of TImage). Perhaps my version of Lazarus is too old.
On the other hand, Dzandaa's game compiled and works perfectly :)

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Memoriam - A memory game
« Reply #5 on: November 09, 2024, 09:43:50 pm »
I tweaked the CreateNewScoresFile procedure to give more information...
Code: Pascal  [Select][+][-]
  1. procedure TScoresForm.CreateNewScoresFile();
  2. Var
  3.   ScoresFile: File of TScore;
  4.   Entry: TScore;
  5.   i: Byte;
  6. begin
  7.   try
  8.     If FileExists(SCORE_FILE_PATH) then Exit();
  9.     AssignFile(ScoresFile, SCORE_FILE_PATH);
  10.     Rewrite(ScoresFile, SizeOf(TScore));
  11.     Entry.Name := 'Computer';
  12.     for i := 0 to High(TopScores) do
  13.     begin
  14.         Entry.Moves := 100 + i * 10;
  15.         Entry.Clock := (i*30+5*60);
  16.         Entry.Date := Now;
  17.         Entry.Score := CalculateScore(Entry.Moves, Entry.Clock);
  18.         Write(ScoresFile, Entry);
  19.     end;
  20.     CloseFile(ScoresFile);
  21.   except
  22.     on E: Exception do
  23.       ShowMessage('Error: ' + E.Message + #13#10 + 'Current Directory: ' + GetCurrentDir);
  24.   end;
  25. end;
  26.  

This gave the message Error: Access denied. Current directory: /

This is because in macOS, unless otherwise specified, the default directory is / which is strictly out of bounds!

On Macs there is an additional problem whereby determining the app folder is not straightforward. I actually ran up against this issue recently, so I took the liberty of adding a unit of mine caPaths.pas to your units folder, and tweaking the TScoresForm code accordingly.

I've attached the changed code as a zip file to this message. It will add a new unit capaths.pas and will change scoresunit.pas and scoresunit.lfm; it should work equally well on macOS and Windows! I haven't tried Linux yet...
"It builds... ship it!"

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Memoriam - A memory game
« Reply #6 on: November 09, 2024, 09:53:29 pm »
I haven't managed to finish a game yet 😉

Until I realised that the matching card showed up in a different colour. That made it a bit easier. Putting {$DEFINE RELEASE_BUILD} at the start made it a bit more challenging 😉
"It builds... ship it!"

Wesbat

  • New Member
  • *
  • Posts: 22
Re: Memoriam - A memory game
« Reply #7 on: November 10, 2024, 12:00:44 am »
This gave the message Error: Access denied. Current directory: /

This is because in macOS, unless otherwise specified, the default directory is / which is strictly out of bounds!

On Macs there is an additional problem whereby determining the app folder is not straightforward. I actually ran up against this issue recently, so I took the liberty of adding a unit of mine caPaths.pas to your units folder, and tweaking the TScoresForm code accordingly.

I've attached the changed code as a zip file to this message. It will add a new unit capaths.pas and will change scoresunit.pas and scoresunit.lfm; it should work equally well on macOS and Windows! I haven't tried Linux yet...

Hi Carl. No this certainly isn't my birth name (which is Wesley)  :D

Thank you so much for not only testing, but providing a solution too. This is fantastic and a great learning experience. I will mention you as the contributor when I commit this, unless you object?

Until I realised that the matching card showed up in a different colour. That made it a bit easier. Putting {$DEFINE RELEASE_BUILD} at the start made it a bit more challenging 😉

That was my testing mechanism when running Debug builds. Guess I should have mentioned this in the first post ... Whoops

Hi,

It reminds me of one of my first programs when I was learning Pascal Lazarus

B->

Nice! I can see the similarity. He he.

Hi @Wesbat,
I tried to test your code on Linux but with errors (unknown property Images of TImage). Perhaps my version of Lazarus is too old.
On the other hand, Dzandaa's game compiled and works perfectly :)
Hi,

It reminds me of one of my first programs when I was learning Pascal Lazarus

B->

Thanks to both for trying my game. I will dust off my Debian laptop in the next few weeks to setup a secondary Pascal/Lazarus development environment. I hope to become more diligent at cross-platform testing.

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Memoriam - A memory game
« Reply #8 on: November 10, 2024, 12:12:27 am »
Dunno if it is specific to linux/gtk2/laz3.6 (or intentional) but when progressing the game the backsides of some of the cards disappeared (4 of them).

Just an empty space which can still be clicked in order to turn the card.

picture after I 'solved' the 2 other empty spaces.
« Last Edit: November 10, 2024, 12:19:31 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Memoriam - A memory game
« Reply #9 on: November 10, 2024, 12:36:37 am »
Thank you so much for not only testing, but providing a solution too. This is fantastic and a great learning experience. I will mention you as the contributor when I commit this, unless you object?

No objections, at all! Happy to help 😃
"It builds... ship it!"

Wesbat

  • New Member
  • *
  • Posts: 22
Re: Memoriam - A memory game
« Reply #10 on: November 10, 2024, 04:30:39 am »
Dunno if it is specific to linux/gtk2/laz3.6 (or intentional) but when progressing the game the backsides of some of the cards disappeared (4 of them).

Just an empty space which can still be clicked in order to turn the card.

picture after I 'solved' the 2 other empty spaces.

Thanks for reporting this bug TRon.

When run with the "Debug" build mode, the matching pair for a card is highlighted (for testing purposes of course). The image index was incremented to "highlight" the other card, except I forgot to apply a modulus so the value never wraps around to a valid image :-[

I fixed that, and you must now be enable the testing feature via the "Debug" main menu - to avoid people accidentally playing the test mode.

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Memoriam - A memory game
« Reply #11 on: November 10, 2024, 05:03:11 am »
You're most welcome Wesbat.

Sorry for being rude: hello, and welcome. Thank you for the nice game !

Thank you for the quick reply and fix (that commit/explanation made perfect sense).
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Tomi

  • Full Member
  • ***
  • Posts: 130
Re: Memoriam - A memory game
« Reply #12 on: November 10, 2024, 02:30:59 pm »
Hello, Wesbat!
Your game looks great! I like memory and other logical games similar to your application, so I will download and try it.
I also wrote a memory game many years ago for a website, but in Game Maker 5.3A and this can be found here:
http://vesszentrianon.n1.hu/jatekok/64vm.zip

 

TinyPortal © 2005-2018