VCLButtons(Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates how to use a TButtonGroup. This example requires the following components:

The form should look like in the following image.

ButtonGroup1.JPG

After running the project and pressing the button, the form should look like in the following image.

ButtonGroup2.JPG

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.

ButtonGroup3.JPG

Code

//Delphi

//procedures and variables declaration
procedure Validate();
procedure StartGame();
procedure FillMatrix(Pos: Integer; I: Integer);
procedure Comp();

var
  Form1: TForm1;
  Random_var: Byte; //generate a random number from 0 through 9
  GameMatrix: array[1..3,1..3] of Integer; //set to be -10 for an empty position, 0 for the 0-player, 1 for the X-player
  Score: Integer; //used for verifying whether the game is over (equals 3 if the X-player wins, 0 if the 0-player wins)

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  // 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';
end;

//procedures implementation
procedure FillMatrix(Pos: Integer;I: Integer);
begin
  //set a specific item of the matrix to be the value given through the parameter
  case Pos of
    0: GameMatrix[1,1] := I;
    1: GameMatrix[1,2] := I;
    2: GameMatrix[1,3] := I;
    3: GameMatrix[2,1] := I;
    4: GameMatrix[2,2] := I;
    5: GameMatrix[2,3] := I;
    6: GameMatrix[3,1] := I;
    7: GameMatrix[3,2] := I;
    8: GameMatrix[3,3] := I;
  end;
end;

procedure StartGame();
var I, J: Integer;
begin
  //show the button group and all the buttons' captions to an empty string
  Form1.ButtonGroup1.Visible := True;
  for I := 0 to 8 do
     Form1.ButtonGroup1.Items.Items[I].Caption := '';
  //fill the game matrix with negative values, to help calculating the score
  for I := 1 to 3 do
    for J := 1 to 3 do
     GameMatrix[I,J] := -10;
end;

procedure Validate;
var
  I, J: Integer;
begin
  // verify the lines
  for i := 1 to 3 do
  begin
    Score := 0;
    for j := 1 to 3 do Score := Score + GameMatrix[i,j];
    if (Score = 3) or (Score = 0) then
      Exit;
 end;
 // verify the main diagonal
 Score := 0;
 for i := 1 to 3 do Score := Score + GameMatrix[i,i];
  if (Score = 3) or (Score = 0) then
    Exit;
 // verify the secondary diagonal
 Score := 0;
 for i := 1 to 3 do Score := Score + GameMatrix[i,4-i];
  if (Score = 3) or (Score = 0) then
    Exit;
 // verify the columns
 for i := 1 to 3 do
 begin
  Score := 0;
  for j := 1 to 3 do Score := Score + GameMatrix[j,i];
  if (Score = 3) or (Score = 0) then
      Exit;
 end;
end;

procedure Comp();
var I: Integer;
begin
  //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 to 9 do
  begin
    Random_var := Random(9);
    if (Form1.ButtonGroup1.Items.Items[Random_var].Caption = '') then
    begin
      Form1.ButtonGroup1.Items.Items[Random_var].Caption := '0';
      FillMatrix(Random_var,0);
      Break;
    end;
  end;
end;

//OnClick event handler for TButton
procedure TForm1.Button1Click(Sender: TObject);
var I, J: Integer;
begin
  ButtonGroup1.Align := alTop;
  ButtonGroup1.ButtonWidth := ButtonGroup1.Width div 3;
  ButtonGroup1.Height := ButtonGroup1.Width;
  ButtonGroup1.ButtonHeight := ButtonGroup1.Height div 3;
  ButtonGroup1.Enabled := True;
  StartGame();
end;

//OnClick event handler for a button group item (TGrpButtonItems)
procedure TForm1.ButtonGroup1Items0Click(Sender: TObject);
begin
  //when the user presses a button, set its caption to 'X', complete the GameMatrix,
  //verify whether X wins, set another button's caption to '0', verify whether 0 wins
  if (ButtonGroup1.Items.Items[0].Caption = '') then
  begin
  ButtonGroup1.Items.Items[0].Caption := 'X';
  GameMatrix[1,1] := 1;
  Validate();
  if Score = 3 then
      begin
        ShowMessage('X WINS!');
        ButtonGroup1.Enabled := False;
      end
    else begin
        Comp();
        Validate();
        if Score = 0 then begin
          ShowMessage('0 WINS!');
          ButtonGroup1.Enabled := False;
        end;
    end;
  end;
end;

//[...]
//rewrite the previous procedure for all the other 8 button group items

end.

Uses

Personal tools
Assistance