python围棋代码
时间: 2023-07-02 20:05:49 浏览: 83
以下是一个简单的 Python 围棋代码示例,可以实现二人对弈的功能:
```python
class GoBoard:
def __init__(self, size=19):
self.size = size
self.board = [[0 for _ in range(size)] for _ in range(size)]
self.current_player = 1
def play(self, x, y):
if self.board[x][y] != 0:
print("Invalid move!")
return False
self.board[x][y] = self.current_player
self.current_player = 3 - self.current_player
return True
def display(self):
for i in range(self.size):
for j in range(self.size):
if self.board[i][j] == 0:
print(".", end=" ")
elif self.board[i][j] == 1:
print("X", end=" ")
else:
print("O", end=" ")
print()
```
使用示例:
```python
board = GoBoard()
board.display()
while True:
x = int(input("Player " + str(board.current_player) + ", enter x coordinate: "))
y = int(input("Player " + str(board.current_player) + ", enter y coordinate: "))
board.play(x, y)
board.display()
```
运行后,程序将提示玩家依次输入坐标,然后显示当前棋盘状态。