Programming Tic Tac Toe

0 Reynaldo Gunawan · June 13, 2015
For my next project, i am asked to create Tic Tac Toe in C. The only clue that i got is that i need 3-dimensional array, as the users will put in the number of column and row to indicate which box they want to play. 

The question also says that i need to prove whether the box is already full or whether one of the player has his/her symbol three times in a line, well everyone knows Tic Tac Toe.

I know that i need a tremendous amount of functions to make this program, but i don't even know where to start and which and how many variables i would need. I've created the structure of the game without the number like the photos below, but that's only like 1/1000 done of what i need to do. I am so screwed...

If someone has any ideas, please let me know. I'm not expecting a whole complete program, as i would not learn as much from it anyway. I appreciate your replies.

Example of the format:


Post a Reply


- page 1
Oldest  Newest  Rating
+4 Jonas Meise · June 13, 2015
Ye I'm okay with it^^ I started writing that tic tac toe when I saw your post because I wanted to know if I was able to do it with java^^

I tried to comment the code so you understand  what I have done where and why^^ Hope it's useful to you :)

Here is what the program would look like if you executed it:

+2 Alan Johnson · June 13, 2015
You certainly don't need a 3D array, you don't even need a 2D array for that matter.

I would break the project down into smaller parts. For tic-tac-toe you probably want functions for the following:

  • Get input from the user for where to move

  • Error check the input to make sure nobody has already played in that position

  • Make a move (put X or O in a cell)

  • Switch to the other player's turn

  • Check if a player has won by analyzing the cells in vertical, horizontal and diagonal directions.

  • Print the game to the console (seems like you're already on with that)

+1 Keyno Neile · June 16, 2015
Nice programing, its really neat and well documented  @jonas Meise
+1 Alan Johnson · June 23, 2015
Fixed it up for you, let me know if its not as you want it.
+1 Alan Johnson · June 14, 2015
Wrote a version in C for you. I've left comments in the code but just ask if you don't understand anything.

The formatting is weird on here so I've put it on pastebin:
0 Reynaldo Gunawan · June 26, 2015
Try to check the first page. There are many useful informations that have been posted to start from scratch ^^
0 Reynaldo Gunawan · June 14, 2015
@JonasMeise well it didn't seem for you to take a long time to make the whole program :P Gosh, when can i be like that... Thank you once again. Though i have to make some connections to C, it's still relatively understandable even for me. Phew i know something...

@AlanJohnson wow thank you! I really appreciate it! I will definitely ask questions since i'm still a noob. 

Guys, i'm really thankful for what you're doing, but isn't this a bit too much? Creating the whole program and all haha i mean if you guys enjoy it, then it's fine. I feel like i'm burdening the media :P or maybe it's just me...
0 Jonas Meise · June 14, 2015
Hehe :P 
Well the reason I like helping out with such things mainly is because I get better through practice^^ The project I had before this was a calculator and then I ran out of ideas what to program so the idea with tic tac toe seemed good to me :P
0 Reynaldo Gunawan · June 15, 2015
@AlanJohnson may i ask you questions on your code? This might be beginner's questions, but i don't quite get how the function make_move (Line 50) work. And I'm having difficulties understanding the code inside the while-loop (Line 116). Can you please explain a little bit more, if you don't mind.

I don't know why i put the line number there, i mean it's your own code.

0 Alan Johnson · June 15, 2015
make_move puts either an X or O in the position the user wants to move in, depending on whose turn it is.
turn ? says if the turn variable is true(1)  then then put a "O" in the cell, otherwise put an "X" in the cell.
 board[pos] = turn ? "O" : "X";

Then it switches to the other player's turn, if it was 1 before (player O), it becomes 0 (player X),  and vice versa, so the other player can make a move next.
turn = !turn;

The while loop continuously executes until the game is over, it asks the user for what position to play in, puts an X or O in the cell they want to move in and finally shows them what the game board looks like now after their turn.



One of the most popular languages of all time.

Bucky Roberts Administrator