Making a chess board in the console

In summary, the conversation discusses a programming project involving a console chess game. The individual is seeking help with creating the game board using for loops and implementing black and white squares. Suggestions are given on how to draw the board and how to incorporate pieces. The conversation ends with the individual understanding the method and making progress on the project.
  • #1
finchie_88
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:
Technology news on Phys.org
  • #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:
  • #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 .
b
b
b
b
b
etc.

where b and w are black and white respectively.
 
Last edited by a moderator:
  • #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...
 
  • #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.
 
  • #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:
int i,j;
for(j=0;j<40;j++)
    {
                      for(i=0;i<72;i++)
                      {
                      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:

1. How do I create a chess board in the console?

To create a chess board in the console, you can use a combination of loops and print statements. Firstly, you will need to create a 2D array to represent the board. Then, using nested for loops, iterate through the array and print out the appropriate symbols for each square.

2. Can I customize the appearance of the chess board in the console?

Yes, you can customize the appearance of the chess board by changing the symbols used for the different pieces, as well as the background and text colors. You can also add borders or spacing to make the board easier to read and navigate.

3. How can I make sure that the pieces are in the correct starting positions on the chess board?

You can ensure that the pieces are in the correct positions by setting up the initial state of the 2D array to match the starting positions in a standard chess game. Alternatively, you can create a function that allows the user to input their own starting positions.

4. Is it possible to implement chess game logic in the console?

Yes, it is possible to implement basic chess game logic in the console. This includes moving pieces, checking for checkmate and stalemate, and enforcing the rules of the game. However, more advanced features such as AI opponents or multiplayer capabilities would require additional programming.

5. Can I save and load games played on the console chess board?

Yes, you can save and load games played on the console chess board by using file input/output operations. This allows you to save the state of the 2D array and reload it at a later time to continue the game. You can also save multiple games by using different file names.

Similar threads

  • General Discussion
2
Replies
42
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Math Proof Training and Practice
Replies
25
Views
2K
Replies
13
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • General Math
Replies
25
Views
16K
  • Special and General Relativity
Replies
8
Views
902
  • General Discussion
Replies
10
Views
4K
  • Science Fiction and Fantasy Media
Replies
3
Views
2K
Replies
3
Views
3K
Back
Top