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 2
Oldest  Newest  Rating
0 Jon Z. · June 17, 2015
Hi, I just spent 2 hours writing the program.
The only problem I find so far is that is that it doesn't display properly who won, when someone wins it always says Player #1 won. I think I made some mistake in the checkWhoWon function.
Here is the code:

* tic.c
* Tic Tac Toe
* by Jon Z.


#define SYSTEM_CLEAR "cls"   //"cls" for windows. for unix-based systems replace "cls" with "clear"

void clear();
void displayGameState();
void userInput(int user);
int checkWhoWon();
int checkIfFull();

char pos[3][3]; //array to story state of the game


    clear(pos); //set every element in the array to ' ' (space)
    int gameOver = 0; //set to 1 to end game
    int whoWon = 0; //player 1 or 2? 0 for a tie

    //loops until someone wins, or all the positions are taken
    while(gameOver == 0){

        whoWon = checkWhoWon();
        if(whoWon != 0){
            gameOver = 1;
            break;  //exits loop if someone won
            gameOver = 0;

        gameOver = checkIfFull();
        if(gameOver != 0){
            break;  //exits loop if array is full and it's a tie


        whoWon = checkWhoWon();
        if(whoWon != 0){
            gameOver = 1;
            gameOver = 0;

        gameOver = checkIfFull();
        if(gameOver != 0){  //this is not necesary sincsince it's at the end of the loop, but i just copied it from before
            break;  //exits loop if array is full and it's a tie
    } //end of loop

    if(whoWon =! 0){
        printf("\n\tPlayer #%d Won!!!\n\n", whoWon);
        printf("\n\tIt's a tie\n\tGAME OVER!\n\n");

    return 0;

//set every element in the array to ' ' (space)
void clear(){
    int i, j;

    for(i=0; i
0 Alan Johnson · June 16, 2015
No problem, glad to help you
0 Reynaldo Gunawan · June 16, 2015
@AlanJohnson alright, i'm beginning to understand it. Thanks a lot for your work and replies.
+1 Keyno Neile · June 16, 2015
Nice programing, its really neat and well documented  @jonas Meise
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.
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 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 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...
+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 Jonas Meise · June 13, 2015
the places where you see numbers are just buttons I didn't click yet.



One of the most popular languages of all time.

Bucky Roberts Administrator