How on earth does this code work

A rook can move horizontally or vertically. So a rook can attack another rook if they’re in the same row or column of the chessboard, i.e. there are at least two rooks in the same row or column.

This code seems to assume that the only pieces in the chessboard are rooks. If a square has a piece the value of the list element is 1, otherwise it’s 0.

The first for loop goes through each row of the chessboard, counting the number of squares that have a piece in that row. If the count is more than 1, the rooks are not safe, and it returns false.

The second for loop does the same thing, but goes by column instead of row.

If neither loop returns false, it means all the rooks are safe, and it returns true at the end.

Browse More Popular Posts

Leave a Comment