python 五子棋 悔棋
时间: 2024-01-13 14:20:25 浏览: 167
以下是使用Python实现五子棋悔棋的示例代码:
```python
class Gobang:
def __init__(self):
self.board = [[' ' for _ in range(15)] for _ in range(15)]
self.history = []
def play(self, row, col, player):
self.board[row][col] = player
self.history.append((row, col))
def undo(self):
if self.history:
row, col = self.history.pop()
self.board[row][col] = ' '
def print_board(self):
for row in self.board:
print(' '.join(row))
# 创建五子棋对象
game = Gobang()
# 玩家1下棋
game.play(7, 7, 'X')
game.print_board()
# 玩家2下棋
game.play(8, 8, 'O')
game.print_board()
# 玩家1悔棋
game.undo()
game.print_board()
```
这段代码创建了一个Gobang类,其中包含了play方法用于下棋,undo方法用于悔棋,print_board方法用于打印当前棋盘状态。你可以根据需要进行修改和扩展。
阅读全文