Thread: chessboard simulation

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    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. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> 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. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    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. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote 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 #?
    Last edited by Axel; 09-01-2006 at 01:06 AM.

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    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. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    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. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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] = 
    {' ','#',' ','#',' ','#',' ','#',
     '#',' ','#',' ','#',' ','#',' ',
     ' ','#',' ','#',' ','#',' ','#',
     '#',' ','#',' ','#',' ','#',' ',
     ' ','#',' ','#',' ','#',' ','#',
     '#',' ','#',' ','#',' ','#',' ',
     ' ','#',' ','#',' ','#',' ','#',
     '#',' ','#',' ','#',' ','#',' '};
    Last edited by swoopy; 09-01-2006 at 01:09 PM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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;
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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. #11
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    I dont understand Salem's code.. in the nester for loop and the statement x = ~x;
    It is not who I am inside but what I do that defines me.

  12. #12
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Perhaps i'm completely blind, but I don't think Salem has posted in this thread yet.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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. #14
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    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;
    It is not who I am inside but what I do that defines me.

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork, execv, spawn, or something else?
    By DavidP in forum C++ Programming
    Replies: 8
    Last Post: 01-26-2009, 04:25 PM
  2. Role of c++ in simulation software
    By CChakra in forum C++ Programming
    Replies: 9
    Last Post: 11-18-2008, 02:36 AM
  3. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  4. Help with time simulation assignment
    By rakan in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2006, 11:39 AM
  5. Pipeline freeze simulation
    By darklightred in forum C Programming
    Replies: 2
    Last Post: 07-24-2006, 11:57 AM