I am looking for some help with regards to accessing values of an array through user input...
The following code is a chessboard representation using cout << to create a chessboard (graphic/grid) and elements within an 8x8 array to represent the chess pieces.
I would like to be able to input moves (ie: e2e4) and then have the chessboard display that the pawn has moved from the e2 square to the e4 square.
***I am not yet at the point of implementing legal move generation or trying to make this work through object oriented programming or by using mailbox arrays or bitboards... although any helpful advice would be greatly welcome!***
Question #2: have i initialized the array correctly or do i need to define each empty space by using ' ' ?
Question #3: is there a clearer way for me to write the code that represents the array of chess pieces within the cout << graphical grid ?
* i first tried using nested for loops to accomplish this task but couldn't get the grid to align properly ;(
Lastly, i am very new to programming so the more specific you can be with regards to help/code would be greatly appreciated.
Thank you.
Code:
#include <string>
using namespace std;
int main()
{
const int RANKS = 8;
const int FILES = 8;
cout << "\n";
char cb[RANKS][FILES] = {
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r' },
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p' },
{ },
{ },
{ },
{ },
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' },
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R' } };
cout << "**************************************\n"
<< "* *\n"
<< "* WELCOME TO CHESS !!! *\n"
<< "* *\n"
<< "**************************************\n\n";
// Chessboard Representation
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 8 | " << cb[0][0] << " | " << cb[0][1] << " | " << cb[0][2] << " | " << cb[0][3] << " | " << cb[0][4] << " | " << cb[0][5] << " | " << cb[0][6] << " | " << cb[0][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 7 | " << cb[1][0] << " | " << cb[1][1] << " | " << cb[1][2] << " | " << cb[1][3] << " | " << cb[1][4] << " | " << cb[1][5] << " | " << cb[1][6] << " | " << cb[1][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 6 | " << cb[2][0] << " | " << cb[2][1] << " | " << cb[2][2] << " | " << cb[2][3] << " | " << cb[2][4] << " | " << cb[2][5] << " | " << cb[2][6] << " | " << cb[2][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 5 | " << cb[3][0] << " | " << cb[3][1] << " | " << cb[3][2] << " | " << cb[3][3] << " | " << cb[3][4] << " | " << cb[3][5] << " | " << cb[3][6] << " | " << cb[3][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 4 | " << cb[4][0] << " | " << cb[4][1] << " | " << cb[4][2] << " | " << cb[4][3] << " | " << cb[4][4] << " | " << cb[4][5] << " | " << cb[4][6] << " | " << cb[4][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 3 | " << cb[5][0] << " | " << cb[5][1] << " | " << cb[5][2] << " | " << cb[5][3] << " | " << cb[5][4] << " | " << cb[5][5] << " | " << cb[5][6] << " | " << cb[5][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 2 | " << cb[6][0] << " | " << cb[6][1] << " | " << cb[6][2] << " | " << cb[6][3] << " | " << cb[6][4] << " | " << cb[6][5] << " | " << cb[6][6] << " | " << cb[6][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " 1 | " << cb[7][0] << " | " << cb[7][1] << " | " << cb[7][2] << " | " << cb[7][3] << " | " << cb[7][4] << " | " << cb[7][5] << " | " << cb[7][6] << " | " << cb[7][7] << " | \n";
cout << " +---+---+---+---+---+---+---+---+\n";
cout << " a b c d e f g h\n\n";
int move;
cout << "Please enter a move: ";
cin >> move;
return 0;
}