help needed with array manipulation through user input

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

priorityversion

help needed with array manipulation through user input

Post by priorityversion »

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.

#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;

}
cyberfish

Re: help needed with array manipulation through user input

Post by cyberfish »

If you are THAT new to programming, a chess engine is probably not what you want to tackle first. It is certainly not a complete beginner's project.
priorityversion

Re: help needed with array manipulation through user input

Post by priorityversion »

Matt,

fortunately for me, i am not trying to create a chess engine at the moment... (i realize this would not be a practical approach to learning a programming language, C++).

i am only trying to learn how to access values of an array through user input.

if you have any suggestions or advice that would be of any particular help regarding this, that would be great.
cyberfish

Re: help needed with array manipulation through user input

Post by cyberfish »

fortunately for me, i am not trying to create a chess engine at the moment...
then, unfortunately, you are on a wrong forum. This forum is for discussing more or less theoretical aspects of chess programming (more specifically the AI part).

Your question is a basic programming question that would be a lot more suited to (and you will probably receive a lot more help from) a general C++ forum.

If you have not discovered any good one so far, I recommend this one:
http://cboard.cprogramming.com/forumdisplay.php?f=3

Friendly and knowledgeable posters and it's also quite active (I post there occasionally, too).
priorityversion

Re: help needed with array manipulation through user input

Post by priorityversion »

Matt,

i'll have a look at the link you provided...

thanks for your time and helpful suggestions.

- Steven
revengeska

Re: help needed with array manipulation through user input

Post by revengeska »

Hey Steven. I'm not a seasoned programmer, but I think I can give some insights. I noticed you didn't do #include <iostream> in your program. Your compiler might include that by default, but maybe it's good to put in for cross compatibility(I'm using gcc on Linux).

For question 2: with the code that you have, you've assigned values to some parts of the array but not others. I wonder with your code where exactly in the array your white pieces would be, because it doesn't look like enough spaces were allocated in the array you did. What I would do, just to avoid any confusion, is do what you were thinking and assign the middle terms to hold ' ' for the empty spaces. This will also have the added effect of aligning your grid just right, as well as make sure your whole array has a value, to which you can change at your leisure.

For question 3: You should be able to use a loop with a mathematical formula to print the whole board. Getting the grid to align right is probably just a matter of tinkering with the cout in the loop. It would clear up a lot of lines of code. Actually I think Tic-Tac-Toe would be a simpler way to try to accomplish the same thing you're trying to do, you might at least try to use that example to get the cout in the loop to align, as it's not as many squares, then you can use the idea for the chess example.

Now onto the big picture, the move input. For this you'll probably want an infinite loop that will encompass the board display, prompt the user to enter a move, take the input from the user and translate that into a move on the board that uses a mathematical formula via the array(we know that the e pawn at the start is in the array [7][4], where 7 is "RANKS" and 4 is "FILES", so in the loop, perhaps if another piece isn't specified from user input, by default move a pawn "RANKS - 2" to move it two squares forward.), and finally go through the loop again. So what you would have is a loop that first contains another loop which displays the board, then once that loop exits you'll move on to the cin code, the user enters a move, then it will take that move and update the array(perhaps in your code you'll have the P adjust [-2][0] in the array with the input). You'll of course have to figure out how to relate user input with moving values to different places in the array. At this point, you'll reiterate through the loop and do the whole thing again infinitely. You can specify an if statement that will stop the loop(and end the program if you desire), when a certain statement happens. This is the approach I would take. Hope it helps.