ticTacToe program, logic help!

This is a discussion on ticTacToe program, logic help! within the C Programming forums, part of the General Programming Boards category; Hi, thanks for looking at this. Here is what i have so far: Code: int printBoard(int x, int y, char ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    ticTacToe program, logic help!

    Hi, thanks for looking at this.

    Here is what i have so far:
    Code:
    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
    Code:
    return printf( "%c",board[x][y] );
    I also need help understanding how to use a function like this and some how incorporating this function:

    Code:
     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
    Posts
    3,211
    Either declare your board array globally or in a function that calls other functions.

    So for example:

    Code:
    int main(void)
    {
        char board[3][3];
        ....
        printBoard(board);
        ....
    }
    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, 06:49 PM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. logic on a program
    By officegirl in forum C Programming
    Replies: 0
    Last Post: 10-13-2001, 10:41 PM

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