python五子棋人机对战
时间: 2023-09-02 09:15:44 浏览: 82
python 五子棋人机对战
好的,下面是一个简单的 Python 五子棋人机对战的代码实现,使用了 MiniMax 算法来实现 AI 的决策。
```python
import copy
import random
class Board:
def __init__(self):
self.board = [[0 for x in range(15)] for y in range(15)]
def get_empty_cells(self):
cells = []
for i in range(15):
for j in range(15):
if self.board[i][j] == 0:
cells.append((i, j))
return cells
def is_game_over(self):
# Check rows
for i in range(15):
for j in range(11):
if self.board[i][j] != 0 and self.board[i][j] == self.board[i][j+1] and self.board[i][j] == self.board[i][j+2] and self.board[i][j] == self.board[i][j+3] and self.board[i][j] == self.board[i][j+4]:
return True
# Check columns
for i in range(11):
for j in range(15):
if self.board[i][j] != 0 and self.board[i][j] == self.board[i+1][j] and self.board[i][j] == self.board[i+2][j] and self.board[i][j] == self.board[i+3][j] and self.board[i][j] == self.board[i+4][j]:
return True
# Check diagonal (top-left to bottom-right)
for i in range(11):
for j in range(11):
if self.board[i][j] != 0 and self.board[i][j] == self.board[i+1][j+1] and self.board[i][j] == self.board[i+2][j+2] and self.board[i][j] == self.board[i+3][j+3] and self.board[i][j] == self.board[i+4][j+4]:
return True
# Check diagonal (top-right to bottom-left)
for i in range(11):
for j in range(4, 15):
if self.board[i][j] != 0 and self.board[i][j] == self.board[i+1][j-1] and self.board[i][j] == self.board[i+2][j-2] and self.board[i][j] == self.board[i+3][j-3] and self.board[i][j] == self.board[i+4][j-4]:
return True
# Check if the board is full
for i in range(15):
for j in range(15):
if self.board[i][j] == 0:
return False
return True
def make_move(self, move, player):
self.board[move[0]][move[1]] = player
def undo_move(self, move):
self.board[move[0]][move[1]] = 0
def print_board(self):
for i in range(15):
print(self.board[i])
class AI:
def __init__(self):
self.player = None
def mini_max(self, board, depth, alpha, beta, maximizing_player):
if depth == 0 or board.is_game_over():
return self.get_board_score(board)
if maximizing_player:
max_score = float('-inf')
for move in board.get_empty_cells():
board.make_move(move, self.player)
score = self.mini_max(board, depth-1, alpha, beta, False)
board.undo_move(move)
max_score = max(max_score, score)
alpha = max(alpha, score)
if beta <= alpha:
break
return max_score
else:
min_score = float('inf')
for move in board.get_empty_cells():
board.make_move(move, 3 - self.player)
score = self.mini_max(board, depth-1, alpha, beta, True)
board.undo_move(move)
min_score = min(min_score, score)
beta = min(beta, score)
if beta <= alpha:
break
return min_score
def get_board_score(self, board):
score = 0
# Check rows
for i in range(15):
for j in range(11):
if board.board[i][j] == self.player and board.board[i][j+1] == self.player and board.board[i][j+2] == self.player and board.board[i][j+3] == self.player:
score += 1000
# Check columns
for i in range(11):
for j in range(15):
if board.board[i][j] == self.player and board.board[i+1][j] == self.player and board.board[i+2][j] == self.player and board.board[i+3][j] == self.player:
score += 1000
# Check diagonal (top-left to bottom-right)
for i in range(11):
for j in range(11):
if board.board[i][j] == self.player and board.board[i+1][j+1] == self.player and board.board[i+2][j+2] == self.player and board.board[i+3][j+3] == self.player:
score += 1000
# Check diagonal (top-right to bottom-left)
for i in range(11):
for j in range(4, 15):
if board.board[i][j] == self.player and board.board[i+1][j-1] == self.player and board.board[i+2][j-2] == self.player and board.board[i+3][j-3] == self.player:
score += 1000
return score
def get_best_move(self, board):
empty_cells = board.get_empty_cells()
best_score = float('-inf')
best_move = random.choice(empty_cells)
for move in empty_cells:
board.make_move(move, self.player)
score = self.mini_max(board, 3, float('-inf'), float('inf'), False)
board.undo_move(move)
if score > best_score:
best_score = score
best_move = move
return best_move
def play_game():
board = Board()
ai = AI()
ai.player = 1 if random.randint(0, 1) == 0 else 2
while not board.is_game_over():
if ai.player == 1:
x, y = map(int, input("Enter your move (row, column): ").split())
while board.board[x][y] != 0:
print("Invalid move. Try again.")
x, y = map(int, input("Enter your move (row, column): ").split())
board.make_move((x, y), ai.player)
else:
move = ai.get_best_move(board)
board.make_move(move, ai.player)
print(f"AI plays ({move[0]}, {move[1]})")
board.print_board()
print()
ai.player = 3 - ai.player
winner = 3 - ai.player
if winner == 1:
print("You win!")
elif winner == 2:
print("AI wins!")
else:
print("It's a tie!")
if __name__ == '__main__':
play_game()
```
这个实现中,我们定义了一个 `Board` 类来表示棋盘和相关操作,还定义了一个 `AI` 类来实现 AI 的决策。AI 使用 MiniMax 算法来搜索最佳的下棋位置。在 `get_board_score` 方法中,我们定义了一些简单的规则来评估棋盘的得分。在 `play_game` 方法中,我们交替让玩家和 AI 下棋,直到游戏结束。
阅读全文