用python写一个五子棋
时间: 2024-01-08 21:46:33 浏览: 20
好的,我可以为您提供一个简单的五子棋程序,使用Python语言编写。
```python
import numpy as np
# 创建棋盘
def create_board():
board = np.zeros((15,15))
return board
# 检查是否有玩家获胜
def win_game(board, player):
# 水平方向
for i in range(15):
for j in range(11):
if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
return True
# 垂直方向
for i in range(11):
for j in range(15):
if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
return True
# 左上到右下方向
for i in range(11):
for j in range(11):
if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player:
return True
# 右上到左下方向
for i in range(11):
for j in range(4, 15):
if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player:
return True
return False
# 下棋
def place(board, player, row, col):
board[row][col] = player
# 主函数
def game():
board = create_board()
player = 1
game_over = False
while not game_over:
# 玩家1下棋
if player == 1:
row = int(input("玩家1,请输入行号:"))
col = int(input("玩家1,请输入列号:"))
place(board, player, row, col)
if win_game(board, player):
print("玩家1获胜!")
game_over = True
player = 2
# 玩家2下棋
else:
row = int(input("玩家2,请输入行号:"))
col = int(input("玩家2,请输入列号:"))
place(board, player, row, col)
if win_game(board, player):
print("玩家2获胜!")
game_over = True
player = 1
print(board)
game()
```
这个程序使用了numpy库来创建棋盘,并且使用了一个简单的算法来检查是否有玩家获胜。在游戏中,玩家1的棋子用数字1表示,玩家2的棋子用数字2表示。玩家可以通过输入行号和列号来下棋。
阅读全文