用python代码做一个五子棋小游戏
时间: 2023-07-26 09:03:47 浏览: 177
好的,以下是一个简单的五子棋小游戏的 Python 代码:
```python
import numpy as np
def create_board():
board = np.zeros((15,15))
return board
def place(board, player, position):
if board[position] == 0:
board[position] = player
else:
print("This position is already occupied. Please choose another one.")
position = tuple(map(int, input().split(",")))
place(board, player, position)
def win(board, player):
# Check rows
for i in range(15):
if all(board[i,j] == player for j in range(11)):
return True
# Check columns
for j in range(15):
if all(board[i,j] == player for i in range(11)):
return True
# Check diagonal
for i in range(11):
for j in range(11):
if all(board[i+k,j+k] == player for k in range(11)):
return True
# Check reverse diagonal
for i in range(11):
for j in range(4,15):
if all(board[i+k,j-k] == player for k in range(11)):
return True
return False
board = create_board()
player = 1
while True:
print(board)
print("Player", player, "turn.")
position = tuple(map(int, input().split(",")))
place(board, player, position)
if win(board, player):
print(board)
print("Player", player, "wins!")
break
player = 3 - player # Switch player
```
这个程序使用了 NumPy 库来创建一个二维数组作为棋盘,玩家用 1 和 2 来表示,每个玩家轮流在棋盘上放置自己的棋子,同时判断是否胜利。如果有一方胜利,程序结束并宣布胜利者。
阅读全文