1. Home
  2. Computing & Technology
  3. Delphi Programming
Your First Delphi Game: Tic Tac Toe
Page 4: New Game?
 More of this Feature
• Page 1: Preparing the GUI
• Page 2: Game Initialization
• Page 3: Player X, your turn!

• Download full source CODE

Printer friendly versionPrinter friendly version
 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

There are two more handlers we have to code: create a procedure for a new game, and a procedure that will reset the score.

New Game
We already have the InitPlayGround procedure, therefore creating a new game would be a peace of cake. All we have to do is call the InitPlayGround.

Note: a player can click on the btnNewGame button after the game is finished (bGameOver = True) or during a game (bGameOver = False). Because of that it would be nice to ask the user to confirm his decision.

In this particular case there would be negligible damage or there would be no damage at all. Just remember that good programming practice implicate getting the user's confirmation.

Double click the btnNewGame button to create and add the code for the New Game option:

procedure TfrMain.btnNewGameClick(Sender: TObject);
begin
 if bGameOver = False then
 begin
  if MessageDlg(
      'End the current game?', 
      mtConfirmation, 
      mbOKCancel,0) = mrCancel then Exit;
 end;

 InitPlayGround;
end;

Note: if you are using a Non English Delphi version and want to have dialog buttons and messages on your local language, explore the article: "The fastest path to Delphi localization".

Reseting the Scoreboard
Recall that the iXScore and iOScore variables hold the number of wins for each player. Oponents read the score form the lblXScore and lblOScore labels.

The task is simple: set iXScore and iOScore values to zero, and update lblXScore.Caption and lblOScore.Caption.

Off course, get the user's confirmation first.

procedure TfrMain.btnResetScoreClick(Sender: TObject);
begin
 if MessageDlg(
   'Reset the scores?',
   mtConfirmation,
   mbOKCancel,0) = mrCancel then Exit;

 iXScore := 0;
 iOScore := 0;
 lblXScore.Caption := IntToStr(iXScore);
 lblOScore.Caption := IntToStr(iOScore);
end;

Delphi plays Tic Tac Toe
Some exercises for you...
Please, take a look at that CheckWin function again. Note that in case of a "we have a winner" it returns an integer number from 0 to 8. What is this number for? See the picture:

TicTacToe win possibilities

If we have a winner and the winner has occupied the middle row, the CheckWin will retun 2.
Your excersise (for this chapter) is to create a new function to mark (in any way you want) a victory on a playfied. For example, you could change the background color for the three labels making the wining combination. Or, draw an ellipse arround the cells, it's up to you... ;)

Anyway be sure to see the full Tic Tac Toe game code.

   To the next chapter: A Beginner's Guide to Delphi Programming
This is the end of the tenth chapter, in the next chapter, we'll deal with creating your first MDI (multiple document interface) application.

If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

First page > Preparing the Game GUI > 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.