五子棋人机对战python 代码
时间: 2023-07-09 20:51:56 浏览: 103
好的,以下是一个简单的五子棋人机对战的Python代码示例,使用了蒙特卡罗树搜索算法。
```python
import numpy as np
import random
# 定义棋盘大小
BOARD_SIZE = 15
# 定义玩家和电脑的棋子
player = 1
computer = 2
# 定义棋盘
board = np.zeros((BOARD_SIZE, BOARD_SIZE))
# 定义蒙特卡罗树搜索算法
class MCTS():
def __init__(self):
self.visits = 1
self.wins = 0
self.child_nodes = None
def select_child(self):
best_score = -1
best_child = None
for child in self.child_nodes:
score = child.wins / child.visits + 1.4 * np.sqrt(np.log(self.visits) / child.visits)
if score > best_score:
best_score = score
best_child = child
return best_child
def expansion(self):
self.child_nodes = []
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == 0:
new_node = MCTS()
new_node.board = np.copy(board)
new_node.board[i][j] = computer
self.child_nodes.append(new_node)
def simulation(self):
current_board = np.copy(self.board)
current_player = player
while True:
available_pos = np.where(current_board == 0)
if len(available_pos[0]) == 0:
return 0
random_pos = random.randint(0, len(available_pos[0]) - 1)
i = available_pos[0][random_pos]
j = available_pos[1][random_pos]
current_board[i][j] = current_player
if check_win(current_board, current_player):
if current_player == computer:
return 1
else:
return -1
current_player = 3 - current_player
def backpropagation(self, result):
self.visits += 1
self.wins += result
if self.parent_node:
self.parent_node.backpropagation(result)
# 定义检查胜利的函数
def check_win(board, player):
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == player:
if j + 4 < BOARD_SIZE 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
if i + 4 < BOARD_SIZE 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
if i + 4 < BOARD_SIZE and j + 4 < BOARD_SIZE 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
if i + 4 < BOARD_SIZE and j - 4 >= 0 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 computer_move():
root = MCTS()
root.board = np.copy(board)
for i in range(1000):
node = root
while node.child_nodes is not None:
node = node.select_child()
node.expansion()
winner = node.simulation()
node.backpropagation(winner)
best_child = None
best_wins = -1
for child in root.child_nodes:
if child.wins > best_wins:
best_wins = child.wins
best_child = child
board = np.copy(best_child.board)
return board
# 定义玩家下棋的函数
def player_move(x, y):
if board[x][y] == 0:
board[x][y] = player
return True
else:
return False
# 主函数
if __name__ == '__main__':
while True:
print(board)
x, y = input("请输入你要下的位置,用空格分隔:").split()
if player_move(int(x), int(y)):
if check_win(board, player):
print("恭喜你,你赢了!")
break
board = computer_move()
print(board)
if check_win(board, computer):
print("你输了,再接再厉!")
break
else:
print("该位置已有棋子,请重新输入!")
```
这只是一个简单的示例代码,您可以根据实际需求进行修改和优化。同时,注意这段代码可能存在语法错误和逻辑问题,仅供您参考。
阅读全文