Tic Tac Toe - Game programming
This tutorial will teach how to code the game Tic Tac Toe. The game is pretty simple , all you have to do is to get your symbols(usually ‘X’ or ‘0’) in straight line of three .
Graphic Library Used - Turbo C/C++ Bgi
For this program we assume Turbo C++ 3.0 IDE and installed in folder C:\TC (Please choose same directory name to keep things simple).
This program is essentially console based and works in Windows XP (or) lower and requires to be run in fullscreen mode.
We first create data the game needs . In this game we need following data -
We need a 2D 3 X 3 Matrix to store the board. Each member of the board contains one the three values -
- 'O' if there is a zero at that place.
- 'X' if there is a cross at that place
- '*' if there is no element at that place ie. placed has not yet been filled.
- Now that the game data is ready, we initialize the entire board to '*'. This indicates that no block is filled.
We use a function called startboard() called in main to do this job for us.
Thus the code developed is:
Now that the game is setup we have to initialize the graphics engine and draw a graphical image of the board from the array data.
The function drawboard() does this job. It first clears all the graphics on the present screen. Then it initiates the graphics and redraws the entire screen. It then draws the border lines. And thereafter draws a cross, a circle or nothing depending upon the values in the array.
It uses function drawcircle() and drawcross() to draw circles and cross respectively. These function take input value 0 to 9 depending upon the array position to decide what to draw.
This function is first called in initialization but will be called several times
we call this function drawboard()-
Game Engine -
The game engine is basically a loop that terminates after 5 user moves. It must do the following jobs in one sequence for every user turn:
• Take user input .
• Place user move on board.
• Calculate computer move.
• Place computer move on board.
• Refresh Screen for next round.
• Check wether there is a winner.
we use function play to be name of our game engine and game exits when the function exits. This function is called once in main.
We create a function named user_input() which does the first two jobs of our game engine. It takes user input and if valid places that on to the data.
User is asked to input X coordinate (1 - 3) and Y coordinate (1 - 3).
If in our data board [X-1] and [Y-1] position is '*' then we place a 'X' There otherwise re-prompt for input.
The required functions are:
Now after user move we need to make the computer move. Since this first version of the Article we will be using simple AI .
Our AI starts from the first box and where ever it finds first empty space it puts a zero.
We call this function computer.
The required functions are
Now last thing our game engine must do is to see wether we have a winner or not.
This function use a flag variable key (arbitary name) which is initialise to '*'
Then our function checks all possible combination of winning and if it finds a character other than '*' in series of 3 it sets key value to it.
Then we calculate whether player wins, computer wins or nobody wins and game is still
Now here is the final code of this version -