> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Check if Move is Legal Python Solution

> Tested Python solution for LeetCode 1958 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1958, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix), [Enumeration](/catalog/topics/enumeration). [View on LeetCode](https://leetcode.com/problems/check-if-move-is-legal/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1958   # by problem number
lcpy gen -s check_if_move_is_legal   # by problem name
```

## Problem

You are given a 0-indexed 8 x 8 grid board, where board\[r]\[c] represents the cell (r, c) on a game board. On the board, free cells are represented by '.', white cells are represented by 'W', and black cells are represented by 'B'.

Each move in this game consists of choosing a free cell and changing it to the color you are playing as (either white or black). However, a move is only legal if, after changing it, the cell becomes the endpoint of a good line (horizontal, vertical, or diagonal).

A good line is a line of three or more cells (including the endpoints) where the endpoints of the line are one color, and the remaining cells in the middle are the opposite color (no cells in the line are free). You can find examples for good lines in the figure below:

![Good lines](https://assets.leetcode.com/uploads/2021/07/22/goodlines5.png)

Given two integers rMove and cMove and a character color representing the color you are playing as (white or black), return true if changing cell (rMove, cMove) to color color is a legal move, or false if it is not legal.

Example 1:

Input: board = \[\[".",".",".","B",".",".",".","."],\[".",".",".","W",".",".",".","."],\[".",".",".","W",".",".",".","."],\[".",".",".","W",".",".",".","."],\["W","B","B",".","W","W","W","B"],\[".",".",".","B",".",".",".","."],\[".",".",".","B",".",".",".","."],\[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B"
Output: true
Explanation: '.', 'W', and 'B' are represented by the colors blue, white, and black respectively, and cell (rMove, cMove) is marked with an 'X'.
The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.

Example 2:

Input: board = \[\[".",".",".",".",".",".",".","."],\[".","B",".",".","W",".",".","."],\[".",".","W",".",".",".",".","."],\[".",".",".","W","B",".",".","."],\[".",".",".",".",".",".",".","."],\[".",".",".",".","B","W",".","."],\[".",".",".",".",".",".","W","."],\[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W"
Output: false
Explanation: While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.

Constraints:

board.length == board\[r].length == 8
0 \<= rMove, cMove \< 8
board\[rMove]\[cMove] == '.'
color is either 'B' or 'W'.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/07/10/grid11.png)

```
Input: board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B"
Output: true
Explanation: The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.
```

![Example 2](https://assets.leetcode.com/uploads/2021/07/10/grid2.png)

```
Input: board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W"
Output: false
Explanation: While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.
```

### Constraints

* board.length == board\[r].length == 8
* 0 \<= rMove, cMove \< 8
* board\[rMove]\[cMove] == '.'
* color is either 'B' or 'W'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_move_is_legal/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_move_is_legal/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(8 * 8) = O(1)
    # Space: O(1)
    def check_move(self, board: list[list[str]], r_move: int, c_move: int, color: str) -> bool:
        directions = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1))
        for dr, dc in directions:
            r, c = r_move + dr, c_move + dc
            seen = 0
            while 0 <= r < 8 and 0 <= c < 8 and board[r][c] != ".":
                if board[r][c] == color:
                    if seen >= 1:
                        return True
                    break
                seen += 1
                r += dr
                c += dc
        return False
```

## Complexity

| Time | Space |
| - | - |
| O(8 \* 8) = O(1) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.