五子棋游戏python
时间: 2023-08-28 18:07:51 浏览: 88
以下是一个简单的五子棋游戏的Python代码,供您参考:
```python
import numpy as np
# 初始化棋盘
def create_board():
board = np.zeros((15,15))
return board
# 判断落子是否合法
def valid_move(board, row, col):
if board[row][col] == 0:
return True
else:
return False
# 落子
def drop_piece(board, row, col, piece):
board[row][col] = piece
# 判断是否有五子连珠
def winning_move(board, piece):
# 判断行
for r in range(15):
for c in range(11):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece and board[r][c+4] == piece:
return True
# 判断列
for r in range(11):
for c in range(15):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece and board[r+4][c] == piece:
return True
# 判断正斜线
for r in range(11):
for c in range(11):
if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece and board[r+4][c+4] == piece:
return True
# 判断反斜线
for r in range(4, 15):
for c in range(11):
if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece and board[r-4][c+4] == piece:
return True
# 主函数
def game():
board = create_board()
print(board)
game_over = False
turn = 1
while not game_over:
# 玩家1落子
if turn == 1:
row = int(input("Player 1 enter row:"))
col = int(input("Player 1 enter col:"))
if valid_move(board, row, col):
drop_piece(board, row, col, 1)
if winning_move(board, 1):
print("Player 1 wins!")
game_over = True
turn = 2
# 玩家2落子
else:
row = int(input("Player 2 enter row:"))
col = int(input("Player 2 enter col:"))
if valid_move(board, row, col):
drop_piece(board, row, col, 2)
if winning_move(board, 2):
print("Player 2 wins!")
game_over = True
turn = 1
print(board)
game()
```
这是一个比较简单的实现,只涉及到了基本的落子和判断胜负的逻辑。您可以根据自己的需求进行修改和完善。
阅读全文