VCLButtons(C++)
Description
This example demonstrates how to use a TButtonGroup. This example requires the following components:
- A TButtonGroup object with 3 TGrpButtonItems objects
- A TButton object
The form should look like in the following image.
Error creating thumbnail: Unable to save thumbnail to destination
After running the project and pressing the button, the form should look like in the following image.
Error creating thumbnail: Unable to save thumbnail to destination
The user can press different buttons from the button group, setting their caption to X. The computer automatically places a 0 caption on an empty button, chosen randomly.
If a column, line, or diagonal is marked the same, a ShowMessage box appears, announcing the winner.
Error creating thumbnail: Unable to save thumbnail to destination
Code
//C++
//procedures and variables declaration
int Random_var;//generate a random number from 0 through 9
int GameMatrix[3][3];//set to be -10 for an empty position, 0 for the 0-player, 1 for the X-player
int Score;//used for verifying whether the game is over (equals 3 if the X-player wins, 0 if the 0-player wins)
implementation
void Validate() {
int I, J;
// verify the lines
for (I = 0; I < 3; I++) {
Score = 0;
for (J = 0; J < 3; J++)
Score += GameMatrix[I][J];
if ((Score == 3) || (Score == 0))
return;
}
// verify the main diagonal
Score = 0;
for (I = 0; I < 3; I++)
Score += GameMatrix[I][I];
if ((Score == 3) || (Score == 0))
return;
// verify the secondary diagonal
Score = 0;
for (I = 0; I < 3; I++)
Score += GameMatrix[I][2 - I];
if ((Score == 3) || (Score == 0))
return;
// verify the columns
for (I = 0; I < 3; I++) {
Score = 0;
for (J = 0; J < 3; J++)
Score += GameMatrix[J][I];
if ((Score == 3) || (Score == 0))
return;
}
}
void StartGame() {
int I, J;
// show the button group and all the buttons' captions to an empty string
Form1->ButtonGroup1->Visible = True;
for (I = 0; I < 9; I++)
Form1->ButtonGroup1->Items->Items[I]->Caption = "";
// fill the game matrix with negative values, to help calculating the score
for (I = 0; I < 3; I++)
for (J = 0; J < 3; J++)
GameMatrix[I][J] = -10;
}
void FillMatrix(int Pos, int I) {
// set a specific item of the matrix to be the value given through the parameter
switch (Pos) {
case 0:
GameMatrix[0][0] = I;
break;
case 1:
GameMatrix[0][1] = I;
break;
case 2:
GameMatrix[0][2] = I;
break;
case 3:
GameMatrix[1][0] = I;
break;
case 4:
GameMatrix[1][1] = I;
break;
case 5:
GameMatrix[1][2] = I;
break;
case 6:
GameMatrix[2][0] = I;
break;
case 7:
GameMatrix[2][1] = I;
break;
case 8:
GameMatrix[2][2] = I;
break;
default: ;
}
}
void Complete() {
int I;
// when the user presses a button and sets its caption to 'X', randomly find an
// unused button and set its caption to '0'
for (I = 1; I <= 9; I++) {
Random_var = Random(9);
if (Form1->ButtonGroup1->Items->Items[Random_var]->Caption == "") {
Form1->ButtonGroup1->Items->Items[Random_var]->Caption = "0";
FillMatrix(Random_var, 0);
break;
}
}
}
void __fastcall TForm1::FormCreate(TObject *Sender) {
// initialize the properties of the button group and other visual objects
Form1->Caption = "Tic Tac Toe";
ButtonGroup1->ButtonHeight = 50;
ButtonGroup1->ButtonWidth = 50;
ButtonGroup1->Height = 170;
ButtonGroup1->Width = 170;
ButtonGroup1->Visible = False;
ButtonGroup1->BorderStyle = bsNone;
ButtonGroup1->Font->Size = ButtonGroup1->ButtonHeight - 10;
Button1->Align = alBottom;
Button1->Caption = "START GAME";
}
//OnClick event handler for TButton
void __fastcall TForm1::Button1Click(TObject *Sender) {
int I, J;
ButtonGroup1->Align = alTop;
ButtonGroup1->Width = Form1->Width - 10;
ButtonGroup1->ButtonWidth = ButtonGroup1->Width / 3;
ButtonGroup1->Height = ButtonGroup1->Width;
ButtonGroup1->ButtonHeight = ButtonGroup1->Height / 3;
ButtonGroup1->Enabled = True;
StartGame();
}
//OnClick event handler for a button group item (TGrpButtonItems)
void __fastcall TForm1::ButtonGroup1Items0Click(TObject *Sender) {
// when the user presses a button, set its caption to 'X', complete the GameMatrix,
// verify if the game is over, set another button's caption to '0'
if (ButtonGroup1->Items->Items[0]->Caption == "") {
ButtonGroup1->Items->Items[0]->Caption = "X";
GameMatrix[0][0] = 1;
Validate();
if (Score == 3) {
ShowMessage("X WINS!");
ButtonGroup1->Enabled = False;
}
else {
Complete();
Validate();
if (Score == 0) {
ShowMessage("0 WINS!");
ButtonGroup1->Enabled = False;
}
}
}
}
//[...]
//rewrite the previous procedure for all the other 8 button group items
Uses
- Vcl.StdCtrls.TButton ( fr | de | ja )
- Vcl.StdCtrls.TButton.OnClick ( fr | de | ja )
- Vcl.StdCtrls.TButton.Caption ( fr | de | ja )
- Vcl.StdCtrls.TButton.Align ( fr | de | ja )
- Vcl.ButtonGroup.TButtonGroup ( fr | de | ja )
- Vcl.ButtonGroup.TButtonGroup.Items ( fr | de | ja )
- Vcl.ButtonGroup.TButtonGroup.Align ( fr | de | ja )
- Vcl.ButtonGroup.TButtonGroup.Enabled ( fr | de | ja )
- Vcl.ButtonGroup.TButtonGroup.Visible ( fr | de | ja )
- Vcl.ButtonGroup.TGrpButtonItems ( fr | de | ja )
- Vcl.ButtonGroup.TGrpButtonItems.Items ( fr | de | ja )
- Vcl.ButtonGroup.TGrpButtonItem ( fr | de | ja )
- Vcl.CategoryButtons.TBaseButtonItem.Caption ( fr | de | ja )
- Vcl.CategoryButtons.TBaseButtonItem.OnClick ( fr | de | ja )