?
Solved

Tic Tac Toe using Delphi bit buttons

Posted on 2004-08-22
4
Medium Priority
?
559 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 1000 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

Industry Leaders: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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