Thread: ticTacToe program, logic help!

  1. #1
    Registered User
    Join Date
    Oct 2007

    ticTacToe program, logic help!

    Hi, thanks for looking at this.

    Here is what i have so far:
    int printBoard(int x, int y, char array)
      int x;  //row
      int y;  //column
      char board[3][3]; /* declares an array for the gameboard */
      for ( x = 0; x < 3; x++ ) {
        for ( y = 0; y < 3; y++ )
          board[x][y] = '-'; /* Set each element to a dash */
      printf( "Tic-Tac-Toe board:\n" );
      for ( x = 0; x < 3;x++ ) {
        for ( y = 0; y < 3; y++ )
        printf( "%c",board[x][y] );
    So this basically prints a 3 x 3 matrix with '-' in each position. One question is how can i return the output to my main function? can i say
    return printf( "%c",board[x][y] );
    I also need help understanding how to use a function like this and some how incorporating this function:

     int userMove() {
      a = scanf("%d%d");
        i = a / 10;
        j = a%10;
        board[i][j] = 'x';    //user is an X
    So this just assigns an 'x' to a user specified location in the 3x3 matrix. I can't get my mind around how to save this into the printBoard function. It continues to print out a 3x3 matrix full of dashes but when the user inputs the first 'X' how do i tell the function to exclude that certain position and instead print the 'x'. Any help understanding this would be great.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Either declare your board array globally or in a function that calls other functions.

    So for example:

    int main(void)
        char board[3][3];
    Incidentally, your printBoard() function has some weird things. You have x and y passed to the function, but then redeclared, which is rather silly. You also pass a char to the function but you call it array. Also, returning the result of printf() is not what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. logic on a program
    By officegirl in forum C Programming
    Replies: 0
    Last Post: 10-13-2001, 10:41 PM