Check if Move is Legal
This problem can be approached by checking in all eight directions from the given cell (rMove, cMove)
. If we find a line that fulfills the criteria of a ‘good line’ as specified in the problem statement, we return True
.
Python solution:
|
|
In the code above:
dirs
is a list of eight directions. Each direction is represented by a pair of integers.opposite_color
is the opposite color ofcolor
.- For each direction, we first move one step from
(rMove, cMove)
to(x, y)
. If(x, y)
is inside the board and its color isopposite_color
, we continue moving in the same direction. - During the movement, if we find a cell with color
color
, we returnTrue
. If we find a cell that is free, we break the loop and continue with the next direction. - If we don’t find any good line in all eight directions, we return
False
.