Core C++

Lesson 9 : Tic Tac Toe

Overview

In this lesson, we wrote a simple video game using the material that we covered in the previous lessons. Despite the small amount of material covered thus far, this lesson shows that we can accomplish quite a bit already. The intention of this lesson has been to put the material to use in a larger program and to give an introduction to game programming, specifically.

Download Code

For anyone who is interested in writing games in C++, this video gives a basic idea of what is involved. Almost without exception, every game will have a main game loop like the one shown here. In turned-based games, the loop will tend to run once for each player's turn or once for all of the player's turns. In real-time games, the game-loop will run once for each screen repaint and will check for events like player input, collisions, etc. The basic algorithm for Tic Tac Toe looks like this:

Initialization: Variable declaration and initialization

Game loop:

        Display board

        Get a player's move

        Check end of game condition

                8 Wins

                1 Draw




Game loops differ from game to game, obviously. However, there are some key elements, which we will often separate from each other. For example, there will almost always be input, movement, a check for events, and an updated display. By separating stages from each other, we cut down the confusion in programming—an important point since even relatively simple games often have fairly complex interactions.

Typically, we will process the loop in the order shown above and check for events after everything has moved. Here, our check for events is simply a check for the end of the game. In other games, the events will be more complex like object collisions. In these cases, the state of objects in the game change and we will want to let the display know that we need to render something else like an explosion, for example. The loop in tic tac toe above has the same basic ordering, except that we started with the display stage first. However, the order of the stages is the same.

 

© 2007–2024 XoaX.net LLC. All rights reserved.