Thread: Help! tic tac toe program C.... can someone just help me fix this mess?( int main)

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    Help! tic tac toe program C.... can someone just help me fix this mess?( int main)

    the player moves has too few arguments which I have no clue. Also I haven't finished the main function yet because I also have errors there......


    Code:
    #include <stdio.h>
    
    
    
    
    
    
    void info()
    {
     printf("TIC-TAC-TOE\nWritten by me\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;
     gameBoard();
     printf("Your Move (0-8): ");
     scanf("%d", &i);
     while ( i < 0 || i > 8 )
          { printf("Invalid Move"); userMove(); }
    
    
    }
    
    
    void computerMove(char cell[])
    {
    scanf( cell[0]=' ' );
    scanf( cell[1]=' ' );
    scanf( cell[2]=' ' );
    scanf( cell[3]=' ' );
    scanf( cell[4]=' ' );
    scanf( cell[5]=' ' );
    scanf( cell[6]=' ' );
    scanf( cell[7]=' ' );
    scanf( cell[8]=' ' );
    
    
    }
    
    
    /* Check for a winning line - diagonals first */
    char checkWinner (char cell[])
    {
            if(cell[0] == cell[1] == cell[2])
            {
                    if(cell[0] != ' ')
                    {
                    return cell[0];
                    } else return ' ';
            } else if(cell[3] == cell[4] == cell[5])
            {
                    if(cell[3] != ' ')
                    {
                    return cell[3];
                    } else return ' ';
            } else if(cell[6] == cell[7] == cell[8])
            {
                    if(cell[6] != ' ')
                    {
                    return cell[6];
                    } else return ' ';
            } else if(cell[0] == cell[3] == cell[6])
            {
                    if(cell[0] != ' ')
                    {
                    return cell[0];
                    } else return ' ';
            } else if(cell[1] == cell[4] == cell[7])
            {
                    if(cell[1] != ' ')
                    {
                    return cell[1];
                    } else return ' ';
            } else if(cell[2] == cell[5] == cell[8])
            {
                    if(cell[2] != ' ')
                    {
                    return cell[2];
                    } else return ' ';
            } else if(cell[0] == cell[4] == cell[8])
            {
                    if(cell[0] != ' ')
                    {
                    return cell[0];
                    } else return ' ';
            } else if(cell[2] == cell[4] == cell[6])
            {
                    if(cell[2] != ' ')
                    {
                    return cell[2];
                    } else return ' ';
            } else return 'd';
    
    
    
    
    }
    int main()
    
    
    info();
    init();
    Last edited by Dom; 10-04-2011 at 12:40 AM.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Did you try compiling this with warnings turned up and looked at the warnings? That should give you a few ideas on where to start.

    Code:
    $ gcc -Wall -Wextra -pedantic cboard.c -o cboard
    cboard.c: In function 'userMove':
    cboard.c:34:2: error: too few arguments to function 'gameBoard'
    cboard.c:20:6: note: declared here
    cboard.c:38:2: error: too few arguments to function 'userMove'
    cboard.c:31:6: note: declared here
    cboard.c:31:20: warning: unused parameter 'cell'
    cboard.c: In function 'computerMove':
    cboard.c:45:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:46:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:47:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:48:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:49:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:50:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:51:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:52:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c:53:2: warning: passing argument 1 of 'scanf' makes pointer from integer without a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/stdio.h:347:37: note: expected 'const char *' but argument is of type 'char'
    cboard.c: In function 'checkWinner':
    cboard.c:60:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c:66:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c:72:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c:78:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c:84:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c:90:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c:96:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c:102:9: warning: suggest parentheses around comparison in operand of '=='
    cboard.c: In function 'main':
    cboard.c:117:1: error: expected declaration specifiers before 'info'
    cboard.c:118:1: error: expected declaration specifiers before 'init'
    cboard.c:118:1: error: expected '{' at end of input
    cboard.c:118:1: warning: control reaches end of non-void function
    The first thing you should do is to create your main function and call info and init from there, you cannot call them outside a function as it is now. Start with changing your last lines of code to:

    Code:
    int main(void)
    {
        info();
        init();
        return 0;
    }
    As for the errors about too few arguments, look at how you call the functions userMove and gameBoard and see if the number of arguments match with the function definition... Sort these things out and try to correct the rest of the errors/warnings. Then come back if you're still having problems with any of them.

    It wouldn't hurt to properly indent your code for better readability either.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    ok, i am aware of those errors and warnings

    i also stated int main is not finished because of such errors...

    I just need someone to tell me if the check winner is ok, if the usermove is good, and then i will stack together the main.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Dom View Post
    i also stated int main is not finished because of such errors...

    I just need someone to tell me if the check winner is ok, if the usermove is good, and then i will stack together the main.
    Why don't you just test it yourself?

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    ahhh no I mean, I need help fixing those...

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Dom View Post
    ahhh no I mean, I need help fixing those...
    Have you looked at the error messages? Do you understand what they mean at all? It says quite clearly that you are calling a function but not passing enough arguments. So you need to look at the function definition and see what is missing. Then move on to the other warnings and errors.

  7. #7
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    I would start off declaring your char array, you need to do that before you can use it.


    Code:
    int main(void) 
    {
        
    char cell[9]; info(); init(cell);
    // Now what??
    return 0;
    }
    Next I would put a loop in main for user move, if nobody has won keep going.

    Make some changes and post your new code.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A development process
    Start small and compile often (and test almost as often).

    Writing 100+ lines, then finding out "oh no, all those errors - what shall I do? - I know, dump the whole sorry mess on a forum for someone else to fix" isn't the way to go.

    You should just delete computerMove() and checkWinner() for now, until you've got the basics working. They're very broken anyway, so you're not going to lose much by just pressing the delete key on them.
    But the first 40 lines look very good - you should keep them.

    Like for example, start simple with
    Code:
    void userMove(char cell[])
    {
     int i;
     gameBoard();
     printf("Your Move (0-8): ");
     scanf("%d", &i);
    }
    
    int main()
      info();
      init();
    }
    Now, it's a much smaller problem with a lot fewer errors to fix.

    Now carefully update it to the point where userMove() can make ONE valid move.
    Until you can do that much, there isn't any point at all in worrying about a computer move, or checking for a winner.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return to main or restart program
    By behzad_shabani in forum C Programming
    Replies: 5
    Last Post: 06-29-2008, 04:58 AM
  2. How to call main of one program in other
    By rinkle_lalwani in forum C Programming
    Replies: 4
    Last Post: 11-28-2005, 12:47 PM
  3. main() at bottom of program
    By jamie85 in forum C Programming
    Replies: 4
    Last Post: 02-27-2005, 01:57 PM
  4. Warnings that mess up the program...
    By Dark Nemesis in forum Windows Programming
    Replies: 9
    Last Post: 08-31-2003, 06:58 PM
  5. C program without main()
    By satish_ram in forum C Programming
    Replies: 5
    Last Post: 09-18-2001, 06:24 AM