logo CodeStepByStep logo

knightCanMove

Language/Type: C++ Grid collections
Related Links:
Author: Marty Stepp (on 2016/06/16)

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 "" "" "" "" "" "" "" ""
Type your C++ solution code here:


This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.