Solved

Tic Tac Toe using Delphi bit buttons

Posted on 2004-08-22
4
520 Views
Last Modified: 2010-04-05
Hello,

I am a little stuck with my code, and need a bit of direction.  Not a solution, just some guidance.  Basically, using Delphi, I am trying to create a tic tac toe game using bit buttons (TBitBtn).  So far I have the following:

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;

type
  TMainform = class(TForm)
    ColorDialog1: TColorDialog;
    ColorBtn: TButton;

    BoardPanel: TPanel;
    Sq1: TBitBtn;
    Sq4: TBitBtn;
    Sq7: TBitBtn;
    Sq9: TBitBtn;
    Sq6: TBitBtn;
    Sq3: TBitBtn;
    Sq8: TBitBtn;
    Sq5: TBitBtn;
    Sq2: TBitBtn;

    EmptyFace: TBitBtn;
    BlueNoughtFace: TBitBtn;
    RedCrossFace: TBitBtn;

    InputBox: TEdit;
    OutputBox: TEdit;
    InputLabel: TLabel;
    OutputLabel: TLabel;

    BoardBtn: TButton;
    DecimalBtn: TButton;
    Play_a_turn: TButton;

    procedure FormCreate(Sender: TObject);
    procedure SquareClick(Sender: TObject);
    procedure Play_a_turnClick(Sender: TObject);
    end;

Square = record
           SquareValue: Integer;
           SquareFace: TBitBtn;
         end;

var
  Mainform: TMainform;
  Board : Array [1..9] of Square;

procedure SetBoardSquare(Pos: Integer; SquareVal: Integer);
procedure IncrementBoardSquare(Pos: Integer);

implementation

{$R *.dfm}

procedure TMainform.FormCreate(Sender: TObject);
var Pos: Integer;

begin
  Board[1].SquareFace:= Sq1;
  Board[2].SquareFace:= Sq2;
  Board[3].SquareFace:= Sq3;
  Board[4].SquareFace:= Sq4;
  Board[5].SquareFace:= Sq5;
  Board[6].SquareFace:= Sq6;
  Board[7].SquareFace:= Sq7;
  Board[8].SquareFace:= Sq8;
  Board[9].SquareFace:= Sq9;
  for Pos := 1 to 9 do
      Board[Pos].SquareValue:= 2;


end;

procedure SetBoardSquare(Pos: Integer; SquareVal: Integer);

begin
   case SquareVal of
        0: begin Board[pos].SquareFace.Glyph := Mainform.EmptyFace.glyph;
                 Board[pos].SquareValue:= 0; end;
        1: begin Board[pos].SquareFace.Glyph := Mainform.BlueNoughtFace.glyph;
                 Board[pos].SquareValue:= 1; end;
        2: begin Board[pos].SquareFace.Glyph := Mainform.RedCrossFace.glyph;
                 Board[pos].SquareValue:= 2; end;
    end;
end;


procedure IncrementBoardSquare(Pos: Integer);
var
   NewVal: Integer;
begin
NewVal:= (Board[Pos].SquareValue + 1) mod 3;
SetBoardSquare(Pos, NewVal);
end;

procedure TMainform.SquareClick(Sender: TObject);
begin
 if (Sender = Board[1].SquareFace) then IncrementBoardSquare(1);
 if (Sender = Board[2].SquareFace) then IncrementBoardSquare(2);
 if (Sender = Board[3].SquareFace) then IncrementBoardSquare(3);
 if (Sender = Board[4].SquareFace) then IncrementBoardSquare(4);
 if (Sender = Board[5].SquareFace) then IncrementBoardSquare(5);
 if (Sender = Board[6].SquareFace) then IncrementBoardSquare(6);
 if (Sender = Board[7].SquareFace) then IncrementBoardSquare(7);
 if (Sender = Board[8].SquareFace) then IncrementBoardSquare(8);
 if (Sender = Board[9].SquareFace) then IncrementBoardSquare(9);
end;

procedure TMainform.Play_a_turnClick(Sender: TObject);
begin
   begin
  if
  MessageDlg(
      'Start Over?',
      mtConfirmation,
      mbOKCancel,0) = mrCancel then
      Exit;

 end;
     
 end;

end.

I am stuck with the actual mechanics of using the procedure Play_a_turn to increment the non-user Bit Button, i.e. if the player is crosses, then the noughts need to increment each time Play_a_turn is pressed.

Any help or pseudo code would be greatly appreciated.
0
Comment
Question by:south_paw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:south_paw
ID: 11867192
Once again, just to clarify, I'm not really in need of direct answers, more of a "where to from here" in order to get the Play_a_turn procedure working.

Cheers.
0
 
LVL 2

Accepted Solution

by:
gary_williams earned 500 total points
ID: 11867199
I'd find a 3x3 "magic square" matrix such that each row, column, and diagonal has a total of 15.  Plug these values into the Tag property of the corresponding buttons.

The software player can then try to find moves that either will (a) cause it to have three squares totalling 15, or a move that will prevent the human player from having three squares totalling 15, with preference given to blocking multiple paths simultaneously if possible.  Obviously, if the software player goes first, it must take the center square.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11867646
hello

  Read this article to help you to implement your game

Your First Delphi Game: Tic Tac Toe
http://delphi.about.com/library/weekly/aa021803a.htm
0
 

Author Comment

by:south_paw
ID: 11872717
Gary,

So in essence I should declare a 2d array (already have a 1d for the board), such as:

type Line = array [1..3, 1..3] of Integer;
 
Which would serve as my magic square, then just loop through the possible scenarios?

Cheers.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Working with hours 3 85
Problem working with dynamic array - help 2 44
Firemonkey Queue list 1 48
Posting a string with PostMessage 23 42
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question