* * *

Author Topic: How to do a tic tac toe game with form?  (Read 627 times)

Whillis

  • Newbie
  • Posts: 1
How to do a tic tac toe game with form?
« on: September 12, 2017, 11:12:26 pm »
 Im having a project to do a tic tac toe game in lazarus, no a.i. just player vs. player.
And i really don't know how to make an alternating player1 player2 thing.
I want it to start as 'X' and then changes to 'O' and keeps until goes to 9 characters total.
i haven't done much yet, only the buttons.

Code: Pascal  [Select]
  1.  
  2. unit Unit2;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm2 }
  14.  
  15.   TForm2 = class(TForm)
  16.     Button10: TButton;
  17.     Button2: TButton;
  18.     Button3: TButton;
  19.     Button4: TButton;
  20.     Button5: TButton;
  21.     Button6: TButton;
  22.     Button7: TButton;
  23.     Button8: TButton;
  24.     Button9: TButton;
  25.     procedure Button10Click(Sender: TObject);
  26.     procedure Button2Click(Sender: TObject);
  27.     procedure Button3Click(Sender: TObject);
  28.     procedure Button4Click(Sender: TObject);
  29.     procedure Button5Click(Sender: TObject);
  30.     procedure Button6Click(Sender: TObject);
  31.     procedure Button7Click(Sender: TObject);
  32.     procedure Button8Click(Sender: TObject);
  33.     procedure Button9Click(Sender: TObject);
  34.   private
  35.     { private declarations }
  36.   public
  37.     { public declarations }
  38.  
  39.   end;
  40.  
  41. var
  42.   Form2: TForm2;
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TForm2 }
  49.  
  50. procedure TForm2.Button2Click(Sender: TObject);
  51. begin
  52.     if (player=1) then
  53.     begin
  54.       Button2.Caption:=('X');
  55.     end;
  56.  
  57.     if (player=2) then
  58.     begin
  59.       Button2.Caption:=('X');
  60.     end
  61. end;
  62.  
  63. procedure TForm2.Button10Click(Sender: TObject);
  64. begin
  65.  
  66. end;
  67.  
  68. procedure TForm2.Button3Click(Sender: TObject);
  69. begin
  70.  
  71. end;
  72.  
  73. procedure TForm2.Button4Click(Sender: TObject);
  74. begin
  75.  
  76. end;
  77.  
  78. procedure TForm2.Button5Click(Sender: TObject);
  79. begin
  80.  
  81. end;
  82.  
  83. procedure TForm2.Button6Click(Sender: TObject);
  84. begin
  85.  
  86. end;
  87.  
  88. procedure TForm2.Button7Click(Sender: TObject);
  89. begin
  90.  
  91. end;
  92.  
  93. procedure TForm2.Button8Click(Sender: TObject);
  94. begin
  95.  
  96. end;
  97.  
  98. procedure TForm2.Button9Click(Sender: TObject);
  99. begin
  100.  
  101. end;
  102.  
  103. end.
  104.  
  105.  
  106.  

RAW

  • Sr. Member
  • ****
  • Posts: 495
Re: How to do a tic tac toe game with form?
« Reply #1 on: September 12, 2017, 11:52:49 pm »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

RAW

  • Sr. Member
  • ****
  • Posts: 495
Re: How to do a tic tac toe game with form?
« Reply #2 on: September 13, 2017, 12:48:17 am »
You probably need a TTT-Board...

Maybe like this...
Code: Pascal  [Select]
  1. // PRIVATE
  2. //  arrBoard: Array[0..2, 0..2] Of Char;
  3. Procedure TForm1.FormClick(Sender: TObject);
  4.   Var
  5.    i, ix: Integer;
  6.  Begin
  7.   For i:= Low(arrBoard) To High(arrBoard)
  8.   Do
  9.    For ix:= Low(arrBoard) To High(arrBoard)
  10.    Do arrBoard[i, ix]:= 'x';
  11.  End;

Save the board maybe like this...
Code: Pascal  [Select]
  1. Procedure TForm1.SaveBoard;
  2.   Var
  3.    strFileName: String;
  4.    fs         : TFileStream;
  5.  Begin
  6.   strFileName:= Application.Location+'Board.txt';
  7.  
  8.   fs:= TFileStream.Create(strFileName, fmCreate);
  9.    Try
  10.     fs.WriteBuffer(arrBoard, SizeOf(arrBoard));
  11.    Finally
  12.     fs.Free;
  13.    End;
  14.  End;
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

RAW

  • Sr. Member
  • ****
  • Posts: 495
Re: How to do a tic tac toe game with form?
« Reply #3 on: September 13, 2017, 01:33:21 am »
Loading the board should be the same...

Code: Pascal  [Select]
  1. Procedure TForm1.LoadBoard;
  2.   Var
  3.    strFileName: String;
  4.    fs         : TFileStream;
  5.  Begin
  6.   strFileName:= Application.Location+'Board.txt';
  7.    If Not FileExists(strFileName) Then Exit;
  8.  
  9.   fs:= TFileStream.Create(strFileName, fmOpenRead);
  10.    Try
  11.     If fs.Size <> SizeOf(arrBoard) Then Exit;
  12.     fs.ReadBuffer(arrBoard, fs.Size);
  13.    Finally
  14.     fs.Free;
  15.    End;
  16.  End;

Show the board as string...  :D :D :D

Code: Pascal  [Select]
  1. Procedure TForm1.Button2Click(Sender: TObject);
  2.   Var
  3.    i, ix, ic: Integer;
  4.    str      : String;
  5.  Begin
  6.   ic:= 0;
  7.   LoadBoard;
  8.  
  9.   For i:= Low(arrBoard) To High(arrBoard)
  10.   Do
  11.    For ix:= Low(arrBoard) To High(arrBoard)
  12.    Do
  13.     Begin
  14.      Inc(ic);
  15.       If ic = 4
  16.       Then
  17.        Begin
  18.         ic := 1;
  19.         str:= str+sLineBreak;
  20.        End;
  21.      str:= str+arrBoard[i, ix];
  22.     End;
  23.  
  24.   Label1.Caption:= str;
  25.  End;

Yeah.. I know... I am the greatest game programmer the world has ever seen... that's for sure...  :P
« Last Edit: September 13, 2017, 08:00:13 pm by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

Eugene Loza

  • Sr. Member
  • ****
  • Posts: 450
    • My "almost daily" development blog
Re: How to do a tic tac toe game with form?
« Reply #4 on: September 13, 2017, 07:11:44 am »
Whillis, the approach you use is very inefficient, but I believe, you can start from this.
E.g. when a player pushes the button, you have a correct checking of "player" but you have to define the variable first in Var. Then you just have to switch player. e.g. in your code:
Code: Pascal  [Select]
  1.     if (player=1) then
  2.     begin
  3.       Button2.Caption:=('X');
  4.       Player := 2;
  5.     end
  6.     else //<------------- this is important here
  7.     if (player=2) then
  8.     begin
  9.       Button2.Caption:=('O');
  10.       Player := 1;
  11.     end
Next you'll have to check for "win/loose" conditions. That'd be a bit more tricky. But, more importantly, you'll have to copy the code 9 times for every button.
That said, you'd better learn how procedures work. e.g. creating a
Code: Pascal  [Select]
  1. procedure MoveHere(aButton: TButton);
  2.     if (player=1) then
  3.     begin
  4.       aButton.Caption:=('X');
  5.       Player := 2;
  6.     end
  7.     else
  8.     begin
  9.       aButton.Caption:=('O');
  10.       Player := 1;
  11.     end;
  12. if (Button1.Caption="X") and (Button2.Caption="X") and (Button3.Caption="X") then alert('Player X wins!!!');
  13. if (Button4.Caption="X") and (Button5.Caption="X") and (Button6.Caption="X") then alert('Player X wins!!!');
  14. ... check all possible lines
  15. //do the same for player O
  16. if (Button1.Caption="O") and (Button2.Caption="O") and (Button3.Caption="O") then alert('Player O wins!!!');
  17. ... and check all possible lines again
  18.  
  19. ...
  20. ...
  21. procedure TForm2.Button4Click(Sender: TObject);
  22. begin
  23.   MoveHere(Button4);
  24. end;
This is still very inefficient, but it's easy to understand.
Once you've done with this way, try to generate the buttons dynamically. With this you can automate creation, placement, operation of buttons and more efficiently checking for win conditions.
My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

RAW

  • Sr. Member
  • ****
  • Posts: 495
Re: How to do a tic tac toe game with form?
« Reply #5 on: September 13, 2017, 10:25:51 am »
BTW: The easiest way is probably to do almost everything inside the Button1.OnClick event and then assign this event to all the other buttons....
And a good idea is to use TPanels, then you can set the color (background) of the panel to show the line if someone wins... A normal button at least on WINDOWS cannot change the color...
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

RAW

  • Sr. Member
  • ****
  • Posts: 495
Re: How to do a tic tac toe game with form?
« Reply #6 on: September 18, 2017, 12:25:24 am »
Try this...

Check Winner:
(If you have a bigger board then it's probably a good idea to put the arrays inside one big dyn.Array or a TList or something like that...)
Or use your own algorithm...  :)
Or the way used on the above mentioned website...
Code: Pascal  [Select]
  1. Procedure TwndGUI.SearchWinner;
  2.   Type
  3.    TArr = Array[1..5] Of String;
  4.   Var
  5.    booFoundX,
  6.    booFoundO: Boolean;
  7.  
  8.    Procedure CheckLine(arr: TArr);
  9.      Var
  10.       i: Integer;
  11.     Begin
  12.      booFoundX:= True;
  13.      booFoundO:= True;
  14.       For i:= 1 To 5
  15.       Do
  16.        Begin
  17.         If arrPnlBoard[StrToInt(arr[i])].Caption <> 'X'
  18.         Then booFoundX:= False;
  19.  
  20.         If arrPnlBoard[StrToInt(arr[i])].Caption <> 'O'
  21.         Then booFoundO:= False;
  22.        End;
  23.  
  24.        If booFoundX Or booFoundO
  25.        Then
  26.         Begin
  27.          For i:= 1 To 5
  28.          Do arrPnlBoard[StrToInt(arr[i])].Color:= clLime;
  29.  
  30.          booWinner:= True;
  31.          Caption  := 'TicTacFive';
  32.         End;
  33.     End;
  34.  Begin
  35.   CheckLine(arrRow1); If booWinner Then Exit;
  36.   CheckLine(arrRow2); If booWinner Then Exit;
  37.   CheckLine(arrRow3); If booWinner Then Exit;
  38.   CheckLine(arrRow4); If booWinner Then Exit;
  39.   CheckLine(arrRow5); If booWinner Then Exit;
  40.   CheckLine(arrCol1); If booWinner Then Exit;
  41.   CheckLine(arrCol2); If booWinner Then Exit;
  42.   CheckLine(arrCol3); If booWinner Then Exit;
  43.   CheckLine(arrCol4); If booWinner Then Exit;
  44.   CheckLine(arrCol5); If booWinner Then Exit;
  45.   CheckLine(arr11);   If booWinner Then Exit;
  46.   CheckLine(arr12);  
  47.  End;

Code: Pascal  [Select]
  1. Procedure TwndGUI.OnBoardClick(Sender: TObject);
  2.  Begin
  3.   If Sender Is TPanel
  4.   Then
  5.    If Not booWinner
  6.    Then
  7.     If TPanel(Sender).Caption = ''
  8.     Then
  9.      Begin
  10.       If Not booX
  11.       Then
  12.        Begin
  13.         TPanel(Sender).Caption:= 'X';
  14.  
  15.         booX     := True;
  16.         booPlayed:= True;
  17.         Caption  := 'TicTacFive    PLAYER  O';
  18.        End
  19.       Else
  20.        Begin
  21.         TPanel(Sender).Caption:= 'O';
  22.  
  23.         booX     := False;
  24.         booPlayed:= True;
  25.         Caption  := 'TicTacFive    PLAYER  X';
  26.        End;
  27.  
  28.       SearchWinner;
  29.       Inc(cBoardClick);
  30.  
  31.       If cBoardClick = 25
  32.       Then Caption  := 'TicTacFive    GAME OVER';
  33.      End;
  34.  End;
« Last Edit: September 19, 2017, 12:34:30 am by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

RAW

  • Sr. Member
  • ****
  • Posts: 495
Re: How to do a tic tac toe game with form?
« Reply #7 on: September 19, 2017, 08:15:13 am »
2 Player Version:
 
 Player X = Mouse
 Player O = ArrowKeys + Return

I don't have time for testing this, my most important computer is down...
I've done a quick test on my XPx86 Celeron M430 "MACHINE"... looks ok...  :D

Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus