请用python写一个五子棋游戏
时间: 2023-10-10 11:06:47 浏览: 109
好的,以下是Python实现的五子棋游戏代码:
```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 get_valid_moves(board):
valid_moves = []
for row in range(15):
for col in range(15):
if board[row][col] == 0:
valid_moves.append((row, col))
return valid_moves
def check_win(board, piece):
# 检查行
for row in range(15):
for col in range(11):
if board[row][col] == piece and board[row][col+1] == piece \
and board[row][col+2] == piece and board[row][col+3] == piece \
and board[row][col+4] == piece:
return True
# 检查列
for row in range(11):
for col in range(15):
if board[row][col] == piece and board[row+1][col] == piece \
and board[row+2][col] == piece and board[row+3][col] == piece \
and board[row+4][col] == piece:
return True
# 检查斜线
for row in range(11):
for col in range(11):
if board[row][col] == piece and board[row+1][col+1] == piece \
and board[row+2][col+2] == piece and board[row+3][col+3] == piece \
and board[row+4][col+4] == piece:
return True
for row in range(11):
for col in range(4, 15):
if board[row][col] == piece and board[row+1][col-1] == piece \
and board[row+2][col-2] == piece and board[row+3][col-3] == piece \
and board[row+4][col-4] == piece:
return True
return False
def main():
board = create_board()
game_over = False
turn = 1
while not game_over:
if turn == 1:
row = int(input("Player 1 (black), enter row: "))
col = int(input("Player 1 (black), enter column: "))
piece = 1
else:
row = int(input("Player 2 (white), enter row: "))
col = int(input("Player 2 (white), enter column: "))
piece = 2
if board[row][col] == 0:
place_piece(board, row, col, piece)
if check_win(board, piece):
if turn == 1:
print("Player 1 (black) wins!")
else:
print("Player 2 (white) wins!")
game_over = True
turn = 1 if turn == 2 else 2
else:
print("That spot is taken, please try again.")
if __name__ == '__main__':
main()
```
希望以上代码可以帮助您实现一个简单的五子棋游戏。
阅读全文