Like Tree1Likes

New Tic Tac Toe program. Only need help with int main function...

This is a discussion on New Tic Tac Toe program. Only need help with int main function... within the C Programming forums, part of the General Programming Boards category; can you please explain the return int a little more???? thanks Code: #include <stdio.h> #include <stdlib.h> void info() { printf("TIC-TAC-TOE\nWritten ...

  1. #16
    Dom
    Dom is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    OK i simplified it and made it shorter and removed the return x...return o...

    can you please explain the return int a little more???? thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void info()
    {
        printf("TIC-TAC-TOE\nWritten by MEEEE\n");
        printf("Your Symbol 'x'");
        printf("Computer Symbol 'o'");
    }
    
    
    /*initialize board*/
    void init(char cell[])
    {
        int i;
        for (i=0; i<9; i++)
            cell[i] = ' ';
    }
    
    
    /*print gameboard */
    void gameBoard(char cell[])
    {
        printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
    }
    
    
    /*player moves*/
    void userMove(char cell[])
    {
        int i = 0;
        int cont = 1;
    
    
        do{
            printf("Your Move (0-8): ");
            scanf("%d", &i);
    
    
            if((cell[i] != ' ') || (i < 0 || i > 8))
            {
                printf("Invalid Input\n");    //invalid input
            }
            else
            {
                cell[i] = 'x';
                cont--;        //set while loop to false, break
            }
        }
        while(cont);
    }
    
    
    
    
    /*computer moves*/
    void computerMove(char cell[])
    {
        int i = 0;
        int cont = 1;
    
    
        do
        {
            if(cell[i] == ' ')
            {
                cell[i] = 'o';
                cont--;
            }
            else
            {
                i++;
            }
    
    
        }while(cont);
    
    
    }
    
    
    char checkWinner(char cell[])
    {
    int i;
    
    
    // Diagonals
    if (cell[4]!=0 && (cell[4]==cell[0] && cell[4]==cell[8] || cell[4]==cell[2] && cell[4]==cell[6]))
    return cell[4];
    
    
    // Horizontals
    for (i=0; i<9; i+=3)
    if (cell[i]>' ' && cell[i]==cell[i+1] && cell[i]==cell[i+2])
    return cell[i];
    
    
    // Verticals
    for (i=0; i<3; i++)
    if (cell[i]>' ' && cell[i]==cell[i+3] && cell[i]==cell[i+6])
    return cell[i];
    
    
    // No match
    return ' ';
    }
    
    
    
    
    
    
    int main()
    {
        char cell[9];
        int cont = 1;
        int ccode = 0;
    
    
        info();
        init(cell);
        gameBoard(cell);
        while(cont)
        {
    userMove(cell);
    gameBoard(cell);
    
    
    
    
    //check if user wins
    //if user wins, print user won
    //and cont-- to exit loop
    computerMove(cell);
    gameBoard(cell);
    
    
    checkWinner(cell);
    
    
    //check if computer wins, print computer won
    //and cont-- to exit loop
    //if nobody won get another user input
          }
    
    
        return 0;
    }

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    return stops the current function on that line, and returns back to whatever function called it; optionally returning a value to that function.
    Code:
    int foo( void )
    {
        return 3; /* send 3 back to main */
    }
    
    int main( void )
    {
        int x;
        x = foo();
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Dom
    Dom is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    i was actually talking to the other guy since he's actually more helpful.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well it's a good thing C cares about who explains it, because it would make no sense for a language to have set rules that anyone could explain to you.


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

  5. #20
    Dom
    Dom is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    Could you guide me into how to get the checkWinner to actually do its job during the game? It is not working as of now....

  6. #21
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Code:
    int checkWinner(cell)
    {
    //Do all your checks //if you found out someone won, //check if that cell is an 'x' or an 'o' //if it's an 'x' return 1 //if it's an 'o' return 2 //if no one won, return 0;
    }
    Then in your main if return is 1, print user wins, if return is 2, computer wins, if return is 0 get another move.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you check a winner on paper? Check each column. Check each row. Check each diagonal. Don't try to do the whole thing at once. Do one. Check one column for a winner. Then figure out how to do that for each column. Then do the same thing for the other two. Don't worry about the entire thing at once. Just do one column, or one row, or one diagonal. Then work up from there.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Dom
    Dom is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    rmatze

    do you think you could just give that one to me pleaaaase....it's so late I don't really know anymore.......

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  2. Replies: 9
    Last Post: 05-08-2011, 12:14 PM
  3. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 12:31 PM
  4. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  5. Replies: 2
    Last Post: 03-30-2009, 02:06 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21