Quickie on Arrays...

This is a discussion on Quickie on Arrays... within the C++ Programming forums, part of the General Programming Boards category; We'll say I had an 8x8 array like this: Code: char chessBoard[8][8] = { { 'r', 'n', 'b', 'q', 'k', ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262

    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..
    Last edited by twomers; 12-14-2005 at 06:54 AM. Reason: whoops

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    take pointers to indivisual rows

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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;   
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    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';

  5. #5
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  5. 2d arrays quickie
    By gordy in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2002, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21