python简易五子棋
时间: 2023-10-08 12:08:03 浏览: 74
python《五子棋(控制台版)》+源码+使用教程+程序配置+可执行程序,可作为毕业设计或者期末作业
5星 · 资源好评率100%
好的,以下是一个简易的 Python 五子棋游戏实现:
```python
import os
# 初始化棋盘
board = []
for i in range(15):
board.append(['+']*15)
# 打印棋盘
def print_board():
os.system('cls')
print(' ', end='')
for i in range(15):
print(chr(65+i), end=' ')
print()
for i in range(15):
print(i+1, end=' ')
for j in range(15):
print(board[i][j], end=' ')
print()
# 判断胜负
def check_win(row, col):
# 横向判断
count = 1
for i in range(col-1, -1, -1):
if board[row][i] == board[row][col]:
count += 1
else:
break
for i in range(col+1, 15):
if board[row][i] == board[row][col]:
count += 1
else:
break
if count >= 5:
return True
# 纵向判断
count = 1
for i in range(row-1, -1, -1):
if board[i][col] == board[row][col]:
count += 1
else:
break
for i in range(row+1, 15):
if board[i][col] == board[row][col]:
count += 1
else:
break
if count >= 5:
return True
# 左上-右下判断
count = 1
i = row - 1
j = col - 1
while i >= 0 and j >= 0:
if board[i][j] == board[row][col]:
count += 1
i -= 1
j -= 1
else:
break
i = row + 1
j = col + 1
while i < 15 and j < 15:
if board[i][j] == board[row][col]:
count += 1
i += 1
j += 1
else:
break
if count >= 5:
return True
# 右上-左下判断
count = 1
i = row - 1
j = col + 1
while i >= 0 and j < 15:
if board[i][j] == board[row][col]:
count += 1
i -= 1
j += 1
else:
break
i = row + 1
j = col - 1
while i < 15 and j >= 0:
if board[i][j] == board[row][col]:
count += 1
i += 1
j -= 1
else:
break
if count >= 5:
return True
return False
# 开始游戏
player = 'B'
while True:
print_board()
print('当前玩家:', player)
move = input('请输入您的落子(例如:A1):')
col = ord(move[0]) - ord('A')
row = int(move[1:]) - 1
if board[row][col] == '+':
board[row][col] = player
if check_win(row, col):
print_board()
print(player, '获胜!')
break
player = 'B' if player == 'W' else 'W'
else:
print('此位置已经下过,请重新输入!')
```
运行上述代码,即可开始玩简易五子棋游戏。
阅读全文