用python写个五子棋程序
时间: 2023-10-26 12:07:35 浏览: 84
抱歉,作为AI语言模型,我无法编写实际的程序。但是,以下是一个简单的五子棋游戏的代码示例,你可以参考它:
```python
import numpy as np
# 创建棋盘
def create_board():
board = np.zeros((15,15))
return board
# 在棋盘上放置棋子
def place_piece(board, row, col, piece):
board[row][col] = piece
# 检查棋子是否合法
def is_valid_move(board, row, col):
if row < 0 or row > 14 or col < 0 or col > 14:
return False
elif board[row][col] != 0:
return False
else:
return True
# 检查是否有五个棋子连成一线
def is_win(board, piece):
# 检查行
for i in range(15):
if all(board[i][j] == piece for j in range(11)):
return True
# 检查列
for j in range(15):
if all(board[i][j] == piece for i in range(11)):
return True
# 检查正对角线
for i in range(11):
for j in range(11):
if all(board[i+k][j+k] == piece for k in range(5)):
return True
# 检查反对角线
for i in range(11):
for j in range(4, 15):
if all(board[i+k][j-k] == piece for k in range(5)):
return True
return False
# 游戏主程序
def game():
board = create_board()
print(board)
game_over = False
turn = 1
while not game_over:
if turn == 1:
row = int(input("Player 1, enter row: "))
col = int(input("Player 1, enter column: "))
if is_valid_move(board, row, col):
place_piece(board, row, col, 1)
if is_win(board, 1):
print("Player 1 wins!")
game_over = True
turn = 2
else:
print("Invalid move. Try again.")
else:
row = int(input("Player 2, enter row: "))
col = int(input("Player 2, enter column: "))
if is_valid_move(board, row, col):
place_piece(board, row, col, 2)
if is_win(board, 2):
print("Player 2 wins!")
game_over = True
turn = 1
else:
print("Invalid move. Try again.")
print(board)
game()
```
阅读全文