# Thread: Would you help me understand this code?

1. ## 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
{
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
{
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. Anyone?

3. 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. 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
{
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
{
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. 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. 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.

7. 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.

8. It works perfectly mate, I just checked it.

9. checked it how?
what compiler are you using?
and if you just checked it, why do you want people to explain what it does?

10. 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. when you detect inflicted confusion say stop.

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

12. 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. 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. Run it, it's hard to explain.

15. Originally Posted by eXeCuTeR
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.