Question:
There is a chessboard, let's say an array of 100 elements, if you represent it in the form of a square, you get the following
9 9 9 9 9 9 9 9 9 9
9 0 0 0 0 0 0 0 0 9
9 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 9
9 0 0 0 0 0 0 0 0 0
9 0 3 0 0 5 0 0 0 9
9 0 0 0 0 0 0 0 0 0
9 0 0 0 0 2 0 0 0 9
9 0 0 0 0 0 0 0 0 0
9 9 9 9 9 9 9 9 9 9
number 5 indicates the position of the rook on the field, it can move vertically and horizontally (for those who do not play chess). Help write an algorithm for finding available moves, it should be taken into account that someone else's piece may be caught on its way – it is indicated by the number 3 on the field, then the cell with the figure is available for hitting, or my own figure is the number 2. I suffer for more than 2 hours, except for a stupid search in turn four directions, nothing in the head does not climb (
Answer:
var x = f.x, y = f.y; // сохраняем координаты
while(table[--x][y] == 0)
{
// помечаем клетку для прохода
if(table[x][y] <= 9) // своя фигура или край доски; отдельная проверка для черных/белых фигур
{
// снимаем отметку
}
}
//и по аналогии для ++x, --y и ++y
I think something like that.