Chess Game Board Question

Hi everyone, beginner in C++ but I'm surviving. For my final project, I'm programming a chess game, but I wanted to know if it was possible to have the board appear at the top of the window and have the program run right under it. So that all the modifications would show up at the top of the window.

If that makes sense? I just feel like printing the board over and over seems so ....annoying.

If this is possible, can anyone point me to the right direction to learn about this or how to do this?

Thanks.
For a beginner I think a game of chess is a little bit too complex, there are lots of heuristics to have in mind for the AI. But for moves alone it's not that difficult. If you are making it into console, you just have to clear the screen, update board, make moves, repeat.

There is the "bad" way to do it, don't do this
system("clear");

Then there's _clrscrn from conio.h which is deprecated
_clrscrn()

Then if you use windows, you can use win32api to clear the screen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <windows.h>

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;

// Get the number of character cells in the current buffer. 

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer 
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write 
                                    coordScreen,     // Coordinates of first cell 
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer 
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute 
                                    coordScreen,      // Coordinates of first cell 
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}

Wow, I didn't even think of it as a set of instructions like "clear the screen/update the moves/etc", I feel kinda dumb I just assumed it was something so hard and complex, but so relieved. Thank you for the help, Golden Lizard !!

I'm not doing an AI, I'm just going to do a 2 player thing. Hopefully not as complex as an AI would have been (LOL....it doesn't seem that bad conceptually).

I'm coding this in the Win32 console, that is what you meant, right ?
Yes, but if you are a beginner you may just use System("clear") or clrscrn() from conio.h, it's not like you make a program that should be secure or very resource efficient, no one's gonna berate you for using system or a deprecated library in this case. Just don't forget to don't use system for real applications.
Last edited on
Topic archived. No new replies allowed.