Thursday, July 30, 2009

How can i make a program
in the game tic tac toe:
using c programming?

i have a machine problem 2 be passed next week. its about c programming!! hehehe! tic tac toe was the game.. any one please help!

How can i make a program


in the game tic tac toe:


using c programming?
It's simple. You create a 9-cell string array. You initially populate the array with its current cell index; that is, array[0] has a value of 0; array[1] has a value of 1, etc.





Then, you display a 3 x 3 grid, where the upper left hand corner is 0 and the lower right hand corner is 8, by printing out each cell's value, like this:





0 | 1 | 2


----------


3 | 4 | 5


----------


6 | 7 | 8





You prompt the user to pick a square. You check to see if that value is greater than or if the square currently has an X or O. If yes to either, you prompt him to choose a different cell. If not, you replace the value of the chosen array cell with an X.





You examine the array for a win. A win is any horizontal, vertical or diagonal row of the same symbol. Thus, if array[0], array[1] and array[2] all have the same value, that's a win; if array[1], array[4] and array[7] have the same symbol, that's a win; if array [0], array[4] and array[8] have the same value, that's a win.





If there is no win, you set the value of a simple boolean to true.





You enter a do loop that continues while your boolean is true.


The do loop first calculates a random number from 0 to 8. It then looks in the array cell for the random number it generated and sees if there is an X or O in that cell.





If so, it loops back, generates a new number, and tries again.





If not, the boolean is set to false and you enter O in the cell number you generated.





You then check, again, for a win. If there is no win, loop back to the start and repeat.





The grid after the first pass might look like this:





X | 1 | 2


----------


3 | O | 5


----------


6 | 7 | 8

periwinkle

No comments:

Post a Comment