Recent

Author Topic: [solved] fishing problem ;)  (Read 2176 times)

speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #15 on: November 28, 2025, 02:59:41 am »
Unfortunately....... all i get is "Range error", no line numbers.
OK, thanks anyway.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

creaothceann

  • Sr. Member
  • ****
  • Posts: 250
Re: fishing problem ;)
« Reply #16 on: November 28, 2025, 04:26:12 pm »
Pressing 0 creates a range error in FormKeyUp because hands is defined as array [kcomp..khuman, kclubs..kspades, 1..13] of boolean.

Anyway, there are some issues with the code, imo:

- Game logic is intertwined with presentation. Consider creating a "Game : TGame" record or class that contains only the game-related things. It should be possible to use it without changes in a console program or in a GUI.
- Consider skipping the Randomize call for debugging. This allows hunting for reproducible bugs.
- Instead of using several structures (card_num, deck, etc.), consider creating the cards as their own objects, with attributes like suit and value. Then create decks, hands and books as containers that can hold any amount of cards.

I've attached a partially rewritten version.


(EDIT: books don't even have to be created as containers, they can be checked dynamically.)
« Last Edit: November 28, 2025, 04:35:00 pm by creaothceann »

speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #17 on: November 28, 2025, 10:59:29 pm »
I've attached a partially rewritten version.
Thanks, I'll study your code.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Datalore

  • New member
  • *
  • Posts: 8
Re: fishing problem ;)
« Reply #18 on: November 30, 2025, 11:52:03 pm »
I wasn't really planning to do this,

But attached you'll find the go fish game in a state i think you more or less expected it to work.
Fair warning, it has all the shortcomings that are stated in the previous post and mixes game logic and interface.

as far as i can tell it doesn't hog cpu time anymore and the range errors are gone


speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #19 on: November 30, 2025, 11:57:55 pm »
I wasn't really planning to do this,

But attached you'll find the go fish game in a state i think you more or less expected it to work.
Fair warning, it has all the shortcomings that are stated in the previous post and mixes game logic and interface.

as far as i can tell it doesn't hog cpu time anymore and the range errors are gone
Thanks, I'll check it out.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #20 on: December 01, 2025, 12:28:29 am »
i turned on the debug checks, then ran the program.

I got a range check, when the computer fished an Ace. The error pointed to line 211 (the second of the two lines below):
Code: Pascal  [Select][+][-]
  1.       Deal(Suit,a);
  2.       Hands[P, Suit, a]:=True;
Deal returns a=13 for an Ace, but Hands' third dimension is 0..12.

Fixed by changing line 95 in proc.deal() from
Code: Pascal  [Select][+][-]
  1.   if Card=0 then Card:=13;
to
Code: Pascal  [Select][+][-]
  1.   if Card=13 then Card:=0;

back to testing... :)

EDIT: I have now played two games, without any errors, and was able to close the application at the end of that; so somewhere in your changes, you fixed the "hang on close" error. :)


« Last Edit: December 01, 2025, 12:33:37 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Datalore

  • New member
  • *
  • Posts: 8
Re: fishing problem ;)
« Reply #21 on: December 01, 2025, 01:07:26 am »
Well, the fact i erred is probably due to a lot of copy paste work and a lot of search and replace.

basically i created a new form and pasted your code while trying to rewrite things to make sense for me (card games are not my thing) any errors may stem from that.

as it's your code feel free to take from the modified version what you can use.

i believe i left an error in lines 351 and 365

line 365 waits for a counter to reach 100 but there are only 52 card in the deck.

line 351 i erred in card counting.... needs to be B=1+random(12)

ending with an ace would not be great.

speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #22 on: December 01, 2025, 01:22:15 am »
Irrespective of the above, I thank you for your efforts, in trying to fix my buggy program.

Much appreciated.

cheers
S.

EDIT: BTW, both the lines you refer to are OK.
  361: b:=random(13)  picks a card (0..12) which is correct
  365: (c=100)             this is a simple counter, so the code exits the loop if it can't find a card.
« Last Edit: December 01, 2025, 01:37:06 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

jamie

  • Hero Member
  • *****
  • Posts: 7493
Re: fishing problem ;)
« Reply #23 on: December 01, 2025, 03:22:06 am »
I put this in the TFormMain.SetBooks method to replace the double line you had there.
Code: Pascal  [Select][+][-]
  1.         PBList[I].canvas.Brush.Color := Specialize IfThen<TColor>(Player=Human,$A0FFA0,$FFA0A0);//Use this
  2.         if (Player=Human) then PBList[I].Canvas.Brush.Color:=$A0FFA0;// don't need this now
  3.         if (Player=Comp) then PBList[I].Canvas.Brush.Color:=$FFA0A0;    // Don't need this either!    
  4.  

Jamie
 :D
The only true wisdom is knowing you know nothing

Datalore

  • New member
  • *
  • Posts: 8
Re: fishing problem ;)
« Reply #24 on: December 01, 2025, 09:12:57 pm »
Found a teenie tiny logic flaw in the code... game is highly addictive to play  :D

hope you don't get tired of me yet. i think this is the last bit.

when dealing cards you start with a standard deck of 52 cards, players get their cards,
the rest goes on a pile for fishing. except....... when dealing..... those cards are never taken off the set of cards.

leaving room for players to draw the same cards and causing the game to fail in peculiar ways.

changed a few lines of code to actually take away cards from the deck.

speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #25 on: December 01, 2025, 11:43:01 pm »
.. game is highly addictive to play  :D
In a couple of days I will post a (cleaned-up) OOP version of the game (with graphics) in the games subforum. All that remains is to add code to handle 3 rows of cards (at present it assumes 1 or 2 rows). :o Unfortunately, I will be on the road today, driving up to Brisbane (Aus); which is quite unfortunate.
when dealing cards you start with a standard deck of 52 cards, players get their cards,
the rest goes on a pile for fishing. except....... when dealing..... those cards are never taken off the set of cards.
The program simply increments a counter in deal() when a card is delt, it doesn't access the cards in the deck at all.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #26 on: December 05, 2025, 03:20:14 am »
I may have made some progress in working out why the program (posted in msg#1) hangs when the user tries to close it (in windows, I don't know if it happens in other OSs).

I added an extra form with a memo to receive msgs at the start and end of each procedure. This showed that the program starts an "infinite" paint cycle when either player completes a "book".

The following is an excerpt from the debug memo:
Code: [Select]
begin pbclick
 begin pbclick > request
  begin pbclick > other
  end pbclick > other
  begin pbclick > SetBooks
   in pbclick > SetBooks; a=1
   in pbclick > SetBooks; You completed a book of Ace's!
   in pbclick > SetBooks; before pb.refresh; a=1
    begin pbpaint; tag=1
    end pbpaint; tag=1
    in pbclick > SetBooks; after pb.refresh
  end pbclick > SetBooks
 end pbclick > request
 begin pbclick > ComputerMove
  begin pbclick > request
   begin pbclick > other
   end pbclick > other
   begin pbclick > SetBooks
   end pbclick > SetBooks
  end pbclick > request
 end pbclick > ComputerMove
 begin pbclick > CardCound
 end pbclick > CardCound
 begin pbclick > CardCound
 end pbclick > CardCound
 begin showcards
 end showcards
 begin pbclick > checkfin
 end pbclick > checkfin
end pbclick
begin pbpaint; tag=1
end pbpaint; tag=1
begin pbpaint; tag=1
end pbpaint; tag=1
begin pbpaint; tag=1
end pbpaint; tag=1
begin pbpaint; tag=1
end pbpaint; tag=1

procedure pbpaint is as follows:
Code: Pascal  [Select][+][-]
  1. //----------------------------------------------------------------------
  2. procedure TForm_main.pbpaint(Sender: TObject);
  3. //----------------------------------------------------------------------
  4. var
  5.   t : integer;
  6.   pb : tpaintbox;
  7.   arect : trect;
  8.   sz : tsize;
  9.   s : string;
  10.   p : tpoint;
  11. begin
  12.   if not started then exit;
  13.   application.processmessages;
  14.  
  15.   pb := tpaintbox(sender);
  16.   t := pb.tag;
  17.   dbgmsg(1,'begin pbpaint; tag='+t.tostring);
  18.  
  19.   if hands[khuman,kClubs,t] or hands[khuman,kDiamonds,t] or
  20.      hands[khuman,kHearts,t] or hands[khuman,kSpades,t] then
  21.     pb.font.color := clblack
  22.   else
  23.     pb.font.color := $a0a0a0;
  24.  
  25.   if books[t] = knone then
  26.     pb.canvas.Brush.color := pbcolr[t]
  27.   else
  28.     begin
  29.       pb.canvas.Brush.color := p_colours[books[t]];
  30.       pb.font.color := clblack
  31.     end;
  32.  
  33.   arect := rect(0,0, pb.width,pb.height);
  34.   pb.canvas.Rectangle(arect);
  35.  
  36.   s := card_long[t];
  37.   sz := pb.canvas.textextent(s);
  38.   p := point((pb.width - sz.cx) div 2, (pb.height - sz.cy) div 2);
  39.   pb.canvas.textout(p.x,p.y,s);
  40.   dbgmsg(-1,'end pbpaint; tag='+t.tostring);
  41. end;

I *don't* know if this is what is causing the program to hang, but it certainly is strange! :)

I am attaching the (updated) project.

If anyone spots something fishy, please post.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Lulu

  • Sr. Member
  • ****
  • Posts: 362
Re: fishing problem ;)
« Reply #27 on: December 05, 2025, 08:24:21 pm »
Hi, the line
Code: Pascal  [Select][+][-]
  1. Application.ProcessMessage;
inside a Paint method seam suspicious. Not sure
« Last Edit: December 05, 2025, 08:26:31 pm by Lulu »
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

tetrastes

  • Hero Member
  • *****
  • Posts: 733
Re: fishing problem ;)
« Reply #28 on: December 05, 2025, 10:11:02 pm »
I *don't* know if this is what is causing the program to hang, but it certainly is strange! :)

Yes, this is what. Here is workaround, though I don't understand, why it is needed and how it works, and why the variant without pblist array works without it. This is certainly strange indeed!  %)

Code: Pascal  [Select][+][-]
  1. procedure TForm_main.PboxPaint(Sender: TObject);
  2. //----------------------------------------------------------------------
  3. var
  4.   t : integer;
  5.   pb : tpaintbox;
  6.   arect : trect;
  7.   sz : tsize;
  8.   s : string;
  9.   p : tpoint;
  10. begin
  11.   if not started then exit;
  12.  
  13.   pb := tpaintbox(sender);
  14.   t := pb.tag;
  15.  
  16.   if books[t] = knone then
  17.   begin
  18.     pb.canvas.Brush.color := pbcolr[t];
  19.  
  20.     if hands[khuman,kClubs,t] or hands[khuman,kDiamonds,t] or
  21.        hands[khuman,kHearts,t] or hands[khuman,kSpades,t] then
  22.       pb.font.color := clblack
  23.     else
  24.       pb.font.color := $a0a0a0;
  25.   end
  26.   else
  27.     begin
  28.       pb.canvas.Brush.color := p_colours[books[t]];
  29.       pb.font.color := clblack
  30.     end;
  31.  
  32.   arect := rect(0,0, pb.width,pb.height);
  33.   pb.canvas.Rectangle(arect);
  34.  
  35.   s := card_long[t];
  36.   sz := pb.canvas.textextent(s);
  37.   p := point((pb.width - sz.cx) div 2, (pb.height - sz.cy) div 2);
  38.   pb.canvas.textout(p.x,p.y,s);
  39. end;

 
« Last Edit: December 05, 2025, 10:15:37 pm by tetrastes »

speter

  • Sr. Member
  • ****
  • Posts: 479
Re: fishing problem ;)
« Reply #29 on: December 05, 2025, 11:58:31 pm »
Thanks tetrastes,

I'll check it out.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

 

TinyPortal © 2005-2018