python五子棋人机对战
时间: 2023-06-21 19:24:45 浏览: 31
下面是一个简单的 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 is_valid_move(board, row, col):
if row < 0 or row >= 15 or col < 0 or col >= 15:
return False
if board[row][col] != 0:
return False
return True
def is_win(board, piece):
# Check rows
for i in range(15):
if np.all(board[i:i+5, :] == piece):
return True
# Check columns
for i in range(15):
if np.all(board[:, i:i+5] == piece):
return True
# Check diagonals
for i in range(11):
for j in range(11):
if np.all(board[i:i+5, j:j+5].diagonal() == piece):
return True
if np.all(board[i:i+5, j:j+5][::-1].diagonal() == piece):
return True
return False
def print_board(board):
symbols = {0: '.', 1: 'X', 2: 'O'}
print(' ' + ' '.join([str(i) for i in range(15)]))
for i in range(15):
row = [symbols[int(x)] for x in board[i]]
print(str(i) + ' ' + ' '.join(row))
def ai_move(board):
for i in range(15):
for j in range(15):
if board[i][j] == 0:
board_copy = board.copy()
place_piece(board_copy, i, j, 2)
if is_win(board_copy, 2):
return (i, j)
for i in range(15):
for j in range(15):
if board[i][j] == 0:
board_copy = board.copy()
place_piece(board_copy, i, j, 1)
if is_win(board_copy, 1):
return (i, j)
for i in [7, 3, 11, 6, 8, 2, 12, 5, 9, 1, 13, 4, 10, 0, 14]:
for j in [7, 3, 11, 6, 8, 2, 12, 5, 9, 1, 13, 4, 10, 0, 14]:
if board[i][j] == 0:
return (i, j)
def play_game():
board = create_board()
current_player = 1
print_board(board)
while True:
if current_player == 1:
row = int(input("Player " + str(current_player) + ", enter row: "))
col = int(input("Player " + str(current_player) + ", enter column: "))
if is_valid_move(board, row, col):
place_piece(board, row, col, current_player)
print_board(board)
if is_win(board, current_player):
print("Player " + str(current_player) + " wins!")
break
if np.count_nonzero(board) == 225:
print("Game over, it's a tie!")
break
current_player = 3 - current_player
else:
print("Invalid move, try again.")
else:
row, col = ai_move(board)
place_piece(board, row, col, current_player)
print("AI placed piece at row " + str(row) + ", column " + str(col))
print_board(board)
if is_win(board, current_player):
print("AI wins!")
break
if np.count_nonzero(board) == 225:
print("Game over, it's a tie!")
break
current_player = 3 - current_player
play_game()
```
这个代码实现了一个简单的五子棋人机对战,AI 的思路是先检查是否有自己的五子连珠的机会,如果没有则检查是否有玩家的五子连珠机会,如果还没有则在棋盘的中间位置下子。您可以运行代码试试看。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)