Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Making a chess board in the console

  1. Jul 30, 2006 #1
    Hello everyone. I have now finished my tetris program, and was planning on starting on a chess game in the console (it will be 2 player, playing at the same terminal - no winsock at the moment). Anyway, I have already set up the basics, however, I need a little help in programming the board. I was thinking about using a few for loops, although, I'm a bit unsure how to implement them. Each square of the chess board will be 9 console units across, and 5 console units down (although, these are just estimates - I might change them, so the board looks better). Any help, regardless of what it may be would be greatly appreciated.

    Edit: Also, because of the sides will be white and red, otherwise, the black pieces would not stand out against the black background.
    Last edited by a moderator: Jul 30, 2006
  2. jcsd
  3. Jul 30, 2006 #2
    I'm not sure i understand what your problem is....

    are you having difficulties in drawing the squares?
    you only need two for loops to draw everything, one that goes over the width, and one that goes over the height.

    for drawing the vertical frame you'll need x%9 to be zero and for the horrizontal y%5... (to draw them every 9 steps or every 5 steps respectively).

    you need to draw the chess peices in this loop too, make an array which represent the board, and check if the spot you draw contains a piece, make an array that contains each shape (3x7 i guess), and copy it over as you draw with these two for statements...
    Last edited: Jul 30, 2006
  4. Jul 30, 2006 #3
    I can't quite get the loops right, so that it gives me a nice board. btw, I'm only drawing the board at the moment. There is another function that will draw the pieces on over the top.

    The board I want is like this:
    wwwwwwwwwbbbbbbbbbwwwwwwwww etc.
    w .
    w .
    w .
    w .

    where b and w are black and white respectively.
    Last edited by a moderator: Jul 30, 2006
  5. Jul 30, 2006 #4
    I already explained how to draw such a board...

    use two for loops, one inside the other, one will be the x value and the second would be the y value, so youre scanning all the possible values on your board, just use if statements inside the loops to determine if you should draw whith or black... if (x/9)%2, if (y/5)%2...
  6. Jul 30, 2006 #5
    Oh rite, now I understand what you mean. Thank you for the help, I don't think I fully understood the first time round.:approve: Thank you.
  7. Jul 30, 2006 #6
    I'm still having a few problems with it, even with those suggestions. I understand the method, it's a question of implementing it.

    This is the loop I have at the moment:
    Code (Text):

    int i,j;
                          Position.X = i;
                          Position.Y = j;
                          SetConsoleCursorPosition(hOut, Position);
                          if( (((i/9)%2) < 1)&&(((j/5)%2) < 1) ) {
                               cout << (unsigned char)219;
    hOut, refers to the handle to the window.
    This if statement gives half of the squares, the problem is, what if would give the other half?
    Edit: Sorry, just figured it out.
    Last edited by a moderator: Jul 30, 2006
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook