-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution for check if move is legal.
- Loading branch information
1 parent
a3fac8b
commit 9d4c878
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class Solution { | ||
public: | ||
bool search(vector<vector<char>>& board, int rMove, int cMove, char g_color, int i, int j) | ||
{ | ||
rMove += i; | ||
cMove += j; | ||
int turn = 1; | ||
|
||
|
||
// Loop over the path until either condition is certain | ||
while(rMove < 8 && cMove < 8 && rMove >= 0 && cMove >= 0 | ||
&& board[rMove][cMove] != '.') | ||
{ | ||
if(turn > 1 && board[rMove][cMove] == g_color) {return true;} | ||
else if(board[rMove][cMove] == g_color) return false; | ||
turn++; | ||
rMove += i; | ||
cMove += j; | ||
} | ||
|
||
return false; | ||
} | ||
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) { | ||
// A problem about searching the cells around and checking conditions | ||
// Eight different moving patterns that needs to be checked | ||
// For each pattern we need to ensure that the first next two celles are the | ||
// opposite color. After this, we need to find a same color. | ||
// If no such exist (either boundary conditions or ".") then return false | ||
// OR the results together and return | ||
|
||
// Early termination: | ||
if(board[rMove][cMove] != '.') | ||
{ | ||
return false; | ||
} | ||
|
||
|
||
// Loop over all possible combinations of row and column traversal | ||
for(int i = -1; i <= 1; i++) | ||
{ | ||
for(int j = -1; j <= 1; j++) | ||
{ | ||
if(search(board, rMove, cMove, color, i, j)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
}; |