Thread: Need Help Understanding This Code.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    Lightbulb Need Help Understanding This Code.

    This is the first time I posted here and I hope that you guys can help me.

    The code below is program which is a Tic Tac Toe Game. I finished it with some help from my sister and there is this part in the code where I dont really get it.

    The part where I dont get is the check(void). There is this two for looops which calls board[i][0], which is a character but inside the for loops it adds with an integer. Thats the part I dont get. How am I gonna know the value of *p+1? Is it possible to add an integer with a char? What is the answr then?

    The second thing is the IF Statement. I undertand the condition but not what it will do if it satisfies it. What will happen if you return *p or return board[0][0]? Does that makes sense?

    Sorry if my question seems to be weird, I'm just a newbie.

    Anyways here is the code:

    Code:
    /*This program creates a Tic Tac Toe game without any AI.*/
    #include<stdio.h>
    #include<stdlib.h>
    
    #define SPACE ' '
    
    char board[3][3]={SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE};
    
    void get_player_move(void), 
    get_nextplayer_move(void);
    void disp_board(void);
    int check(void);
    
    main()
    {
    char done;
    
    printf("This is the game of Tic-Tac-Toe.\n\n");
    done=SPACE;
    
    do
    {
    disp_board();
    get_player_move();
    done=check();
    if(done!=SPACE) break;
    printf("\n");
    disp_board();
    get_nextplayer_move();
    done=check();
    }
    while(done==SPACE);
    if(done=='X')
    {printf("X is the winner!\n\n");}
    else 
    {printf("O is the winner!!!!\n\n");}
    printf("\n");
    disp_board();
    printf("\n\n");
    system("pause");
    }
    
    /*The below code is responsible for the input and output of X.*/
    void get_player_move(void)
    {
    int x,y;
    
    printf("\nEnter Column for your X: ");
    scanf("%d", &x);
    printf("Enter Row    for your X: ");
    scanf("%d", &y);
    x--;y--;
    
    if(board[x][y]!=SPACE)
    {
    printf("\nInvalid move,try again.\n");
    get_player_move();
    }
    
    else 
    {board[x][y]='X';}
    printf("\n");
    }
    
    /*The below code is responsible for the input and output of Y.*/
    void get_nextplayer_move(void)
    {
    int x,y;
    
    printf("\nEnter Column for your Y: ");
    scanf("%d", &x);
    printf("Enter Row    for your Y: ");
    scanf("%d", &y);
    x--;y--;
    
    if(board[x][y]!=SPACE)
    {
    printf("\nInvalid move,try again.\n");
    get_nextplayer_move();
    }
    
    else 
    {board[x][y]='O';}
    printf("\n");
    }
    
    /*This code is responsible for displaying the Tic Tac Toe Board.*/
    void disp_board(void)
    {
    int i;
    
    for(i=0;i<3;i++){
    printf(" %c | %c | %c ", board[i][0],
    board[i][1], board[i][2]);
    if(i!=2) 
    {printf("\n---|---|---\n");}
    }
    printf("\n");
    }
    
    /*This code is responsible for checking if the spaces are occupied or not.*/
    check(void)
    {
    int i;
    char *p;
    
    for(i=0;i<3;i++)
    {
    p=&board[i][0];
    if (*p==*(p+1) && *(p+1)==*(p+2)) 
    {return *p;}
    }
    
    for(i=0;i<3;i++)
    {
    p=&board[0][i];
    if(*p==*(p+3) && *(p+3)==*(p+6)) 
    {return *p;}
    }
    
    if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
    {return board[0][0];}
    if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
    {return board[0][2];}
    
    return SPACE;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Why don't you ask your sister?
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first step to getting any understanding is to start with indented code.
    SourceForge.net: Indentation - cpwiki
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Why don't you ask your sister?
    LOL.... nothing like overstating the obvious.

    Gotta love it!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    *(p+1) is not adding an integer with a char. p is a pointer to the board array. Adding 1 to that pointer makes it point beyond the start of that array. Or +2 means I want to look at the 2nd char in the array. The `*`gets the actual char that`s at the calculated position.

    Return *p returns a char. But you should ask what happens if the condition is NOT satisfied. There is no return. Also, the function should have a return type int in its header. I see there is a proper prototype for it at the top, but its actual declaration should agree with that.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by CommonTater View Post
    LOL.... nothing like overstating the obvious.

    Gotta love it!
    i would not be posting here if she can teach me but she wants me to learn it on my own.

    you guys are rude.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The return value of check is used to end the game. It is part of the board in this implementation, but it wouldn't need to be, because when the game is over, the return value of check would be 'x' or 'o'.

    As for your other question, your sister uses something called pointer arithmetic. As long a you understand that a pointer's value is an address, and that you can do math on an address, then any lesson on this makes sense.
    treat pointers like a ruler when you do arithmetic with pointer variables. Addition and subtraction is like moving up and down a number line, and you can imagine the stored address moving up and down n-bytes. When you store this calculated amount (assignment) think of it like marking a spot on a ruler.

    For larger data than a char, you will have to multiply by the size of the object to get the difference in addresses, but that's really low level thinking. You're almost never concerned with the size of things as much as how many elements you skipped over in the array.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by thisbeme View Post
    i would not be posting here if she can teach me but she wants me to learn it on my own.

    you guys are rude.
    Not rude... Just a bit of blunt humour... programmers are famous for their directness with one another.

    Your sister is right, you should learn as much on your own as you can. Watching videos and having answers handed to you is passive learning and it tends to go in one ear and right out the other, without even slowing down. Doing is learning that stays with you.

    I believe you were asking about this bit...
    Code:
    for(i=0;i<3;i++)
       {
          p=&board[i][0];
          if (*p==*(p+1) && *(p+1)==*(p+2)) 
            {return *p;}
    }
    It's using pointers to access the board array.
    This could also be written using standard array notation for the same effect...
    Code:
    for ( i = 0; i < 3; i++ )
      { if ( board[i][0] == board[i][1] && board[i][1] == board[i][2])
           return board[i][0]; }
    Basically it's just testing to see if you have 3 identical values in a row, horizontally.

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    void disp_board(void)
    {
    int i;
    
    for(i=0;i<3;i++){
    printf(" %c | %c | %c ", board[i][0],
    board[i][1], board[i][2]);
    if(i!=2) 
    {printf("\n---|---|---\n");}
    }
    printf("\n");
    }
    Missing function closing brackets..

    Code:
    check(void)
    {
    int i;
    char *p;
    
    for(i=0;i<3;i++)
    {
    p=&board[i][0];
    if (*p==*(p+1) && *(p+1)==*(p+2)) 
    {return *p;}
    }
    
    for(i=0;i<3;i++)
    {
    p=&board[0][i];
    if(*p==*(p+3) && *(p+3)==*(p+6)) 
    {return *p;}
    }
    
    if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
    {return board[0][0];}
    if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
    {return board[0][2];}
    
    return SPACE;
    }
    this function has no return type mentioned..

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, looks like my "indentation" comment went in one person's ear, and out of another person's ear
    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.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by thisbeme View Post
    i would not be posting here if she can teach me but she wants me to learn it on my own.

    you guys are rude.
    Well, i didn't intend my comment to be insulting in any way, but if i offended you, i'm sorry! I just, as CommonTater said, overstated the obvious.
    Devoted my life to programming...

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Yeah, looks like my "indentation" comment went in one person's ear, and out of another person's ear
    Well, that certainly explains the wax balls...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-10-2010, 11:53 PM
  2. Understanding something in Quake 2 Source Code
    By bengreenwood in forum C Programming
    Replies: 5
    Last Post: 08-05-2009, 02:22 PM
  3. Understanding a Line of Quake 2 Source Code
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 08-04-2009, 03:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread