Check if Move is Legal LeetCode solution in C++

1
class Solution {
2
public:
3
bool inBoard(vector<vector<char>> &board, int x, int y) {
4
return x >= 0 && x < board.size() && y >= 0 && y < board[0].size();
5
}
6

7
bool isLegal(vector<vector<char>> &board, int x, int y, char color) {
8
if (color == 'B') return board[x][y] == 'W';
9
if (color == 'W') return board[x][y] == 'B';
10
return false;
11
}
12

13
bool checkMove(vector<vector<char>> &board, int rMove, int cMove, char color) {
14
vector<int> dir_x = {0, 0, 1, 1, 1, -1, -1, -1}, dir_y = {1, -1, 1, -1, 0, 1, -1, 0};
15
for (int i = 0; i < 8; i++) {
16
int x = rMove + dir_x[i], y = cMove + dir_y[i], count = 0;
17
while (inBoard(board, x, y) && isLegal(board, x, y, color)) {
18
x += dir_x[i], y += dir_y[i];
19
count++;
20
}
21
if (inBoard(board, x, y) && board[x][y] == color && count > 0) return true;
22
}
23
return false;
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0