Solved

Tic Tac Toe using Delphi bit buttons

Posted on 2004-08-22
4
487 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: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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

15 Experts available now in Live!

Get 1:1 Help Now