Thread: Would you help me understand this code?

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

    Would you help me understand this code?

    Please explain me each sentence in this code, Thanks.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void swap(int *a, int *b)
    {
        int c = *a;
        *a = *b;
        *b = c;
    }
    
    void draw(int board[3][3])
    {
        int i, j;
        printf("\n");
        for(i = 0; i < 3; i++)
            for(j = 0; j < 3; j++)
            {
                if(board[i][j] != 0)
                    printf(" |%d|", board[i][j]);
                else
                    printf(" | |");
                if(j == 2)
                    printf("\n ----------- \n");
            }            
    }
    
    int main()
    {
        int board[3][3], *p = (int*)board;
        int i, j, c, x, ei, ej;
        
        //initialization
        for(i = 0, c = 8; i < 3; i++)
            for(j = 0; j < 3; j++, c--)
                board[i][j] = c;
        board[ei = 2][ej = 2] = 0;
         
         // check move //
        c = 0;
        while(c < 8)
        {
            draw(board);
            printf(" Move ");
            x = getchar() - 49 + 1;
            if((x < 10) && (x > 0))
                printf("%d\n",x);
            else 
            {
                printf("bad move\n");
                continue;
            }
            
            if(ei > 0 && board[ei - 1][ej] == x)
            {
                swap(&board[ei][ej], &board[ei - 1][ej]);
                ei--;
            }
            else if(ei < 2 && board[ei + 1][ej] == x)
            {
                swap(&board[ei][ej], &board[ei + 1][ej]);
                ei++;
            }
            else if(ej > 0 && board[ei][ej - 1] == x)
            {
                swap(&board[ei][ej], &board[ei][ej - 1]);
                ej--;
            }
            else if(ej < 2 && board[ei][ej + 1] == x)
            {
                swap(&board[ei][ej], &board[ei][ej + 1]);
                ej++;
            }
            else 
            {
                printf("bad move\n");
                continue;
            }
            
            // check win //
            for(c = 0; c < 8; c++)
                if(p[c] != c + 1)
                    break;             
        }
        
        draw(board);
        printf("\n You Win!");
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Anyone?

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Dude do you seriously expect anyone to explain a hundred lines of code to you one by one? Maybe start by asking what part don't understand, then someone should explain.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Alright then,
    just explain me this part
    Code:
    int main()
    {
        int board[3][3], *p = (int*)board;
        int i, j, c, x, ei, ej;
        
        //initialization
        for(i = 0, c = 8; i < 3; i++)
            for(j = 0; j < 3; j++, c--)
                board[i][j] = c;
        board[ei = 2][ej = 2] = 0;
         
         // check move //
        c = 0;
        while(c < 8)
        {
            draw(board);
            printf(" Move ");
            x = getchar() - 49 + 1;
            if((x < 10) && (x > 0))
                printf("&#37;d\n",x);
            else 
            {
                printf("bad move\n");
                continue;
            }
            
            if(ei > 0 && board[ei - 1][ej] == x)
            {
                swap(&board[ei][ej], &board[ei - 1][ej]);
                ei--;
            }
            else if(ei < 2 && board[ei + 1][ej] == x)
            {
                swap(&board[ei][ej], &board[ei + 1][ej]);
                ei++;
            }
            else if(ej > 0 && board[ei][ej - 1] == x)
            {
                swap(&board[ei][ej], &board[ei][ej - 1]);
                ej--;
            }
            else if(ej < 2 && board[ei][ej + 1] == x)
            {
                swap(&board[ei][ej], &board[ei][ej + 1]);
                ej++;
            }
            else 
            {
                printf("bad move\n");
                continue;
            }
            
            // check win //
            for(c = 0; c < 8; c++)
                if(p[c] != c + 1)
                    break;             
        }
        
        draw(board);
        printf("\n You Win!");
        getchar();
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Homework? If so, pay attention in class. I just started learning and know some of what is going on. Just Study up seeing as I don't think anyone is going to tell you line by line.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Nope, these aren't my homework.
    I am on 9th grade, I study this by my own, through the internet (I bought a book on PHP tho :P ...finished reading it a year ago)
    Usually, in our school we start learning programming languages on the 10th grade.
    Started working with C some months ago.

    So if you can, explain me this.
    This code works so cool so I'd to know how it works.
    Last edited by eXeCuTeR; 10-26-2007 at 07:22 PM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    well, i dont think anyone is going to try to explain this to you just out of principle.
    this kind of falls into that no homework questions/do this for me area.
    not to be rude or anything, im just trying to give advice.
    honestly, if you want to find out what it means, i would start with the basics.
    theres a tutorial here if you want to learn it though.

    oh, and just for future reference, dont do this:
    Code:
    //initialization
        for(i = 0, c = 8; i < 3; i++)
            for(j = 0; j < 3; j++, c--)
                board[i][j] = c;
                    board[ei = 2][ej = 2] = 0;
    from what i understand, this is a bad way to initialize variables.
    and just looking at it, the code itself wouldnt work.
    'why?' you might ask?
    read the tutorial

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    It works perfectly mate, I just checked it.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    checked it how?
    what compiler are you using?
    and if you just checked it, why do you want people to explain what it does?
    Last edited by MikeyIckey; 10-26-2007 at 07:39 PM. Reason: forgot something

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I use Geany.

    I don't want people to explain me what it does, I want people to explain me the lines of this program, I can't understand it.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    when you detect inflicted confusion say stop.

    if else.
    arrays.
    &&.
    multidimensional arrays.
    for loops.
    arithmetic.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    No..
    My question is why did the author typed: board[ei - 1][ej] and not board[ei - 5265][ej].
    How can I explain.

    I don't understand why he typed this: if(ei > 0 && board[ei - 1][ej] == x) for example, and the other things.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    ei > 0 is a range check. I think the other part is something to do with the game rules. what is the program for?

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Run it, it's hard to explain.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by eXeCuTeR View Post
    Run it, it's hard to explain.
    oh ok now I get it...

    ei, ej is where the current position is. and -1 is the offset that you are trying to move. the if else tree is for one of four possible moves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM