高级农民
- 积分
- 1252
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-12-20
- 最后登录
- 1970-1-1
|
- from typing import List
- # Represents a cell in the minesweeper board.
- # A cell has a value (either a number or a mine), and a state (either hidden or revealed).
- class Cell:
- def __init__(self, value: str, state: str):
- self.value = value
- self.state = state
- # Creates a minesweeper board with the specified mine locations, width, and height.
- # The board is represented as a 2D list of Cell objects.
- def createBoard(mine_locations: List[Tuple[int, int]], width: int, height: int) -> List[List[Cell]]:
- board = []
- for i in range(height):
- row = []
- for j in range(width):
- if (i, j) in mine_locations:
- cell = Cell('*', 'hidden')
- else:
- cell = Cell(' ', 'hidden')
- row.append(cell)
- board.append(row)
- return board
- # Prints the specified minesweeper board with an additional new line at the end.
- def printBoard(board: List[List[Cell]]):
- for row in board:
- for cell in row:
- if cell.state == 'hidden':
- print('#', end='')
- else:
- print(cell.value, end='')
- print()
- print()
- # Reveals the specified cell in the minesweeper board and prints the board.
- # If the cell contains a mine, prints "game over" and returns False.
- # Otherwise, returns True.
- def click(board: List[List[Cell]], x: int, y: int) -> bool:
- if board[x][y].value == '*':
- print('game over')
- return False
- board[x][y].state = 'revealed'
- printBoard(board)
- return True
复制代码 |
|