Thread: this doesnt do what i want it to do

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

    Problem with tic tac toe code

    Code:
    #include<stdio.h>
    
    
    int
    main(void)
    {
     char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
    
     char i, j;
    
      for (i=1; i<=3; i=i+1){
          for (j=1; j<=3; j=j+1)
           {
               printf("Please enter symbol in position %d, %d\n",i,j);
               scanf("%", &tic_tac_toe[i][j]);
               scanf("%*[^\n]");
               scanf("%1[\n]");
           }
          }
    
    void print_board (char tic_tac_toe[3][3]){
      int i,j;
      for (i = 0; i < 3; i++){
        for (j = 0; j < 3; j++){
          printf("%c\t", tic_tac_toe[i][j]);
        }
        printf("\n");
      }
    
    }
    
    
    int has_won (char tic_tac_toe[3][3]){
    
      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[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 return 0;
     }
    
    }
    Last edited by theman29; 03-25-2006 at 12:06 AM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    *what did you want it to do
    *where do you think it's going wrong
    *you need a better thread title
    *why are you posting c code on the C++ board?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Psst, this isn't the C++ board.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    i need it to print out a tictactoe board

    i have no clue where i went wrong, it just doesnt print the board after the player puts in letters at the coordinates

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (i=1; i<=3; i=i+1)
    Arrays start at 0, not 1

    > scanf("%", &tic_tac_toe[i][j]);
    You need to work on your use of scanf - two of the calls are broken.
    This one for example does NOT read a char (or anything else)

    > void print_board (char tic_tac_toe[3][3])
    1. It seems to be declared inside main - C doesn't have nested functions
    2. You don't have a prototype for it.
    3. You don't call it from main.
    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.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    there are still random things that i dont understand about some of the parts in this program so for those parts i copied from basic examples in my textbook and made my own ideas on how to apply them to this program

    tell me if im getting warmer

    Code:
    #include<stdio.h>
    
    char tic_tac_toe[3][3] =
            {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
    
    int
    main(void)
    {
     char i, j;
    
      for (i=1; i<=3; i=i+1){
          for (j=1; j<=3; j=j+1)
           {
               printf("Please enter symbol in position %d, %d\n",i,j);
               scanf("%", &tic_tac_toe[i][j]);
               scanf("%*[^\n]");
               scanf("%1[\n]");
           }
          }
    }
    void print_board (char tic_tac_toe[3][3]){
      char i,j;
      for (i=1; i<3; i++){
        for (j=1; j<3; j++){
          printf(" %c\t", tic_tac_toe[i][j]);
        }
        printf("\n");
      }
    
    }
    
    
    int has_won (char tic_tac_toe[3][3]){
    
      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[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 return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your input function writes past the bounds of the array.
    Arrayindexes in c/c++ range from 0 to num-1 ( 0..2 in your case ).
    Your print_board() function would print only a 2x2 board ( you use indexes from 1 to 2 )
    Kurt
    EDIT: just noticed Salem has already told you all of this
    Last edited by ZuK; 03-25-2006 at 03:36 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > tell me if im getting warmer
    Nope - you're getting further away.
    Sure you fixed the nested function problem, but you added some more problems along the way, like breaking the print_board() function.
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    scanf("%", &tic_tac_toe[i][j]);
    % what?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    theman29, Please

    1) give an explanation what these 4 lines do in general, then
    2) what each line specifically does -- in detail. I'd like to know what you are thinking:
    Code:
    printf("Please enter symbol in position %d, %d\n",i,j);
    scanf("%", &tic_tac_toe[i][j]);
    scanf("%*[^\n]");
    scanf("%1[\n]");
    3) Have youe ever heard of the function getchar()?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    3) Have youe ever heard of the function [i]getchar()?
    Code:
    user@0[~]$ man [i]getchar
    No manual entry for [i]getchar
    user@0[~]$
    Nope.
    Code:
               printf("Please enter symbol in position %d, %d\n",i,j);
               scanf("%", &tic_tac_toe[i][j]);
               scanf("%*[^\n]");
               scanf("%1[\n]");
    I suspect you're trying to do something like this:
    Code:
               printf("Please enter symbol in position %d, %d\n",i,j);
               scanf("%c", &tic_tac_toe[i][j]);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed