logo CodeStepByStep logo

knightCanMove

Language/Type: C++ Grid collections
Related Links:

Write a function named knightCanMove that accepts a reference to a Grid of strings and two row/column pairs (r1, c1), (r2, c2) as parameters, and returns true if there is a knight at chess board square (r1, c1) and he can legally move to empty square (r2, c2). For your function to return true, there must be a knight at square (r1, c1), and the square at (r2, c2) must store an empty string, and both locations must be within the bounds of the grid.

Recall that a knight makes an "L" shaped move, going 2 squares in one dimension and 1 square in the other. For example, if the board looks as shown below and the board square at (1, 2) stores "knight", then the call of knightCanMove(board, 1, 2, 2, 4) returns true.

r\c 0 1 2 3 4 5 6 7
0 "" "" "" "" "king" "" "" ""
1 "" "" "knight" "" "" "" "" ""
2 "" "" "" "" "" "" "" ""
3 "" "rook" "" "" "" "" "" ""
4 "" "" "" "" "" "" "" ""
5 "" "" "" "" "" "" "" ""
6 "" "" "" "" "" "" "" ""
7 "" "" "" "" "" "" "" ""
Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.