-
Quickie on Arrays...
We'll say I had an 8x8 array like this:
Code:
char chessBoard[8][8] = {
{ 'r', 'n', 'b', 'q', 'k', 'b', 'k', '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' }
};
is there any way to change the naming of the rows? Is there any way to name the elements as certain names? as in 0,0 a8 etc...
and also how do you change the colour of text..
-
take pointers to indivisual rows
-
You could try something like this (I don't know anything about chess so the indexes might be reversed).
Code:
#include <map>
#include <iostream>
#include <sstream>
using namespace std;
class chessboard {
public:
chessboard ();
char & operator[]( string idx );
protected:
char board[8][8];
char illegal_pos;
};
chessboard::chessboard() {
board[0][0] = 'r'; board[7][0] = 'R';
board[0][1] = 'n'; board[7][1] = 'N';
board[0][2] = 'b'; board[7][2] = 'B';
board[0][3] = 'q'; board[7][3] = 'Q';
board[0][4] = 'k'; board[7][4] = 'K';
board[0][5] = 'b'; board[7][5] = 'B';
board[0][6] = 'n'; board[7][6] = 'N';
board[0][7] = 'r'; board[7][7] = 'R';
for ( int i = 0; i < 8; ++i ){
board[1][i]='p';
board[2][i]=' ';
board[3][i]=' ';
board[4][i]=' ';
board[5][i]=' ';
board[6][i]='P';
}
}
char & chessboard::operator[]( string idx ) {
int p = idx.find(",");
if ( p != string::npos ) {
idx[p]=' ';
stringstream str( idx );
string x, y;
str >> x >> y;
return board[x[0]-'A'][y[0]-'1'];
}
// this is a dirty hack
illegal_pos = '?';
return illegal_pos;
}
int main() {
chessboard board;
board["A,1"]=' ';
cout << board["A,1"];
cout << board["A,2"];
cout << board["A,3"] << endl;
cout << board["H,5"];
cout << board["H,6"];
cout << board["H,7"] << endl;
}
-
You could map string to char. Then use a1-a8, b1-b8, ..., h1-h8 as your keys, and the characters for the pieces as your value.
Code:
std::map<std::string, char> board;
board["a4"] = 'q';
board["h5"] = 'K';
-
>is there any way to change the naming of the rows?
Not if we're still saying you have an 8x8 array. Arrays can only be indexed by numbers because the numbers are used as an offset from the base address of the array. You can't name them without doing something different or adding more framework.
>Is there any way to name the elements as certain names?
Yes, plenty of ways, but it depends on how much effort you want to expend to do it, as the previous posts show. :) It also affects how you treat the "array". For example, if you use a hash table keyed on strings instead of an array, you can't use the convenient index scheme anymore.
>and also how do you change the colour of text..
In some system or compiler dependent way. If you're on Windows, doing a console mode program, as seems to be the case for most people, you can use the Win32 API function SetConsoleTextAttribute where the attributes can be mixed and matched for a lot of colors.