python实现五子棋AI
时间: 2023-05-30 10:02:27 浏览: 49
作为AI,需要实现以下功能:
1. 判断胜负:判断当前局面是否出现五子连珠的情况,如果是则结束游戏。
2. 估值函数:评估当前局面的优劣,给出一个分数。这个分数需要考虑当前棋子的数量、棋型、棋子的位置等因素。
3. 搜索算法:搜索下一步的最优落子位置。可以使用alpha-beta剪枝算法。
具体实现代码如下:
```python
import random
class Board:
def __init__(self, size=15):
self.size = size
self.board = [[0] * size for _ in range(size)]
self.current_player = 1
def move(self, x, y):
self.board[x][y] = self.current_player
self.current_player = 3 - self.current_player
def is_valid_move(self, x, y):
return x >= 0 and x < self.size and y >= 0 and y < self.size and self.board[x][y] == 0
def get_winner(self):
for i in range(self.size):
for j in range(self.size):
if self.board[i][j] == 0:
continue
if j + 4 < self.size and self.board[i][j] == self.board[i][j+1] == self.board[i][j+2] == self.board[i][j+3] == self.board[i][j+4]:
return self.board[i][j]
if i + 4 < self.size and self.board[i][j] == self.board[i+1][j] == self.board[i+2][j] == self.board[i+3][j] == self.board[i+4][j]:
return self.board[i][j]
if i + 4 < self.size and j + 4 < self.size and self.board[i][j] == self.board[i+1][j+1] == self.board[i+2][j+2] == self.board[i+3][j+3] == self.board[i+4][j+4]:
return self.board[i][j]
if i + 4 < self.size and j - 4 >= 0 and self.board[i][j] == self.board[i+1][j-1] == self.board[i+2][j-2] == self.board[i+3][j-3] == self.board[i+4][j-4]:
return self.board[i][j]
return 0
def get_score(self, player):
score = 0
for i in range(self.size):
for j in range(self.size):
if self.board[i][j] == player:
score += self.evaluate_position(i, j)
elif self.board[i][j] == 3 - player:
score -= self.evaluate_position(i, j)
return score
def evaluate_position(self, x, y):
score = 0
for dx, dy in [(0, 1), (1, 0), (1, 1), (1, -1)]:
score += self.evaluate_direction(x, y, dx, dy)
return score
def evaluate_direction(self, x, y, dx, dy):
score = 0
player = self.board[x][y]
for i in range(1, 5):
nx, ny = x + i * dx, y + i * dy
if nx < 0 or nx >= self.size or ny < 0 or ny >= self.size:
break
if self.board[nx][ny] == player:
score += 1
elif self.board[nx][ny] == 0:
pass
else:
score -= 1
break
return score
class AI:
def __init__(self, player, depth=3):
self.player = player
self.depth = depth
def get_move(self, board):
_, move = self.alpha_beta(board, self.depth, float("-inf"), float("inf"), self.player)
return move
def alpha_beta(self, board, depth, alpha, beta, player):
if depth == 0:
return board.get_score(self.player), None
moves = self.generate_moves(board)
if not moves:
return board.get_score(self.player), None
best_move = None
if player == self.player:
for move in moves:
board.move(*move)
score, _ = self.alpha_beta(board, depth - 1, alpha, beta, 3 - player)
board.move(*move[::-1])
if score > alpha:
alpha = score
best_move = move
if alpha >= beta:
break
return alpha, best_move
else:
for move in moves:
board.move(*move)
score, _ = self.alpha_beta(board, depth - 1, alpha, beta, 3 - player)
board.move(*move[::-1])
if score < beta:
beta = score
best_move = move
if alpha >= beta:
break
return beta, best_move
def generate_moves(self, board):
moves = []
for i in range(board.size):
for j in range(board.size):
if board.is_valid_move(i, j):
moves.append((i, j))
random.shuffle(moves)
return moves
board = Board()
ai = AI(1)
while True:
print(board.board)
winner = board.get_winner()
if winner:
print("Winner is player", winner)
break
if board.current_player == 1:
x, y = map(int, input("Please input your move: ").split())
if board.is_valid_move(x, y):
board.move(x, y)
else:
print("Invalid move!")
continue
else:
move = ai.get_move(board)
board.move(*move)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)