Recent

Author Topic: Leap Frogs Logic Game now with Variable Size  (Read 3817 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 524
Leap Frogs Logic Game now with Variable Size
« on: January 20, 2024, 02:18:19 pm »
Here is a fixed array shape version of the Leap Frogs Game.
How could the number of frogs be made variable using a dynamic array of images of frogs, instead of shapes.
(Like the Scratch programming Version at https://scratch.mit.edu/projects/757947021/fullscreen/   )

Code: Pascal  [Select][+][-]
  1.  unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btn_reset: TButton;
  16.     success_image: TImage;
  17.     lbl_Total_moves: TLabel;
  18.     Memo1: TMemo;
  19.     shape_frog1: TShape;
  20.     shape_frog2: TShape;
  21.     shape_frog3: TShape;
  22.     shape_frog4: TShape;
  23.     shape_frog5: TShape;
  24.     shape_frog6: TShape;
  25.     shape_frog7: TShape;
  26.     shape_frog8: TShape;
  27.     shape_frog9: TShape;
  28.     procedure btn_resetClick(Sender: TObject);
  29.     procedure CloseClick(Sender: TObject);
  30.     procedure FormCreate(Sender: TObject);
  31.     procedure shapeMouseDown(Sender: TObject; Button: TMouseButton;
  32.       Shift: TShiftState; X, Y: Integer);
  33.   private
  34.  
  35.   public
  36.     procedure Restore();
  37.     procedure Horray();
  38.   end;
  39.  
  40. var
  41.   Form1: TForm1;
  42.   Moves, Pos, Direction : integer;
  43.   Postions: Array[1..9]of Integer;
  44.   frogs: Array[1..9] of TShape;
  45.  
  46.  
  47. implementation
  48.  
  49. {$R *.lfm}
  50.  
  51. { TForm1 }
  52.  
  53. procedure TForm1.CloseClick(Sender: TObject);
  54. begin
  55.   Halt
  56. end;
  57.  
  58.  
  59. procedure TForm1.FormCreate(Sender: TObject);
  60. begin
  61.   frogs[1]:=shape_frog1;
  62.   frogs[2]:=shape_frog2;
  63.   frogs[3]:=shape_frog3;
  64.   frogs[4]:=shape_frog4;
  65.   frogs[5]:=shape_frog5;
  66.   frogs[6]:=shape_frog6;
  67.   frogs[7]:=shape_frog7;
  68.   frogs[8]:=shape_frog8;
  69.   frogs[9]:=shape_frog9;
  70.   Restore
  71. end;
  72.  
  73. procedure TForm1.Restore;
  74. var i: Integer;
  75. begin
  76. for i:=1 to 9 do
  77.   begin
  78.     Postions[i]:=i;
  79.     frogs[i].Left:=70*i;
  80.   end;
  81. Moves:=0;
  82. lbl_Total_moves.Caption:= 'Moves Made = ' + IntToStr(Moves);
  83. Memo1.Visible:=True;
  84. success_image.Visible:=False;
  85. end;
  86.  
  87. procedure TForm1.Horray;
  88. begin
  89. success_image.Visible:=True
  90. end;
  91.  
  92. procedure TForm1.shapeMouseDown(Sender: TObject; Button: TMouseButton;
  93.   Shift: TShiftState; X, Y: Integer);
  94. var i,j: Integer;
  95. begin
  96.   for i:=1 to 9 do
  97.   if Sender=frogs[i] then
  98.      begin
  99.        if i<=5 then Direction:=1 else Direction:=-1;
  100.        for j:=1 to 2 do
  101.           begin
  102.              if (Postions[5]=Postions[i]+j*Direction) then
  103.                 begin
  104.                    frogs[5].Left:=frogs[5].Left-70*j*Direction;
  105.                    frogs[i].Left:=frogs[i].Left+70*j*Direction;
  106.                    Postions[i]:=Postions[i]+j*Direction;
  107.                    Postions[5]:=Postions[5]-j*Direction;
  108.                    Moves:=Moves+1;
  109.                    lbl_Total_moves.Caption:= 'Moves Made = ' + IntToStr(Moves);
  110.                 end
  111.           end;
  112.     end;
  113.     if Moves=24 then Horray;
  114.  
  115. end;
  116.  
  117. procedure TForm1.btn_resetClick(Sender: TObject);
  118. begin
  119.   Restore
  120. end;
  121.  
  122. end.
  123.  
« Last Edit: January 28, 2024, 12:31:03 am by Boleeman »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Fixed Array Leap Frogs Logic Game to Variable Size?
« Reply #1 on: January 20, 2024, 02:43:56 pm »
How could the number of frogs be made variable using a dynamic array of images of frogs, instead of shapes.

Code: Pascal  [Select][+][-]
  1. type
  2.   TFrogs = array of WhatEverYouWantItToBe;
  3.  
  4. var
  5.   frogs: TFrogs;
Watching your provided images my logic would be, quick and dirty thought:
Code: Pascal  [Select][+][-]
  1. type
  2.   TField = array of Integer; // this holds the playable field as array of integer, while integer is a TFrog Index
  3.   TFrog = record // define all you need for 1 frog
  4.     Alliance: Integer; // 0 for the green 1 for the other
  5.     Direction: Integer; // 0 looks to the right, 1 looks to left
  6.   end;
  7.   TFrogs = array of TFrog; // make above working for all frogs, TField keeps track of where what frog is and what states it got
  8.   TImages = array of TImage; // at creation, assign a TImageList that holds all possible graphics, Like ImageIndex 0 = an empty leaf, ImageIndex 1 = green frog (alliance 0) looking to left (direction 1) plus a leaf where it sit on, ImageIndex 2 = green frog looking to right sit on a leaf .... and so on
That would be my basic needed type definitions, creating them via SetLength() like I showed in the Tower or ShapeColoring thing.

Maybe if I do program such, I would do it differently, anyway, that be my start :)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Boleeman

  • Hero Member
  • *****
  • Posts: 524
Re: Fixed Array Leap Frogs Logic Game to Variable Size?
« Reply #2 on: January 28, 2024, 12:24:24 am »
Horray.

Version 2 of the Leap Frogs Games with a dynamic Array for variable sizes.

Now ... to have a choice of images:  frogs and other images.

« Last Edit: January 28, 2024, 12:30:09 am by Boleeman »

dbannon

  • Hero Member
  • *****
  • Posts: 2855
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Leap Frogs Logic Game now with Variable Size
« Reply #3 on: January 28, 2024, 10:56:18 am »
Two problems -

  • The spacing between 'frogs' messes up first time through. Press the reset button and its fine after that.
  • It is too hard for me !


Cute game !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dseligo

  • Hero Member
  • *****
  • Posts: 1254
Re: Leap Frogs Logic Game now with Variable Size
« Reply #4 on: January 28, 2024, 11:36:39 am »
  • It is too hard for me !
You just start with one color and count moves.
Red - 1 move, blue 2 moves, red 3, blue 4, red 5, and so on.
Need 195 moves for 13 frogs.

Bart

  • Hero Member
  • *****
  • Posts: 5305
    • Bart en Mariska's Webstek
Re: Leap Frogs Logic Game now with Variable Size
« Reply #5 on: January 28, 2024, 11:55:53 am »
Nice one!
Took me a while to solve for > 2 frogs.

The spacing between 'frogs' messes up first time through. Press the reset button and its fine after that.

Move the code in OnCreate to OnShow then it works properly on Windows.
On other WS possibly not, in that case use Application.QueueAsyncCall(@SomeFormMethod,0) in OnShow, so the SomeFormMethod will be called after the window is actually created and shown.

I did not really investigate why the coördinates are off though.

Suggestion:
There must be some general algorithm to solve this game?
Would be nice to implement it and replay/animate the computated solution (especially for large amount of frogs).

Some more remarks: the succes_image for me only shows "Winner" then a newline and some pixels of the text below that.
The width/height of the TImage do not match the width/height of the TImage.Picture (should be easy fix).
Notice, when you do that the image is wider and it now is partly covered by the TMemo.
(An image might also be a bit of an overkill, a TLabel could also do the job...)

The TMemo with the instructions should be read-only.

Bart

Boleeman

  • Hero Member
  • *****
  • Posts: 524
Re: Leap Frogs Logic Game now with Variable Size
« Reply #6 on: January 28, 2024, 01:26:04 pm »
Not sure why others are getting errors. I compiled on Lazarus 32bit, and ran the program on Windows 10 probably 60 times and did not get any errors. Included is an animated gif of it working.


For those who did not pick up the pattern, have Red,Blue,Red,Blue,Red, etc. always alternating in that exact order.

I was going to do KodeZwerg's autosizing type coding, but wanted to get the game working for different sizes using a dynamic array. Actually there are 2 arrays one for the frogs shapes and the other for the positions. Perhaps the positions array needs to be freed, as I only freed the frogs shape array. I have only tested this on Windows 10, compiling with Lazarus 32bit compiler. I am kinda interested to find out why others are getting errors. Also maybe got to do with different operating systems?

Attached is an animated gif showing it working. Because I knew the pattern to the solution Red,Blue,Red,Blue,Red, etc., it was not that hard for me to swap the red and blue shapes to opposite sides.

The total number of moves is (numREDShapes + 1) squared  - 1

There is also a pattern to the types of moves: slide jump slide jump jump slide jump jump jump slide etc

The jumps increase by one separated by one slide each time.
« Last Edit: January 28, 2024, 01:38:10 pm by Boleeman »

dbannon

  • Hero Member
  • *****
  • Posts: 2855
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Leap Frogs Logic Game now with Variable Size
« Reply #7 on: January 29, 2024, 12:51:49 pm »
Not sure why others are getting errors. I compiled on Lazarus 32bit, and ran the program on Windows 10 probably 60 times and did not get any errors. Included is an animated gif of it working.
Only the first run is broken. And not every time. Some uninitialized var ?


For those who did not pick up the pattern, have Red,Blue,Red,Blue,Red, etc. always alternating in that exact order.

No, its not red blue red blue etc at all ! For a 4 frog model, its B r r B B B r r r r B B B B r r r r B B B r r B
With the underlined ones being a slide, B=blue and r=red. Note the size of the groups go up, then down.  The secret, IHMO, is to work towards getting them alternating, in the middle of the game, then its obvious what to do.

Attached is an animated gif showing it working. Because I knew the pattern to the solution Red,Blue,Red,Blue,Red, etc., it was not that hard for me to swap the red and blue shapes to opposite sides.

Yes, very clever that animated gif, it allows you to demonstrate its possible and you can do it but too fast for us to see how its done !  Thaddy would be proud of you !

A great little puzzle, thanks !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Leap Frogs Logic Game now with Variable Size
« Reply #8 on: January 30, 2024, 12:51:53 am »
I've read my name so here's my contribution  :D
Just written in an extra hurry way, should be bulletproof anyway.
Extreme simplified gamelogic.
Enjoy!

// mini updated the autosizing, on a few values it is still a little buggy with the width but it not exceed the window range.

here is my game logic:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.PanelClick(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   Current,
  5.   Empty: Integer;
  6. begin
  7.   if (not (Sender is TPanel)) then
  8.     Exit;
  9.   // we increase tries no matter what clicked
  10.   Inc(FMoves);
  11.   lblMoves.Caption := 'Moves: ' + IntToStr(FMoves);
  12.   // find clicked element in array
  13.   for i := 0 to Pred(Length(FPanels)) do
  14.     if FPanels[i].Name = (Sender as TPanel).Name then
  15.       begin
  16.         Current := i;
  17.         Break;
  18.       end;
  19.   // find "white" element in array
  20.   for i := 0 to Pred(Length(FPanels)) do
  21.     if FPanels[i].Tag = 0 then
  22.       begin
  23.         Empty := i;
  24.         Break;
  25.       end;
  26.   // bad move blue
  27.   if (((Sender as TPanel).Tag = 1) and (Current > Empty)) then
  28.     Exit;
  29.   // bad move red
  30.   if (((Sender as TPanel).Tag = 2) and (Current < Empty)) then
  31.     Exit;
  32.   // bad move white
  33.   if (Sender as TPanel).Tag = 0 then
  34.     Exit;
  35.   // switch Tags
  36.   FPanels[Empty].Tag := (Sender as TPanel).Tag;
  37.   FPanels[Current].Tag := 0;
  38.   UpdateColors;
  39.   if CheckWon then
  40.     begin
  41.       lblMoves.Caption := lblMoves.Caption + ' You Won!';
  42.       RemoveClickEvent;
  43.     end;
  44. end;
« Last Edit: January 30, 2024, 03:47:40 am by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Boleeman

  • Hero Member
  • *****
  • Posts: 524
Re: Leap Frogs Logic Game now with Variable Size
« Reply #9 on: January 30, 2024, 08:45:05 am »
KodeZwerg, smart use of panels. The win logic is what I wanted to do but was struggling with when I was doing my version. Yours seems to have randomize in onCreate, but cannot work out why?


Davo (dbannon), you should see me on the large sizes after a cup of triple blended real coffee and dark chocolate.
Lucky I am not driving a sports car, other wise I'd be booked for speeding.



There is a Delphi Version at Davdata website at https://davdata.nl/math/goats.html where David has created the game where goats actually jump (but the game has only 3 levels). I tried to convert his Delphi code to Lazarus but it had some code and functions that I could not convert (actually got black parts of the screen instead of correct graphics showing as David does his own BMP format, WP would probably be able to do it as he is really good at Delphi conversions)

Would love to know how to do a jumping frogs version with frog noises. I have seen some JavaScript code that produces jumping frogs which I may try to convert when I remember where the site was. There are also various Scratch Block programming sources as well.
« Last Edit: January 30, 2024, 08:50:34 am by Boleeman »

Boleeman

  • Hero Member
  • *****
  • Posts: 524
Re: Leap Frogs Logic Game now with Variable Size
« Reply #10 on: January 30, 2024, 09:25:34 am »
Here is "the Types of Moves Simulation" for 3 levels.

dbannon

  • Hero Member
  • *****
  • Posts: 2855
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Leap Frogs Logic Game now with Variable Size
« Reply #11 on: January 30, 2024, 09:44:06 am »
...
Would love to know how to do a jumping frogs version with frog noises.
Look at the Lazarus Example "Sprites", "Shows how to move a small bitmap (a sprite) over a background image."

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Leap Frogs Logic Game now with Variable Size
« Reply #12 on: January 30, 2024, 07:26:28 pm »
KodeZwerg, smart use of panels. The win logic is what I wanted to do but was struggling with when I was doing my version. Yours seems to have randomize in onCreate, but cannot work out why?
The randomize call cost nothing, its a leftover from the pyramid thingy.
I was not really aware about the rule-set to apply, in your first image I've just read that they can jump from any position to the empty spot as long the direction fit.
Now I've read the goat rules that you can max jump over one, so here is my game logic update to that rule.
Code: Pascal  [Select][+][-]
  1.   // white cant jump :D
  2.   if (Sender as TPanel).Tag = 0 then
  3.     Exit;
  4.   // wrong direction blue
  5.   if (((Sender as TPanel).Tag = 1) and (Current > Empty)) then
  6.     Exit;
  7.   // wrong direction red
  8.   if (((Sender as TPanel).Tag = 2) and (Current < Empty)) then
  9.     Exit;
  10.   // blue can max jump over 1 other
  11.   if (((Sender as TPanel).Tag = 1) and (Current < Empty - 2)) then
  12.     Exit;
  13.   // red can max jump over 1 other
  14.   if (((Sender as TPanel).Tag = 2) and (Current > Empty + 2)) then
  15.     Exit;
What's still missing is a GameLost method. Maybe in next update.  :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Boleeman

  • Hero Member
  • *****
  • Posts: 524
Re: Leap Frogs Logic Game now with Variable Size
« Reply #13 on: January 31, 2024, 07:05:14 am »
Added the revised code.

But ...
Found a count moves error in KodeZwerg's code in which the moves goes up even though no TPanel moves, resulting in wrong total moves at win stage. Attached is an animated gif that demonstrates the error. The error seems to happen in V1 and V2 of KodeZwerg's code. Perhaps needs a if panel clicked but does not move, then do not increment the moves.

Was also wondering how list/record the types of move in each click (slide jump slide) to a memo/TEdit.
« Last Edit: January 31, 2024, 07:12:47 am by Boleeman »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Leap Frogs Logic Game now with Variable Size
« Reply #14 on: January 31, 2024, 11:40:47 am »
Added the revised code.

But ...
Found a count moves error in KodeZwerg's code in which the moves goes up even though no TPanel moves, resulting in wrong total moves at win stage. Attached is an animated gif that demonstrates the error. The error seems to happen in V1 and V2 of KodeZwerg's code. Perhaps needs a if panel clicked but does not move, then do not increment the moves.
Have you even looked at source?
If not, here is your chance to read my comment.
Code: Pascal  [Select][+][-]
  1.   // we increase tries no matter what clicked
  2.   Inc(FMoves);
  3.   lblMoves.Caption := 'Moves: ' + IntToStr(FMoves);
The only thing where I agree is my misleading prefix "Moves" while it should be named "Clicks"
To me it was logic to give first of all a penalty for the try to do something, if you not want it, place the above code under my last posted code.


Was also wondering how list/record the types of move in each click (slide jump slide) to a memo/TEdit.
If you want to keep track of done moves, in my source you have two variables, the colored called "Current" and the whitey called "Empty", they represent the position on the field/array.
So, Memo1.Lines.Add('Moved ' + IntToStr(Current) + ' to ' + IntToStr(Empty));
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018