chess board

I need help how to create a chess board in c++ for beginners
Last edited on
> I need help how to create a chess board in c++ for beginners
How beginner?
1
2
3
4
5
6
7
8
cout << "BWBWBWBW" << endl;
cout << "WBWBWBWB" << endl;
cout << "BWBWBWBW" << endl;
cout << "WBWBWBWB" << endl;
cout << "BWBWBWBW" << endl;
cout << "WBWBWBWB" << endl;
cout << "BWBWBWBW" << endl;
cout << "WBWBWBWB" << endl;
Last edited on
I came up with this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const std::vector<int> nstdstartboardlc
 {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, tnoir, cnoir, fnoir, dnoir, rnoir, fnoir, cnoir, tnoir, -1, -1,
-1, -1, pnoir, pnoir, pnoir, pnoir, pnoir, pnoir, pnoir, pnoir, -1, -1,
-1, -1, vide, vide, vide, vide, vide, vide, vide, vide, -1, -1,
-1, -1, vide, vide, vide, vide, vide, vide, vide, vide, -1, -1,
-1, -1, vide, vide, vide, vide, vide, vide, vide, vide, -1, -1,
-1, -1, vide, vide, vide, vide, vide, vide, vide, vide, -1, -1,
-1, -1, pblanc, pblanc, pblanc, pblanc, pblanc, pblanc, pblanc, pblanc, -1, -1,
-1, -1, tblanc, cblanc, fblanc, dblanc,  rblanc, fblanc, cblanc, tblanc,-1, -1,
-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };

/*formatting sucks, I know */ :)
initialises my actualbord class member

There are unicode characters for chess pieces so if you can get that to work on your console then you can use the console otherwise you cannot use the console to display pieces. Using words would be a DISASTER but I guess if you're fine with that then there's nobody stopping you (good idea to do this to try the logic before you move onto GUI). Your other option is to use some GUI library.

By the way mycuser, I'm just curious, but what do the acronyms (I presume they're acronyms) in snippet stand for?

(VV pointing to mycuser, not op VV)

I think two dimensional would be better than single dimensional.. single dimensional would unnecessarily complicate stuff like moving a piece (and by a lot, suppose if you had to check where a knight could move) basically you would have to, again, treat the single dimensional as a two dimensional so there's no point.

And what are the -1s for? If they represent the border then you should not include them in the array that would unnecessarily be wasting space for something that will never be changed. You can worry about the border while printing.

And I think you should really use better identifiers because I spent a lot of time trying to crack what they meant and I got nothing, not what you want to happen if you want your code to be read by other people.
There are unicode characters for chess pieces

Actually for test purposes, I send abbreviated piece names to display. ( pPnNbBrRqQkK ).
The values for pieces come from :

1
2
3
4
 
enum pieceindex
   { mty, bpawn, wpawn, bknight, wknight, bbishop, wbishop, brook, wrook,
     bqueen, wqueen, bking, wking };


and an internal 2D "chessboard" is populated with this container :

1
2
3
4
5
6
7
8
9
 const std::vector<std::vector<int>> startboard2d
 { { brook, bknight, bbishop, bqueen, bking, bbishop, bknight, brook },
   { bpawn,   bpawn,   bpawn,  bpawn, bpawn,   bpawn,   bpawn, bpawn },
   {   mty,     mty,     mty,    mty,   mty,     mty,     mty,   mty },
   {   mty,     mty,     mty,    mty,   mty,     mty,     mty,   mty },
   {   mty,     mty,     mty,    mty,   mty,     mty,     mty,   mty },
   {   mty,     mty,     mty,    mty,   mty,     mty,     mty,   mty },
   { wpawn,   wpawn,   wpawn,  wpawn, wpawn,   wpawn,   wpawn, wpawn },
   { wrook, wknight, wbishop, wqueen, wking, wbishop, wknight, wrook } };


and m_2Dactualboard is initialized like so :

std::vector<int> m_actualboard { stdstartboardlc };

The -1 cells in the previous board were for checking if a knight move ends up
of the board
.
The -1 board is a recent experiment as actually I have mapped out knight moves in a rather BIG container.

As for going GUI, it's on my TODO list, but I'm getting there.
Topic archived. No new replies allowed.