Forum > Beginners

Tic tac toe component

(1/2) > >>

pascal111:
هل يُمكنني تصميم 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:
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.

pascal111:

--- Quote from: rvk 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.

--- End quote ---

إقتراحكـ بـ 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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Grids; type  TTicTacToe = class(TCustomStringGrid)  public    constructor Create(AOwner: TComponent); override;  published    property OnClick;  end; constructor TTicTacToe.Create(AOwner: TComponent);begin  inherited;  FixedCols := 0;  FixedRows := 0;  ColCount := 3;  RowCount := 3;  DefaultColWidth := 30;  DefaultRowHeight := 30;  Width := 96;  Height := 96;end; // this is just as an example so you can create the code for a component without using packages.procedure TForm1.FormCreate(Sender: TObject);begin  with TTicTacToe.Create(Self) do  begin    Parent := Self;    Left := 200;    Top := 200;  end;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)

pascal111:

--- Quote from: rvk 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Grids; type  TTicTacToe = class(TCustomStringGrid)  public    constructor Create(AOwner: TComponent); override;  published    property OnClick;  end; constructor TTicTacToe.Create(AOwner: TComponent);begin  inherited;  FixedCols := 0;  FixedRows := 0;  ColCount := 3;  RowCount := 3;  DefaultColWidth := 30;  DefaultRowHeight := 30;  Width := 96;  Height := 96;end; // this is just as an example so you can create the code for a component without using packages.procedure TForm1.FormCreate(Sender: TObject);begin  with TTicTacToe.Create(Self) do  begin    Parent := Self;    Left := 200;    Top := 200;  end;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)

--- End quote ---

جميل! ومجهود كود جيّد ولكنّكـ لم تُجب على سؤالي كما أجابوا في هذه الرابط على السائلين 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?"

Navigation

[0] Message Index

[#] Next page

Go to full version