Recent

Author Topic: BINGO application  (Read 9369 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19613
  • Glad to be alive.
Re: BINGO application
« Reply #30 on: August 18, 2024, 01:57:20 pm »
It comes a bit short, but a nice effort!
My version is almost ready, takes a few details.
Any "programmer" that knows only one programming language is not a programmer

seghele0

  • Sr. Member
  • ****
  • Posts: 276
Re: BINGO application
« Reply #31 on: August 20, 2024, 01:45:02 pm »
Have obtained the code below.
It works for the most part, but after pressing the 'Restart' button the size of the stringgrid increases.
I try to do the same thing using FormKeyDown and VK_Space as with the 'New' button, but that doesn't work after pressing 'Restart' either.
Maybe someone has a solution.
 :-[
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics,
  6.   Dialogs, StdCtrls, Grids, ExtCtrls, LCLType;
  7. type
  8.   { TForm1 }
  9.   TForm1 = class(TForm)
  10.     BtnEXIT: TButton;
  11.     BtnGenerate: TButton;
  12.     BtnRestart: TButton;
  13.     LabelNumbersText: TLabel;
  14.     Panel1: TPanel;
  15.     Panel2: TPanel;
  16.     PanelNUMBER: TPanel;
  17.     PanelStringGrid: TPanel;
  18.     PanelButtons: TPanel;
  19.     StringGrid1: TStringGrid;
  20.     procedure BtnEXITClick(Sender: TObject);
  21.     procedure BtnGenerateClick(Sender: TObject);
  22.     procedure BtnRestartClick(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  25.  
  26.    private
  27.     FUsedNumbers: array[1..75] of Boolean;
  28.     FRemainingNumbers: Integer;
  29.     procedure Generate;
  30.     procedure ResetGame;
  31.     function GenerateRandomNumber: Integer;
  32.   public
  33.   end;
  34. var
  35.   Form1: TForm1;
  36. implementation
  37. {$R *.lfm}
  38. { TForm1 }
  39. // Procuderes + Function
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42. // Initialize the game at startup
  43.   ResetGame;
  44. end;
  45.  
  46. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  47.   Shift: TShiftState);
  48. begin
  49.   case Key of
  50.        VK_F12 : BtnEXIT.Click;
  51.        VK_SPACE : //BtnGenerate.Click;
  52.   end;
  53. end;
  54.  
  55. procedure TForm1.ResetGame;
  56. var
  57. i, j: Integer;
  58. begin
  59. // Initialize the StringGrid
  60.   StringGrid1.ColCount := 15;
  61.   StringGrid1.RowCount := 5;
  62. //
  63.   StringGrid1.DefaultRowHeight := 110;
  64.   StringGrid1.DefaultColWidth := 85;
  65. //
  66.   StringGrid1.Canvas.Font.Color:= clBlack ;
  67.   StringGrid1.FixedCols := 0;
  68.   StringGrid1.FixedRows := 0;
  69. // Fill the StringGrid with numbers from 1 to 75
  70.   for i := 0 to 4 do
  71.   for j := 0 to 14 do
  72.   begin
  73.     StringGrid1.Cells[j, i] := IntToStr(i * 15 + j + 1);
  74.     StringGrid1.Canvas.Brush.Color := clWhite;
  75.     StringGrid1.Canvas.FillRect(StringGrid1.CellRect(j, i));
  76.     StringGrid1.Canvas.TextOut(StringGrid1.CellRect(j, i).Left + 10,
  77.     StringGrid1.CellRect(j, i).Top + 10,
  78.     IntToStr(i * 15 + j + 1));
  79.   end;
  80. // Reset the array for used numbers
  81.   for i := 1 to 75 do FUsedNumbers[i] := False;
  82.   FRemainingNumbers := 75;
  83.   PanelNUMBER.Caption:= '';;
  84.   PanelNUMBER.Color:= clSilver;
  85. end;
  86.  
  87. procedure TForm1.Generate;
  88. var
  89.   Number: Integer;
  90.   Row, Col: Integer;
  91. begin
  92. //Generate a random number that has not yet been used
  93.   Number := GenerateRandomNumber;
  94.   if Number = -1 then
  95.   begin
  96.     ShowMessage('All numbers have already been drawn!');
  97.     Exit;
  98.   end;
  99. // Show the generated number
  100.   PanelNUMBER.Caption:= IntToStr(Number);
  101.   PanelNUMBER.Color:= clYellow;;
  102. // Find the row and column in the StringGrid
  103.   Row := (Number - 1) div 15;
  104.   Col := (Number - 1) mod 15;
  105. // Highlight the number in the StringGrid
  106.   StringGrid1.Canvas.Brush.Color := ($FFFF00); // clYellow;
  107.   StringGrid1.Canvas.FillRect(StringGrid1.CellRect(Col, Row));
  108.   StringGrid1.Canvas.TextOut(StringGrid1.CellRect(Col, Row).Left + 10,
  109.   StringGrid1.CellRect(Col, Row).Top + 10, IntToStr(Number));
  110. end;
  111.  
  112. procedure TForm1.BtnEXITClick(Sender: TObject);
  113. begin
  114.   Application.Terminate;
  115. end;
  116.  
  117. procedure TForm1.BtnGenerateClick(Sender: TObject);
  118. begin
  119. // Generate the game
  120.    Generate;
  121. end;
  122.  
  123. procedure TForm1.BtnRestartClick(Sender: TObject);
  124. begin
  125. // Restart the game
  126.   ResetGame;
  127. end;
  128.  
  129. // FUNCTION
  130. function TForm1.GenerateRandomNumber: Integer;
  131. var
  132. Num: Integer;
  133. begin
  134. if FRemainingNumbers = 0 then
  135. begin
  136.   Result := -1; // No more nombers left
  137.   Exit;
  138. end;
  139. // Choose any number that hasn't been used yet
  140. repeat
  141. Num := Random(75) + 1;
  142. until not FUsedNumbers[Num];
  143. // Mark the number as used
  144. FUsedNumbers[Num] := True;
  145. Dec(FRemainingNumbers);
  146. Result := Num;
  147. end;
  148.  
  149. end.
  150.      

Thaddy

  • Hero Member
  • *****
  • Posts: 19613
  • Glad to be alive.
Re: BINGO application
« Reply #32 on: August 20, 2024, 01:52:22 pm »
Wait, working on it, due to provider change I have some issues, but not with the code.
Any "programmer" that knows only one programming language is not a programmer

seghele0

  • Sr. Member
  • ****
  • Posts: 276
Re: BINGO application
« Reply #33 on: August 24, 2024, 02:51:50 pm »
Hello Thaddy and other programmers.
In the meantime, with the support of "NL-Delphi", I already have an 'almost' nice workable program.
 :)
Ther's a problem that is apparently very difficult to solve.
If you click on the StrinGrid with the mouse, all already selected numbers will disappear.
This is a very annoying situation.
Maybe someone can find a solution?
 ;)

bytebites

  • Hero Member
  • *****
  • Posts: 794
Re: BINGO application
« Reply #34 on: August 24, 2024, 03:44:59 pm »
Change the generate-function as follow:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Generate;
  2. var
  3.   Number: Integer;
  4.   Row, Col: Integer;
  5. begin
  6. // It's necessary to show a different number at every startup
  7.   Randomize;
  8. //Generate a random number that has not yet been used
  9.   Number := GenerateRandomNumber;
  10.   if Number = -1 then
  11.   begin
  12.     ShowMessage('All numbers have already been drawn!');
  13.     Exit;
  14.   end;
  15. // Show the generated number
  16.   PanelNUMBER.Caption:= IntToStr(Number);
  17.   PanelNUMBER.Color:= clYellow;;
  18. // Find the row and column in the StringGrid
  19.   Row := (Number - 1) div 15;
  20.   Col := (Number - 1) mod 15;
  21. // Highlight the number in the StringGrid
  22.   StringGrid1.objects[col,row]:=tobject(1);
  23.   StringGrid1.Invalidate
  24.   //StringGrid1.Canvas.Brush.Color := ($FFFF00); // clYellow;
  25.   //StringGrid1.Canvas.FillRect(StringGrid1.CellRect(Col, Row));
  26.   //StringGrid1.Canvas.TextOut(StringGrid1.CellRect(Col, Row).Left + 10,
  27.   //StringGrid1.CellRect(Col, Row).Top + 10, IntToStr(Number));
  28. end;  

Add StringGrid1PrepareCanvas
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1PrepareCanvas(Sender:TObject;aCol,aRow:Integer;
  2.   aState:TGridDrawState);
  3. begin
  4.   if StringGrid1.Objects[acol,arow]<>nil then
  5.     StringGrid1.Canvas.Brush.Color := ($FFFF00);
  6. end;
  7.      

When restarting you need to clear the stringgrid objects.

Hansvb

  • Hero Member
  • *****
  • Posts: 935
Re: BINGO application
« Reply #35 on: August 24, 2024, 04:10:11 pm »
In my experience, coloring cells in a TString grid is indeed best done in PrepareCanvas.
Once you have done bytebites solution you need to add 1 more line to your reset function:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ResetGame;
  2. var
  3. i, j: Integer;
  4. begin
  5. // Initialize the StringGrid
  6.   StringGrid1.Clear;  //<<<---------- Add this line
  7.   StringGrid1.ColCount := 15;
  8.   StringGrid1.RowCount := 5;  

seghele0

  • Sr. Member
  • ****
  • Posts: 276
Re: BINGO - working application
« Reply #36 on: August 24, 2024, 05:19:57 pm »
With many thanks to everyone who spontaneously helped me create a Bingo program.
We (retirees) will enjoy it.
 :)
Attached is the working one.



Thaddy

  • Hero Member
  • *****
  • Posts: 19613
  • Glad to be alive.
Re: BINGO application
« Reply #37 on: August 24, 2024, 05:20:31 pm »
My version will follow and covers all this. Just a couple of available hours.
Any "programmer" that knows only one programming language is not a programmer

seghele0

  • Sr. Member
  • ****
  • Posts: 276
Re: BINGO application
« Reply #38 on: August 24, 2024, 05:25:43 pm »
OK.
 ;)

bytebites

  • Hero Member
  • *****
  • Posts: 794
Re: BINGO application
« Reply #39 on: August 24, 2024, 08:08:35 pm »
Reset needs fix: Nil the stringgrid objects.
Set stringgrid rowcount, colcount, fixedcols and fixedrows in propertyeditor.
Painting is no needed here.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ResetGame;
  2. var
  3. i, j: Integer;
  4. begin
  5. // Initialize the StringGrid
  6.   //StringGrid1.ColCount := 15;
  7.   //StringGrid1.RowCount := 5;
  8.   //StringGrid1.Canvas.Font.Color:= clBlack ;
  9.   //StringGrid1.FixedCols := 0;
  10.   //StringGrid1.FixedRows := 0;
  11.   //StringGrid1.FixedRows := 0;
  12. // Fill the StringGrid with numbers from 1 to 75
  13.   for i := 0 to 4 do
  14.   for j := 0 to 14 do
  15.   begin
  16.     StringGrid1.Cells[j, i] := IntToStr(i * 15 + j + 1);
  17.     StringGrid1.Objects[j, i]:=nil;     <---- add this
  18.     //StringGrid1.Canvas.Brush.Color := clWhite;
  19.     //StringGrid1.Canvas.FillRect(StringGrid1.CellRect(j, i));
  20.     //StringGrid1.Canvas.TextOut(StringGrid1.CellRect(j, i).Left + 10,
  21.     //    StringGrid1.CellRect(j, i).Top + 10,
  22.     //    IntToStr(i * 15 + j + 1));
  23.   end;
  24. // Reset the array for used numbers
  25.   for i := 1 to 75 do FUsedNumbers[i] := False;
  26.   FRemainingNumbers := 75;
  27.   PanelNUMBER.Caption:= '';
  28.   PanelNUMBER.Color:= clSilver;
  29.   // not necessary .... is used in StringGrid - Properties.
  30.   //StringGrid1.DefaultRowHeight := 110;
  31.   //StringGrid1.DefaultColWidth := 85;
  32. end;      
  33.  

seghele0

  • Sr. Member
  • ****
  • Posts: 276
Re: BINGO application
« Reply #40 on: August 25, 2024, 11:30:49 am »
bytebites,
The 'ResetGame' procedure has been adjusted.
But after compiling I get ERROR: "Index out of range celcol=0 row=0 ].
 :(
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ResetGame;
  2. var
  3. i, j: Integer;
  4. begin
  5. // Forum Forum Lazarus .....
  6.   StringGrid1.Clear;
  7. // Fill the StringGrid with numbers from 1 to 75
  8.   for i := 0 to 4 do
  9.   for j := 0 to 14 do
  10.   begin
  11.     StringGrid1.Cells[j, i] := IntToStr(i * 15 + j + 1);
  12.     StringGrid1.Objects[j,i]:= nil;
  13.   end;
  14. // Reset the array for used numbers
  15.   for i := 1 to 75 do FUsedNumbers[i] := False;
  16.   FRemainingNumbers := 75;
  17.   PanelNUMBER.Caption:= '';;
  18.   PanelNUMBER.Color:= clSilver;
  19. end;                

seghele0

  • Sr. Member
  • ****
  • Posts: 276
Re: BINGO application
« Reply #41 on: August 25, 2024, 11:56:35 am »
After a test I understand that " StringGrid1.ColCount := 15; " should be left in the procedure.
All the properties of the StringGrid have been adjusted.
So.....other than that I'm stuck.
 %)


d2010

  • Sr. Member
  • ****
  • Posts: 264
Re: BINGO application
« Reply #42 on: August 30, 2024, 10:42:09 pm »
an only learn from your input.

You replace Byte to Word , of type of these variabiles:
Var
  BingoCard:Array[0..74] Of Byte; -- >BingoCard:Array[0..74] Of Word;
  BingoDraw:Array[0..74] Of Byte; --> BingoDraw:Array[0..74] Of Word;

The speed of is better, Byte make slowest Unit2.exe
 %)

bytebites

  • Hero Member
  • *****
  • Posts: 794
Re: BINGO application
« Reply #43 on: August 31, 2024, 07:39:28 am »
StringGrid1.Clear is not needed. It sets colcount to 0.

seghele0

  • Sr. Member
  • ****
  • Posts: 276
Re: BINGO application
« Reply #44 on: September 01, 2024, 10:23:04 am »
n attachment you can find the latest updated version of the Bingo application.
If improvements can be made, I would like to read about it.
Thanks already.
 ;)


 

TinyPortal © 2005-2018