### matrix help

Hello all
I have some project about chess so I have 2 rooks and I have to mark which part of matrix is marked by rook.

Something like this A1 and B2 are rooks in this case :
http://i.imgur.com/51fGtsn.png

Problem is if I have rooks in the same column (A1,B1) :
http://i.imgur.com/m4oxbcj.png

 ``12345678910111213141516171819202122`` ``````for(i=0;i<8;i++){ for(j=0;j<8;j++){ if((board[i][j]==rook1)||(board[i][j]==rook2)) { for(q=0;q<8;q++) { for(p=j;p

Anyone have some idea ? :)
Sorry for bad english :(
Last edited on
Please help haha :(
As you go through the board, if you find a rook, you set the row and columns of the board to "1". The problem occurs because whatever value the rooks are, it is not equal to "1".

Understand, you have something like this:
 ``12345678910111213141516`` ``````start: r1 -- -- r2 -- -- -- -- -- -- -- -- -- -- -- -- looking for rooks... found rook at 0,0 setting board to "1" 01 01 01 01 01 -- -- -- 01 -- -- -- 01 -- -- -- no more rooks``````

I assume this is for figuring out the valid moves of the rook? Chess is not an easy thing to program.

We started a ChessPlusPlus program at github, you can find it here:
https://github.com/naraku9333/ChessPlusPlus
(I think, maybe you have to log in...)

I did some work on the logic (still needs work on en-passent and castling).

Rooks can move horizontally and vertically. Bishops and queens can move diagonally, so we start with something like this:
 ``1234567891011`` ``````enum Board_Direction{ D_NONE = 0, NORTH = 1, EAST = NORTH << 1, SOUTH = EAST << 1, WEST = SOUTH << 1, NORTH_EAST = NORTH | EAST, SOUTH_EAST = SOUTH | EAST, SOUTH_WEST = SOUTH | WEST, NORTH_WEST = NORTH | WEST };``````

I really need to clean this code up a little, I should be ashamed ( :) )

I stored possible moves as a std::bitset<64>. If a move is valid, it is a 1 (otherwise 0). A rook's possible moves can be calculated as:
 ``12345678`` ``````// Piece* pieces is an array of all of the pieces // Piece* board[] are pointers to the pieces as they sit on the board void Piece::rook_moves (Piece* pieces, Piece* board[]){ shootPath(NORTH, board, this->m_moves, *this); shootPath(EAST , board, this->m_moves, *this); shootPath(SOUTH, board, this->m_moves, *this); shootPath(WEST , board, this->m_moves, *this); }``````

Just a matter of making shootPath()
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````void Piece::shootPath(Board_Direction d, Piece* board[], std::bitset& set, Piece& piece, bool addFriends){ // What is the pieces position? int t_x = piece.xPos(); int t_y = piece.yPos(); // How should the position change int x_off = d & EAST ? 1 : d & WEST ? -1 : 0; int y_off = d & SOUTH ? 1 : d & NORTH ? -1 : 0; // Move that direction t_x += x_off; t_y += y_off; // Make the position an array idx int t_arr = t_x + t_y * WIDTH; while (IsValid(t_x) && IsValid(t_y)) { if (!board[t_arr]) // if there isn't a piece there { set[t_arr] = 1; // I can go there } else if (board[t_arr]->color() != piece.color()) // the piece is an enemy { set[t_arr] = 1; break; // but I can't do anything else } else { if (addFriends) set[t_arr] = 1; break; } t_x += x_off; t_y += y_off; t_arr = t_x + (t_y * WIDTH); } }``````

addFriends is a duct tape solution for fixing a "move makes check" issue. Each piece has a std::bitset for its valid moves. The king can not move into check so it goes something like this:
 ``123456`` ``````-for every piece of the color that just moved (say white) -make the valid moves - black king make moves - for every piece of other color - black_king.moves &= (~piece.moves)``````

This does not work when the king could capture a piece but still be in check, so I threw in the "add friends" parameter.

Add friends should really just be the case always, the friends being removed when we actually want to move that piece

Last edited on
Topic archived. No new replies allowed.