python字符型五子棋人工对战
时间: 2023-10-08 22:11:13 浏览: 40
以下是一个简单的 Python 代码,用于实现两个人工智能之间的五子棋对战。其中,每个人工智能都会根据当前棋盘状态进行决策,并在棋盘上放置自己的棋子,直到有一方获胜或者棋盘被下满。
```
import numpy as np
# 初始化棋盘
def init_board():
board = np.zeros((15, 15))
return board
# 判断当前位置是否可以下棋子
def is_valid_move(board, row, col):
return board[row][col] == 0
# 判断是否有一方获胜
def is_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 game_over(board):
if is_win(board, 1) or is_win(board, 2):
return True
if len(np.where(board == 0)[0]) == 0:
return True
return False
# 人工智能下棋
def ai_move(board, player):
for i in range(15):
for j in range(15):
if is_valid_move(board, i, j):
board[i][j] = player
if is_win(board, player):
return
board[i][j] = 0
# 人工智能对战
def play():
board = init_board()
player = 1
while not game_over(board):
if player == 1:
row = int(input("Player 1, Enter row: "))
col = int(input("Player 1, Enter col: "))
if is_valid_move(board, row, col):
board[row][col] = player
else:
print("Invalid move, try again.")
continue
else:
ai_move(board, player)
print(f"Current board:\n{board}")
player = 3 - player
if is_win(board, 1):
print("Player 1 wins!")
elif is_win(board, 2):
print("Player 2 wins!")
else:
print("Tie game.")
# 开始游戏
if __name__ == "__main__":
play()
```
这个程序中,我们使用了一个 15x15 的 numpy 数组表示棋盘,为了方便,在每个位置上,0 表示空位,1 表示玩家1,2 表示玩家2。我们通过 is_valid_move 函数来判断一个位置是否可以下棋子,通过 is_win 函数来判断是否有一方获胜。在 ai_move 函数中,我们使用了简单的枚举方式来实现人工智能的决策。最后,我们通过 play 函数来初始化游戏并进行对战。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)