Solved

Tic Tac Toe using Delphi bit buttons

Posted on 2004-08-22
4
478 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
  • 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:mnasman
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now