Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Tic Tac Toe using Delphi bit buttons

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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