| Your First Delphi Game: Tic Tac Toe | ||||||||||||||||||||||
| Page 1: Preparing the Game GUI | ||||||||||||||||||||||
Welcome to the tenth chapter of the FREE online programming course: 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?
![]() 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 GameAt 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 GUIOk, 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'.
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 forms 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.
![]() Next, we add some more objects to the form: ![]() 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 >> |
||||||||||||||||||||||




