1. ## chessboard simulation

I'm trying to write a chessboard simulation basically the black and white colors are represented by either #'s or spaces

so something like:

Code:
``` # # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #```
my program currently prints:

Code:
``` # # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #```
the column and row's are both 8 in length.

so basically for each column i do the following:
if (col % 2 == 1)

which either returns a 0 or 1. if it's 0 i put board[row][col] = ' '; otherwise board[row][col] = '#';

Now my problem: I *want* every second row to start off with board[row][col] = '#'; so it looks like a chessboard... Any ideas?

so something like (where 0 = space and 1 = #)
0101010
1010101
0101010
1010101
0101010
1010101
0101010

2. >> hich either returns a 0 or 1. if it's 0 i put board[row][col] = ' '; otherwise board[row][col] = '#';

Make ' ' and '#' flags or something, and do the same sort of modularity by 2 on row and swap the flags.

3. Well, you should take the row numbers into accout also.

Starting from row number 1 and column number 1 (left-most square at the top), you see that
Code:
```if (row_number*column_number = odd)
squre_color= white
else /* if the product is even */
squre_color=black```
Hope helps...

4. Originally Posted by fnoyan
Well, you should take the row numbers into accout also.

Starting from row number 1 and column number 1 (left-most square at the top), you see that
Code:
```if (row_number*column_number = odd)
squre_color= white
else /* if the product is even */
squre_color=black```
Hope helps...
i've managed to swap the rows so that it 0, 1, 0, 1 (since i have the same thing going on in an inner if statement where i say col%2 == and flag it as follows:

1
0
1
0
1
0
1
0

Code:
``` 	  if (row % 2==1)
{
alt = 0;
}
else {
alt = 1;
}```

i just don't know how to apply to so rather than starting every second row with a space i want a #?

5. I did not compile/run the code, but this may work
Code:
``` int row=1, column=1;
while(row<9)
{
if ((row*column)%2)
printf("#");
else
printf("  ");

column++;
if (!(column%8))
{
row++;
printf("\n");
}
}```
As I said, i did not try the code and it is possible to get a wierd output!

6. Well, I didn't compile this, I only want to describe another way, using for and initializing color depending on the number of row.
This will contain for sure some errors, but the idea I think is ok.

Code:
```color = ' ';
for (x=a;x<8;x++)
{
if ((x%2) == 1) { color =' '} else {color ='#' };
for (y=0;y<8;y++)
{
board[x][y] = color;
if (color=='#') { color = ' '} else { color = '#'; }
}
}```

-----------------------
http://www.uberum.com

7. I ended up solving it it seemed confusing at first but i just wrote down the logic in english and it came to me. Basically i got rid of the mod idea and saved the last character in a char (i.e. # or ' ') and kept switching between it and saving to a variable by comparing it to the last character printed. Next in my outer row loop i check this variable and switch between it so it reflects the columns.

Anyway, thanks guys it's always good to know to know how to do it in other ways too..

8. Since the board colors aren't going to change, and there are only 64 squares, you could also just initialize the board at declaration:
Code:
```char board[8][8] =
{' ','#',' ','#',' ','#',' ','#',
'#',' ','#',' ','#',' ','#',' ',
' ','#',' ','#',' ','#',' ','#',
'#',' ','#',' ','#',' ','#',' ',
' ','#',' ','#',' ','#',' ','#',
'#',' ','#',' ','#',' ','#',' ',
' ','#',' ','#',' ','#',' ','#',
'#',' ','#',' ','#',' ','#',' '};```

9. >you could also just initialize the board at declaration
What fun is that?
Code:
```#include <stdio.h>

int main ( void )
{
unsigned int x = 0x55;
unsigned int i, j;

for ( i = 0; i < 8; i++ ) {
for ( j = 0; j < 8; j++ )
putchar ( x >> j & 1 ? ' ' : '#' );
putchar ( '\n' );
x = ~x;
}

return 0;
}```

10. >What fun is that?
Not much, but hey it runs a tad faster.

Of course what happens when you start adding pieces to the board? Then you could add a second board layer for the pieces. At any rate Eventually a gui would be desirable anyway.

11. I dont understand Salem's code.. in the nester for loop and the statement x = ~x;

12. Perhaps i'm completely blind, but I don't think Salem has posted in this thread yet.

13. >Perhaps i'm completely blind, but I don't think Salem has posted in this thread yet.
I think your eyes are fine. I think they meant Prelude. It's easy to get the two confused. They both make the rest of us look like C newbies.

14. Oooops!!! my total mistake... syntax error eh? yeah, i meant Prelude... nah, i already know im a C newbie

but anyways, can any1 explain to me the nested for loop part in Prelude's code?
[/code]
putchar ( x >> j & 1 ? ' ' : '#' );
[code]

and
Code:
`x = ~x;`

15. Good code prelude

I didn't understood following line, can anyone explain(couldn't find it in TC help either)

x = ~x;

where x is unsigned int.