Recent

Author Topic: Tic tac toe component  (Read 1642 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Tic tac toe component
« on: May 10, 2021, 02:39:42 pm »
هل يُمكنني تصميم component عبارة عن لوحة للعبة tic tac toe بحيث تكون اللوحة التقليدية المكونة من تسعة خانات وعند النقر على خانة أستطيع تحديد أن تكون X أو O من خلال حدث مثلاً OnClick.

لقد قرأتُ عن الـ Classes وكوّنت فكرة مبدأيّة.

google translate:

"Can I design a component of a tic tac toe board that is the traditional nine-digit board and when I click on a field I can specify it to be X or O through an event such as OnClick.

I have read about the Classes and got an idea (good primitive one)."

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Tic tac toe component
« Reply #1 on: May 10, 2021, 02:48:29 pm »
Can't you just use the TStringGrid for that?
Just set the height and width to the same width/height at about a characters width and set FixedCol and FixedRow to 0 and RowCount and ColCount to 3.

Then use use the TStringGrid.OnClick and StringGrid1.Col and StringGrid1.Row to set the correct X or O in the cell.

(No need to design a special component for that.)

Otherwise look here https://forum.lazarus.freepascal.org/index.php?topic=38253.0
or search the forum for tic tac toe. There are lots of examples.
« Last Edit: May 10, 2021, 02:50:43 pm by rvk »

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: Tic tac toe component
« Reply #2 on: May 10, 2021, 03:31:03 pm »
Can't you just use the TStringGrid for that?
Just set the height and width to the same width/height at about a characters width and set FixedCol and FixedRow to 0 and RowCount and ColCount to 3.

Then use use the TStringGrid.OnClick and StringGrid1.Col and StringGrid1.Row to set the correct X or O in the cell.

(No need to design a special component for that.)

Otherwise look here https://forum.lazarus.freepascal.org/index.php?topic=38253.0
or search the forum for tic tac toe. There are lots of examples.

إقتراحكـ بـ TStringGrid يبدو جيّداً جدّاً. وقولكـ لا حاجة إلى إنشاء component جديد خصيصاً من أجل tic tac toe منطقي ولكن هل بإمكاننا إنشاء ذلكـ لو أردنا على Lazarus أم لا يُمكننا إنشاء components من الصفر بل نقوم بعمل فقط modifying لـ components موجودة بالفعل؟

إعتبر لوحة الـ tic tac toe نموذج أو تدريب عملي إذا ما أردنا إنشاء component من الصفر ،يُمكنكـ ذكر بعض الخطوات لو أردت.

google translate:

"Your suggestion for TStringGrid sounds very good. And your saying that there is no need to create a new component specifically " for tic tac toe is  logical", but can we create that - if we wanted to on Lazarus, or could we not create components from scratch but rather do just modifying components that already exist?

Consider the tic tac toe as a model or a practical exercise. If we want to create a component from scratch, you can mention some steps if you like."

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Tic tac toe component
« Reply #3 on: May 10, 2021, 04:22:48 pm »
For creating components: https://wiki.lazarus.freepascal.org/How_To_Write_Lazarus_Component

Easiest would be to create a new component with the TStringGrid as base (and inherit from there).

In the constructor TTicTacToe.Create() you could set FixedCol and FixedRow to 0.
There you can set the other default properties too.

Something like this (you'll need to create the OnClick yourself)
I added the dynamic creation of a TTicTacToe instance on the main form as example but for your main component you would create a separate package (see the help/wiki pages)

Code: Pascal  [Select][+][-]
  1. uses Grids;
  2.  
  3. type
  4.   TTicTacToe = class(TCustomStringGrid)
  5.   public
  6.     constructor Create(AOwner: TComponent); override;
  7.   published
  8.     property OnClick;
  9.   end;
  10.  
  11. constructor TTicTacToe.Create(AOwner: TComponent);
  12. begin
  13.   inherited;
  14.   FixedCols := 0;
  15.   FixedRows := 0;
  16.   ColCount := 3;
  17.   RowCount := 3;
  18.   DefaultColWidth := 30;
  19.   DefaultRowHeight := 30;
  20.   Width := 96;
  21.   Height := 96;
  22. end;
  23.  
  24. // this is just as an example so you can create the code for a component without using packages.
  25. procedure TForm1.FormCreate(Sender: TObject);
  26. begin
  27.   with TTicTacToe.Create(Self) do
  28.   begin
  29.     Parent := Self;
  30.     Left := 200;
  31.     Top := 200;
  32.   end;
  33. end;

And as I said... you can also search the forum for Tic Tac Toe because there are lots of other examples.

Edit: B.T.W. in this example you could also omit the OnClick and override the Click procedure to set X or O. But in that case you would need to add a property to determine which user is at turn (the X or the O).

Edit #2: You can also look here https://www.drbob42.com/delphi/componen.htm
It's for Delphi but the essentials are the same for Lazarus.
And it uses the TTicTacToe as an example so you can see exactly how it's done.
(Only that one uses a 3x3 array of buttons instead of a TStringGrid)
« Last Edit: May 10, 2021, 04:34:51 pm by rvk »

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: Tic tac toe component
« Reply #4 on: May 10, 2021, 05:12:35 pm »
For creating components: https://wiki.lazarus.freepascal.org/How_To_Write_Lazarus_Component

Easiest would be to create a new component with the TStringGrid as base (and inherit from there).

In the constructor TTicTacToe.Create() you could set FixedCol and FixedRow to 0.
There you can set the other default properties too.

Something like this (you'll need to create the OnClick yourself)
I added the dynamic creation of a TTicTacToe instance on the main form as example but for your main component you would create a separate package (see the help/wiki pages)

Code: Pascal  [Select][+][-]
  1. uses Grids;
  2.  
  3. type
  4.   TTicTacToe = class(TCustomStringGrid)
  5.   public
  6.     constructor Create(AOwner: TComponent); override;
  7.   published
  8.     property OnClick;
  9.   end;
  10.  
  11. constructor TTicTacToe.Create(AOwner: TComponent);
  12. begin
  13.   inherited;
  14.   FixedCols := 0;
  15.   FixedRows := 0;
  16.   ColCount := 3;
  17.   RowCount := 3;
  18.   DefaultColWidth := 30;
  19.   DefaultRowHeight := 30;
  20.   Width := 96;
  21.   Height := 96;
  22. end;
  23.  
  24. // this is just as an example so you can create the code for a component without using packages.
  25. procedure TForm1.FormCreate(Sender: TObject);
  26. begin
  27.   with TTicTacToe.Create(Self) do
  28.   begin
  29.     Parent := Self;
  30.     Left := 200;
  31.     Top := 200;
  32.   end;
  33. end;

And as I said... you can also search the forum for Tic Tac Toe because there are lots of other examples.

Edit: B.T.W. in this example you could also omit the OnClick and override the Click procedure to set X or O. But in that case you would need to add a property to determine which user is at turn (the X or the O).

Edit #2: You can also look here https://www.drbob42.com/delphi/componen.htm
It's for Delphi but the essentials are the same for Lazarus.
And it uses the TTicTacToe as an example so you can see exactly how it's done.
(Only that one uses a 3x3 array of buttons instead of a TStringGrid)

جميل! ومجهود كود جيّد ولكنّكـ لم تُجب على سؤالي كما أجابوا في هذه الرابط على السائلين https://forum.lazarus.freepascal.org/index.php/topic,25299.msg153432.html#msg153432 فلقد أجابوا السائلين بالرغم أنّهُ يُمكن إقتراح TStringGrid كحل ولكنّ السائلين يُريدون تنفيذ الفكرة بشكل رسومي مثل إقتراح BGRABitmap وكان هذا الإقتراح حلّاً من الحلول الجيّدة لمسألة الـ tic tac toe ،إذاً إذا أردت أن تُجيب على سؤال compoenent من أجل الـ tic tac toe فيُمكنكـ إقتراح TStringGrid ولكنّ من أسئلة الموضوع كذلكـ كيفيّة تنفيذ ذلكـ من الصفر باعتباره تدريب عملي ونموذج لإنشاء component من الصفر أم هل يجب أن أنشئ موضوع مستقل من أجل هذا الغرض وهناكـ سوف تسألني كذلكـ ما الذي أودّ تنفيذه كـ component من الصفر من أجل إقتراح حل آخر غير ما هو مطلوب فنكون في دائرة كأنّكـ تُجيب أسئلة أخرى غير التي في الموضوع؟

google translate:

"Nice! Good code effort, but you did not answer my question as they answered in this link to the questioners [url] https://forum.lazarus.freepascal.org/index.php/topic,25299.msg153432.html#msg153432 [/ url] They answered the questions. Although TStringGrid can be suggested as a solution, the questioners want to graphically implement the idea like the BGRABitmap suggestion. This suggestion was a good solution to the tic tac toe problem, so if you want to answer the compoenent question for tic tac toe then you can suggest the topic from TStringGrid, but Also,"of the questions of the topic" how to implement this - from scratch as a practical training and a model to create a component from scratch, or do I have to create a separate topic for this purpose? And there - you will also ask me - What do I want to implement as a component from scratch in order to suggest a solution other than what is required, so we are "will be" in a circle as if you are? "as if you are " Answer-ing questions other than the one in the topic?"

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Tic tac toe component
« Reply #5 on: May 10, 2021, 06:09:16 pm »
Sorry, I can't really understand your translation.

I'm not familiar with bgraBitmap.
Your question was about tic tac toe and X and O.

The links I gave show you how you can create a component.
https://wiki.lazarus.freepascal.org/How_To_Write_Lazarus_Component
https://www.drbob42.com/delphi/componen.htm

Because creating a componemt from scratch isn't that easy if you haven't any code yet, I showed you how you can create the code dynamically  (with a form) with which you can later create the real component (as shown in the first link).

If you want a more graphical component then you can follow the second link which uses buttons. You can change the buttons to speedbuttons with glyphs.

For a component using bgraBitmap maybe someone else can help.

Bart

  • Hero Member
  • *****
  • Posts: 4345
    • Bart en Mariska's Webstek
Re: Tic tac toe component
« Reply #6 on: May 10, 2021, 06:46:55 pm »
Easiest would be to create a new component with the TStringGrid as base (and inherit from there).

Easy, but well thought off?
You expose all the properties and methods from TStringGrid like InsertRow() etc.
You don't want that.
Take a look at TValueListEditor to see what kind of mess that creates.

I would create a TTicTacToe component that just encapsulates the internal logic.
It could for example have a playing field, of which you can set each field to Knot, Cross, Empty.
It could have a DrawField method, whic in the base class I would make just empty.
Derived classes then can implement how the playing field is drawn: simply on the console or some fancy GUI.

This way, the UI in essence has nothing to do with the TTicTacToe class.

Bart

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: Tic tac toe component
« Reply #7 on: May 10, 2021, 07:25:06 pm »

Easy, but well thought off?
You expose all the properties and methods from TStringGrid like InsertRow() etc.
You don't want that.
Take a look at TValueListEditor to see what kind of mess that creates.


تحليل يدلّ على إدراكـ التفاصيل والخصائص المُختلفة وعمليّة تطبيق فكرة من عدمها!

google translate:

"An analysis indicating an awareness of the various "different" details and characteristics "properties" and the process "practicality" of implementing an idea or not!"


I would create a TTicTacToe component that just encapsulates the internal logic.
It could for example have a playing field, of which you can set each field to Knot, Cross, Empty.
It could have a DrawField method, whic in the base class I would make just empty.
Derived classes then can implement how the playing field is drawn: simply on the console or some fancy GUI.

هذا هو المطلوب بالضبط وبشكل قياسي وعملي يُطبق النظريّة بشكل مثالي.

google translate:

"This is exactly what is required and in a standard and practical manner, it applies the theory perfectly."

This way, the UI in essence has nothing to do with the TTicTacToe class.

Bart

لم أفهم هذا السطر جيّدا.

google translate:

"I didn't understand this line well."

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: Tic tac toe component
« Reply #8 on: May 10, 2021, 07:28:50 pm »
Sorry, I can't really understand your translation.

الزميل @Bart يستوعب الموضوع بشكل صحيح وأكاديمي.

google translate:

"Colleague @Bart understands the topic properly and academically."

 

TinyPortal © 2005-2018