Wednesday, December 30, 2009

TIC TAC TOE - 'C' code and Algorithm - Part 1

TIC TAC TOE

I’m back with a game this time. Well, this might be a huge tutorial, but after this most of the newbie problems will perish. So let’s get started.

Introduction to game:

In a game of Tic Tac Toe, two players take turns making an available cell in a 3X3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical or diagonal row on the grid, the game is over and that player has won. A draw (No winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win.

Algorithm:

Let’s just note down steps to be covered.

(1) Take input position from player.
(2) Check whether respective player wins with that position.
(3) If not, continue start again from 1st step.

It’s so simple to analyse these things, but there is lot more to do to achieve these steps.

Selecting Data Structure:

We need a data structure to store the values of both the players. We have 9 cells and so we need 9 blocks, to store.

Let’s make this program simple by selecting simple data structure, arrays. So we are now left with two options, single and 2-Dimensional arrays.

I selected 2-dimensional array as I have to traverse through rows and columns (I will make it clear) and diagonals too. I will later try to deal the same cases with single dimensional array.

To learn how to traverse a matrix, please read this post.(I will update this ASAP)

Take input position from player

3X3 grid has 9 cells. So let’s name them from 1 to 9.

This is a two human player game and so we will depend on user itself. System just validates the games.

The input taken from user has to be checked whether it is between 1 and 9.

Let the take input be stored in variable named ‘pos’. And so following will be a simple and clear code to validate it.

Hope that you know the syntax and how a do-while loop works, if not go through following post.(I will update this ASAP)

When the given input, ‘pos’, is not in the prescribed range, loop is executed again and this continues until users enters a value in between 1 and 9.

Until now, we had a candy. Now let’s increase the pace.

It is obvious that we need to see that the validated position is not used earlier.
Isn’t that a new point!!! We shouldn’t take repetitive positions. And so another validation is required to see whether given position has been used earlier or not.
To do so, we need to keep a track of positions which we have used initially. So we need a storage place for positions also (we took one 2-dimensional array for 3X3 grid)

I will consider a single dimensional array of size 9 storing the information whether the particular position has been used or not.

Let us name it ‘position’.

Int position[9];

And I will initially place zero in every element of it, meaning that particular position has not been used.

for(i=0;i<9;i++) position[i]=0;

Now we have to check whether given position, ‘pos’, has been used earlier or not.

We are collecting positions from 1 to 9, but the index of an array starts from 0. So we need to subtract one from the position we got so to go for respective index.

Input range: 1 to 9
Array range: 0 to 8

As said earlier, 0 represents that position has not been used yet and 1 states that it has been used. So the first ‘if’ condition checks whether given position’s respective index’s value is equal to 1, if so, it states that position is already used.

If not, value of the respective position is made 1.

But when the position is already is used, we need to ask the user to enter a new position.