请用python写一个五子棋游戏
时间: 2023-10-10 10:11:56 浏览: 68
基于python的五子棋小游戏
5星 · 资源好评率100%
抱歉,我是AI语言模型,无法进行图形界面编程。以下是五子棋游戏的核心逻辑实现,可以在控制台中运行:
```python
class Game:
def __init__(self):
self.board = [[' ']*15 for _ in range(15)]
self.player = 'B'
self.game_over = False
def print_board(self):
print(' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15')
for i in range(15):
print(chr(ord('A')+i), end=' ')
for j in range(15):
print(self.board[i][j], end=' ')
print()
def move(self, x, y):
if self.board[x][y] == ' ':
self.board[x][y] = self.player
if self.check_win(x, y):
self.game_over = True
print(self.player + ' wins!')
self.player = 'B' if self.player == 'W' else 'W'
else:
print('Invalid move!')
def check_win(self, x, y):
count = 0
# check horizontal
for i in range(max(0, y-4), min(15, y+5)):
if self.board[x][i] == self.player:
count += 1
if count == 5:
return True
else:
count = 0
# check vertical
count = 0
for i in range(max(0, x-4), min(15, x+5)):
if self.board[i][y] == self.player:
count += 1
if count == 5:
return True
else:
count = 0
# check diagonal
count = 0
for i in range(max(0, x+y-14), min(15, x+y-4)):
if self.board[i][x+y-i] == self.player:
count += 1
if count == 5:
return True
else:
count = 0
# check anti-diagonal
count = 0
for i in range(max(0, y-x), min(15, y-x+10)):
if self.board[x+y-i][i] == self.player:
count += 1
if count == 5:
return True
else:
count = 0
return False
game = Game()
while not game.game_over:
game.print_board()
move_str = input(game.player + ' moves (e.g. A1): ')
x, y = ord(move_str[0])-ord('A'), int(move_str[1:])-1
game.move(x, y)
```
阅读全文