写一个五子棋python 代码
时间: 2024-02-18 22:36:02 浏览: 57
AI智能五子棋Python代码
4星 · 用户满意度95%
这是一个简单的五子棋python代码:
```
board = [[0 for x in range(19)] for y in range(19)] # 初始化棋盘
def check_win(x, y, player):
# 检查是否有五个连续的棋子
# 向上下左右四个方向检查
count = 0
for i in range(x, x+5):
if i >= 0 and i < 19 and board[i][y] == player:
count += 1
else:
break
if count == 5:
return True
count = 0
for i in range(y, y+5):
if i >= 0 and i < 19 and board[x][i] == player:
count += 1
else:
break
if count == 5:
return True
count = 0
for i in range(-4, 5):
if x+i >= 0 and x+i < 19 and y+i >= 0 and y+i < 19 and board[x+i][y+i] == player:
count += 1
else:
break
if count == 5:
return True
count = 0
for i in range(-4, 5):
if x+i >= 0 and x+i < 19 and y-i >= 0 and y-i < 19 and board[x+i][y-i] == player:
count += 1
else:
break
if count == 5:
return True
return False
def play(x, y, player):
board[x][y] = player # 在棋盘上下棋
if check_win(x, y, player): # 检查是否有人获胜
print("Player", player, "wins!")
return True
return False
# 游戏主循环
while True:
x = int(input("Player 1 enter x: ")) # 输入棋子位置
y = int(input("Player 1 enter y: "))
if play(x, y, 1): # Player 1 下棋
break
x = int(input("Player 2 enter x: ")) # 输入棋子位置
y = int(input("Player 2 enter y: "))
if play(x, y, 2): # Player 2 下棋
break
```
这仅仅是一个简单的五子棋游戏,可以作为你学习python代码的参考。如果你对代码有任何疑问,请随时告诉我。
阅读全文