1. Home
  2. Computing & Technology
  3. Delphi Programming
Your First Delphi Game: Tic Tac Toe
Page 1: Preparing the Game GUI
 More of this Feature
• Page 2: Game Initialization
• Page 3: Player X, your turn!
• Page 4: New Game?
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
Discuss!
 Related Resources
• A Beginner's Guide to Delphi Programming.TOC

• Free Code Delphi Games
• Free Source Delphi Projects
 From Other Guides
• VB Tic Tac Toe
• Java Tic Tac Toe

Welcome to the tenth chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
Designing and developing a real game using Delphi: Tic Tac Toe.

Ok, stop playing with Delphi. After nine chapters of this course you are ready for some real action. It's time to create a full working Delphi project, and what type of project would you be more interested than creating a game?
Your goal in this chapter is to create a simple game using Delphi. Here are the topics you'll practically try and learn here:

  • Building a program GUI
  • Building a program Logic
  • Working with arrays
  • Building Functions, Procedures using Parameter Passing
  • Casting one Delphi object to another
  • Tic Tac Toe, the Rules
    Tic Tac Toe is a traditional game, played by two opponents that alternately place X's and O's into 3x3 playing field.

    Tic Tac Toe Situations

    Before the game starts, players have to decide who will go first and who will mark his moves with an X. After the first move, game proceeds with the opponents alternately placing their marks at any unoccupied cell. The goal of this game is to be the first one with three marks in a row, where the row can be horizontal, diagonal or vertical. If all cells are occupied and both players don't have a winning combination, the game is a draw.

    Back to the "drawing board" ...

    Preparing the Tic Tac Toe Game
    At this moment you should have your copy of Delphi up and running. By default, as you know by now, when you start Delphi, a new (standard Windows) project is created hosting one Form object (and the associated unit).
    Our game will require only one form where all the action is taking place, naturally we'll use the form object Delphi "prepared" for us.

    Saving for the first time
    Before we move on, I suggest you to rename that default form, by assigning the string 'frMain' to the Name property (using the Object Inspector). Next, save the project, by pointing to File | Save All (Delphi IDE main menu). The Save As dialog box will be displayed, providing you with the option to first save the form's unit file, the default name will be Unit1.pas. The offered location will be inside the Projects folder under the Delphi install folder. It is advisable to save every project in a separate folder, therefore, use the 'Create New Folder' toolbar button to create a new folder, and name it 'TicTacToe'. Now, change the name of the Unit1.pas to Main.pas and click Save. You will then be prompted to save the project, by default Delphi will name this project 'Project1' (DPR), of course we'll change it to, let's say, 'TicTacToe'. That's it. Later on, when you want to save the current status of your project, just use Ctrl+Shift+S (keyboard shortcut for Save All).

    GUI
    Ok, first we build the game GUI (graphical user interface). For the moment we only have a form object named 'frMain', use Object Inspector to change the Caption property to 'About Delphi Programming: Tic Tac Toe'

    The playfield. Your goal is to create a "grid" consisting of 9 cells, where each cell will display either nothing, X or O. The simplest is to use the Label component from the Standard palette. Drop one on the form. Note, we'll need 9 labels looking similar. The easiest way to do that is to create first label, name it, choose the appropriate font, color, and set every other property. For this purpose we will name the label 'lblCell0'.
    Use the Object Inspector to set the following properties:

    object lblCell0: TLabel // note Name = lblCell0
      Left = 8
      Top = 8
      Width = 81
      Height = 81
      Alignment = taCenter
      AutoSize = False
      Caption = 'lblCell0'
      Color = clCream	 
      Font.Size = 16
      Font.Name = 'Tahoma'
      Font.Style = [fsBold]
      Layout = tlCenter
    end
    

    Note: If you right click anywhere on your form, and choose 'View As Text' command from the context pop up menu, you are presented with text description of the form’s attributes, inside the Code Editor window. To switch back to 'For View', just right click this "code" window and choose 'View As Form'.

    After we shape up the first (cell) label, we can create other eight elements of the grid. Select the label object, and press CTRL+C. Now, select the form (click anywhere on the form) and press CTRL+V to create another label object. The second object will inherit all properties from the first one, only the Name property will be re-set to Label1. Alter it manually to lblCell1, note that you must also manually change the Caption property to lblCell1. We do not really need the Caption property at this moment, but if you delete it (making it an empty string) you will not be able to differentiate one cell from another (without selecting one, of course).

    Go on and create all nine labels. Make sure that the Name properties are sequential in the grid, we will build the game logic on the cell position / name. Label object with Name lblCell0 zero should be in the top left corner, and the bottom right label should be lblCell8.
    When you are finished your form should be looking something like:

    Tic Tac Toe Delphi playfield

    Next, we add some more objects to the form:

  • Two TButton objects (Standard Palette), name them 'btnNewGame' and 'btnResetScore',
  • One TRadioGroup object (Standard Palette), name it 'rgPlayFirst', set the Caption property to 'First to play'. Now, select this RadioGroup and point to the Items property, click the ellipsis button. Add two radio buttons, each string in Items makes a radio button appear in the radio group box with the string as its caption. The value of the ItemIndex property determines which radio button is currently selected.
  • One TGroupBox object (Name = 'gbScoreBoard'; Caption = 'Score Board') with six labels (names : lblX, lblXScore, lblO, lblOScore, lblMinus, lblColon).

    Tic Tac Toe playfield complete

    Once we have the game GUI, we can proceed to game logic and initialization.

    Next page > Game Initialization > Page 1, 2, 3, 4

    A Beginner's Guide to Delphi Programming: Next Chapter >>
    >> Your First MDI Delphi Project

  • Explore Delphi Programming
    About.com Special Features

    Holiday Central

    What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

    Family Tech Center

    Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

    1. Home
    2. Computing & Technology
    3. Delphi Programming

    ©2009 About.com, a part of The New York Times Company.

    All rights reserved.