Thread: help with tictactoe

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    10

    help with tictactoe

    i need to write a program that
    1) Prompts a user to enter the current state of a board.
    2) Print out the board
    3) Determines if there is a winner, and, if there is, who it is.

    this is what i have, and nothing happens when i use it

    Code:
    #include<stdio.h>
    
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
    
    }
    
    void enter_symbol(char tic_tac_toe[][2], int player)
    {
      int row, col;
      while(1){
       printf("Where would u like to enter symbol\n", player);
       scanf("%d", &row);
       scanf("%d", &col);
       if (tic_tac_toe[row][col]==' ')
       {
         if (player == 1)
          tic_tac_toe[row][col]=='x';
         else
          tic_tac_toe[row][col]='o';
         break;
       }
         else
          printf("Position already taken\n");
      }
    }
    
    void print_board(char tic_tac_toe[][2])
      {
       printf("\n %c|%c|%c\n -----\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
       printf(" %c|%c|%c\n -----\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
       printf(" %c|%c|%c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);
      }
    
    
    int has_won (char tic_tac_toe[][2]){
    
      if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
          tic_tac_toe[0][1] == tic_tac_toe[0][2]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[1][2]){
        printf("%c has won!\n", tic_tac_toe[1][0]);
        return (1);
      }
      else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[2][0]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
               tic_tac_toe[1][0] == tic_tac_toe[2][0]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][1]){
        printf("%c has won!\n", tic_tac_toe[0][1]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][0]){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
     }
      else
        printf("Tie game\n");
    
     return(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nothing happens when you use it, because you don't call any functions in main. So how about showing us code you actually wrote?


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

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    that was a mistake, i took that part out when i was messing around with the code and i never put it back...still doesn't work

    Code:
    #include<stdio.h>
    
     void enter_symbol(char tic_tac_toe[][2], int player);
     void print_board(char tic_tac_toe[][2]);
     int has_won (char tic_tac_toe[][2]);
    
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
     
     }
    
    void enter_symbol(char tic_tac_toe[][2], int player)
    {
      int row, col;
      while(1){
       printf("Where would u like to enter symbol\n", player);
       scanf("%d", &row);
       scanf("%d", &col);
       if (tic_tac_toe[row][col]==' ')
       {
         if (player == 1)
          tic_tac_toe[row][col]=='x';
         else
          tic_tac_toe[row][col]='o';
         break;
       }
         else
          printf("Position already taken\n");
      }
    }
    
    void print_board(char tic_tac_toe[][2])
      {
       printf("\n %c|%c|%c\n -----\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
       printf(" %c|%c|%c\n -----\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
       printf(" %c|%c|%c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);
      }
    
    
    int has_won (char tic_tac_toe[][2]){
    
      if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
          tic_tac_toe[0][1] == tic_tac_toe[0][2]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[1][2]){
        printf("%c has won!\n", tic_tac_toe[1][0]);
        return (1);
      }
      else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[2][0]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
               tic_tac_toe[1][0] == tic_tac_toe[2][0]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][1]){
        printf("%c has won!\n", tic_tac_toe[0][1]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][0]){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
     }
      else
        printf("Tie game\n");
    
     return(0);
    }

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
     
     }
    main() still doesn't do anything. You need to actually call the other functions you made.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    when i use this, it says theres a syntax error before "]" token on line 14, 16, and 18

    Code:
    #include<stdio.h>
    
    void enter_symbol(char tic_tac_toe[][2], int player);
    void print_board(char tic_tac_toe[][2]);
    int has_won(char tic_tac_toe[][2]);
    
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
     int player;
    
     enter_symbol(tic_tac_toe[][2], player);
    
     print_board(tic_tac_toe[][2]);
    
     has_won(tic_tac_toe[][2]);
    
      return(0);
    }
    
    void enter_symbol(char tic_tac_toe[][2], int player)
    {
      int row, col;
      while(1){
       printf("Where would u like to enter symbol\n", player);
       scanf("%d", &row);
       scanf("%d", &col);
       if (tic_tac_toe[row][col]==' ')
       {
         if (player == 1)
          tic_tac_toe[row][col]=='x';
         else
          tic_tac_toe[row][col]='o';
         break;
       }
         else
          printf("Position already taken\n");
     }
    }
    
    void print_board(char tic_tac_toe[][2])
      {
       printf("\n %c|%c|%c\n -----\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
       printf(" %c|%c|%c\n -----\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
       printf(" %c|%c|%c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);
      }
    
    
    int has_won(char tic_tac_toe[][2]){
    
      if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
          tic_tac_toe[0][1] == tic_tac_toe[0][2]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[1][2]){
        printf("%c has won!\n", tic_tac_toe[1][0]);
        return (1);
      }
      else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[2][0]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
               tic_tac_toe[1][0] == tic_tac_toe[2][0]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][1]){
        printf("%c has won!\n", tic_tac_toe[0][1]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == tic_tac_toe[2][2]){
     printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][2]){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][0]){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else
        printf("Tie game\n");
    
     return(0);
    }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Just use the array name when calling a function. No brackets.
    Code:
    print_board(tic_tac_toe);
    All of your function prototypes and definitions should be
    char tic_tac_toe[][3] instead of char tic_tac_toe[][2]


    A bug in enter_symbol
    Code:
    tic_tac_toe[row][col]=='x';

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    i think i may have taken the long way with this code, but all i need for it to do now is repeat asking the player to enter another symbol...instead of it saying "Tie game" after the first move


    Code:
    #include<stdio.h>
    
    void enter_symbol(char tic_tac_toe[][3], int player);
    void print_board(char tic_tac_toe[][3]);
    int has_won(char tic_tac_toe[][3]);
    
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
     int player;
    
     enter_symbol(tic_tac_toe, player);
    
     print_board(tic_tac_toe);
    
     has_won(tic_tac_toe);
    
      return(0);
    }
    
    
     void enter_symbol(char tic_tac_toe[][3], int player)
     {
      int row, col;
      while(1){
       printf("Where would u like to enter symbol\n", player);
       scanf("%d", &row);
       scanf("%d", &col);
       if (tic_tac_toe[row][col]==' ')
       {
         if (player == 1)
          tic_tac_toe[row][col]=='x';
         else
          tic_tac_toe[row][col]='o';
         break;
       }
         else
          printf("Position already taken\n");
      }
     }
    
     void print_board(char tic_tac_toe[][3])
     {
       printf("\n %c|%c|%c\n -----\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
    
       printf(" %c|%c|%c\n -----\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
    
       printf(" %c|%c|%c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);
    
     }
    
    
     int has_won(char tic_tac_toe[][3]){
    
      if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
          tic_tac_toe[0][1] == tic_tac_toe[0][2] &&
          tic_tac_toe[0][2] == 'x'){
       printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
               tic_tac_toe[0][1] == tic_tac_toe[0][2] &&
               tic_tac_toe[0][2] == 'o'){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
    
      else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == 'x'){
        printf("%c has won!\n", tic_tac_toe[1][0]);
        return (1);
      }
      else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == 'o'){
        printf("%c has won!\n", tic_tac_toe[1][0]);
        return (1);
      }
    
      else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] == 'x'){
        printf("%c has won!\n", tic_tac_toe[2][0]);
        return (1);
      }
      else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] == 'o'){
        printf("%c has won!\n", tic_tac_toe[2][0]);
        return (1);
      }
    
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
               tic_tac_toe[1][0] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] == 'x'){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
       else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
               tic_tac_toe[1][0] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] == 'o'){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
    
      else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == 'x'){
        printf("%c has won!\n", tic_tac_toe[0][1]);
        return (1);
      }
       else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == 'o'){
        printf("%c has won!\n", tic_tac_toe[0][1]);
        return (1);
      }
    
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] == 'x'){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] == 'o'){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
    
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] == 'x'){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] == 'o'){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
    
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] == 'x'){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] == 'o'){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
    
      else
        printf("Tie Game\n");
    
      return(0);
     }

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    i think i may have taken the long way with this code
    Yup. For example you could easily cut the has_won function in half by replacing code like
    Code:
    if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
        tic_tac_toe[0][1] == tic_tac_toe[0][2] &&
        tic_tac_toe[0][2] == 'x'){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
    }
    else if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
              tic_tac_toe[0][1] == tic_tac_toe[0][2] &&
              tic_tac_toe[0][2] == 'o'){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
    }
    with this:
    Code:
    if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
        tic_tac_toe[0][1] == tic_tac_toe[0][2] &&
        tic_tac_toe[0][2] != ' ') {
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return 1;
    }

    but all i need for it to do now is repeat asking the player to enter another symbol...instead of it saying "Tie game" after the first move
    Use a loop? Like this:
    Code:
    int main(void)
    {
        char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
        int player = 1;
        int i;
    
        for (i = 0; i < 9; i++) {
            enter_symbol(tic_tac_toe, player);
            print_board(tic_tac_toe);
            if (has_won(tic_tac_toe))
                break;
            player = !player;
        }
    
        return 0;
    }
    Of course you'll also need to loop enter_symbol until a legal move is entered.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    i tried to loop enter_symbol, but then my brain started hurting

    i don't think i did it right, but this is what i tried

    i keep getting "where would u like to enter symbol" over and over

    Code:
    #include<stdio.h>
    
    void enter_symbol(char tic_tac_toe[][3], int player);
    void print_board(char tic_tac_toe[][3]);
    int has_won(char tic_tac_toe[][3]);
    
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
     int player = 1;
     int i;
    
     for (i = 0; i < 9; i++) {
         enter_symbol(tic_tac_toe, player);
         print_board(tic_tac_toe);
         if (has_won(tic_tac_toe))
             break;
         player = !player;
        }
    
      return(0);
    }
    
    void enter_symbol(char tic_tac_toe[][3], int player)
    {
     int j;
     int row, col;
     while(1){
      for (j = 0; j < 9; j++){
       printf("Where would u like to enter symbol\n", player);
       scanf("%d", &row);
       scanf("%d", &col);
    
       if (tic_tac_toe[row][col]==' ')
       {
         if (player == 1)
          tic_tac_toe[row][col]=='x';
         else
          tic_tac_toe[row][col]='o';
         break;
       }
         else
          printf("Position already taken\n");
      }
     }
    }
    
    void print_board(char tic_tac_toe[][3])
      {
       printf("\n %c|%c|%c\n -----\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
       printf(" %c|%c|%c\n -----\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
       printf(" %c|%c|%c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);
      }
    
    
    int has_won(char tic_tac_toe[][3]){
    
      if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
          tic_tac_toe[0][1] == tic_tac_toe[0][2] &&
          tic_tac_toe[0][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[1][0]);
        return (1);
      }
      else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[2][0]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
               tic_tac_toe[1][0] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][1]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else
        printf("Tie game\n");
    
     return(0);
    }

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    i keep getting "where would u like to enter symbol" over and over
    Of course you do, you have two loops in there: the "while" loop and the "for" loop inside it. And you still haven't fixed the problem spydoor pointed out: "tic_tac_toe[row][col]=='x';" is wrong (probably a typo you made), you should use an assignment instead.

    I don't understand why you copied the for loop from the main function into the enter_symbol function. I'm sure you know that "for (i = 0; i < 9; i++)" means that the loop will be executed exactly 9 times (unless you break out of it sooner). After 9 moves the board is full, so that's why I chose to use that loop in main(). But you shouldn't use it anywhere else.

    Here's a better version of enter_symbol:
    Code:
    void enter_symbol(char tic_tac_toe[][3], int player)
    {
        int j;
        int row, col;
    
        while(1) {
            printf("Where would you like to enter the symbol?\n", player);
            scanf("%d", &row);
            scanf("%d", &col);
    
            if (row < 0 || row > 2 || col < 0 || col > 2) {
                printf("Col or Row out of bounds\n");
                continue;
            }
            if (tic_tac_toe[row][col] == ' ') {
                if (player == 1)
                    tic_tac_toe[row][col] = 'x';
                else
                    tic_tac_toe[row][col] = 'o';
                break;
            }
            else
                printf("Position already taken\n");
        }
    }
    The part where I check that row and col are within the allowed limits is extremely important, unless you want your program to crash every time the user does something wrong.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    i understand for loops, but i didnt know how to loop enter_symbol

    i just copied it there to show u where i was trying to start it

    should i get rid of the while loop and make a for loop for enter_symbol? because the program now says "Tie game" after the first move. (which might just have to do with has_won)

    heres what i have now
    Code:
    #include<stdio.h>
    
    void enter_symbol(char tic_tac_toe[][3], int player);
    void print_board(char tic_tac_toe[][3]);
    int has_won(char tic_tac_toe[][3]);
    
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
     int player = 1;
     int i;
    
     for (i = 0; i < 9; i++) {
         enter_symbol(tic_tac_toe, player);
         print_board(tic_tac_toe);
         if (has_won(tic_tac_toe))
             break;
         player = !player;
        }
    
      return(0);
    }
    
    void enter_symbol(char tic_tac_toe[][3], int player)
    {
     int row, col;
     while(1){
       printf("Where would u like to enter symbol\n", player);
       scanf("%d", &row);
       scanf("%d", &col);
    
       if (row < 0 || row > 2 || col < 0 || col > 2) {
                printf("Col or Row out of bounds\n");
                continue;
            }
       if (tic_tac_toe[row][col]==' ')
       {
         if (player == 1)
          tic_tac_toe[row][col]='x';
         else
          tic_tac_toe[row][col]='o';
         break;
       }
         else
          printf("Position already taken\n");
      }
    }
    
    void print_board(char tic_tac_toe[][3])
      {
       printf("\n %c|%c|%c\n -----\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
       printf(" %c|%c|%c\n -----\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
       printf(" %c|%c|%c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);
      }
    
    
    int has_won(char tic_tac_toe[][3]){
    
      if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
          tic_tac_toe[0][1] == tic_tac_toe[0][2] &&
          tic_tac_toe[0][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[1][0]);
        return (1);
      }
      else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[2][0]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
               tic_tac_toe[1][0] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][1] &&
               tic_tac_toe[2][1] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][1]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
               tic_tac_toe[1][2] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][2] &&
               tic_tac_toe[2][2] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][0]);
        return (1);
      }
      else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
               tic_tac_toe[1][1] == tic_tac_toe[2][0] &&
               tic_tac_toe[2][0] != ' '){
        printf("%c has won!\n", tic_tac_toe[0][2]);
        return (1);
      }
      else
        printf("Tie game\n");
    
     return(0);
    }

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    never mind, i got the program to work, i just doesnt say "tie game" anymore...i can live with that

    thanks for helping me out, i needed it big time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TicTacToe AI
    By Loic in forum C++ Programming
    Replies: 9
    Last Post: 05-28-2007, 01:37 PM
  2. C++ Tictactoe
    By vinter in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2007, 09:56 PM
  3. tictactoe symbol entering problem
    By ademkiv in forum C Programming
    Replies: 1
    Last Post: 03-21-2006, 11:53 AM
  4. Tictactoe problem
    By endo in forum Windows Programming
    Replies: 3
    Last Post: 04-10-2005, 09:36 AM
  5. TicTacToe OOP
    By NickESP in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2003, 11:47 AM