利用python设计一个五子棋游戏
时间: 2023-12-27 13:20:01 浏览: 85
好的,以下是一个简单的五子棋游戏的Python代码:
```python
import numpy as np
# 初始化游戏棋盘
def create_board():
board = np.zeros((15, 15))
return board
# 判断当前位置是否可落子
def is_valid_move(board, row, col):
return board[row][col] == 0
# 落子操作
def place_stone(board, row, col, player):
board[row][col] = player
# 判断是否有玩家获胜
def check_win(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 main():
board = create_board()
player = 1
game_over = False
while not game_over:
row = int(input("请输入行数(0-14):"))
col = int(input("请输入列数(0-14):"))
if is_valid_move(board, row, col):
place_stone(board, row, col, player)
if check_win(board, player):
print(f"玩家{player}获胜!")
game_over = True
player = 3 - player # 交换玩家
print(board)
if __name__ == '__main__':
main()
```
运行该代码,即可开始体验五子棋游戏。
阅读全文