python3.6 ai井字棋 alpha-beta剪枝3

所有代码: 

import numpy as np
from tkinter import *


class Game(object):
    def __init__(self):
        self.chess = np.zeros((3, 3), dtype=int)  # 棋盘状态数组  0---空格  1---叉电脑  2---圈玩家
        self.iscircle = True  # 当前圈下,默认玩家先手
        self.select_i, self.select_j = -1, -1
        self.root = Tk()
        self.root.title('井字棋')
        self.canvas = Canvas(self.root, width=230, height=230, bg="white")
        self.canvas.pack()
        self.canvas.create_rectangle(25, 25, 205, 205, fill="#CA9762")
        for i in range(1, 5):
            self.canvas.create_line(25, 25 + 60 * (i - 1), 205, 25 + 60 * (i - 1))  # 横线
            self.canvas.create_line(25 + 60 * (i - 1), 25, 25 + 60 * (i - 1), 205)  # 竖线

        self.canvas.bind("<Button-1>", self.player)
        self.root.mainloop()

    def player(self, event):
        x = event.x
        y = event.y
        if self.iscircle and (25 <= x <= 205) and (25 <= y <= 205):
            i = int((y - 25) / 60)
            j = int((x - 25) / 60)
            if self.chess[i][j] == 0:
                self.chess[i][j] = 2
                # 画圈
                self.drawcircle(i, j)
                # 画完,判断是否结束游戏
                self.isGameOver(2)
                self.iscircle = False
                # 轮到电脑下
                self.computer()

    def win(self, chesstemp, who):
        t = chesstemp.copy()
        for i in range(3):
            if t[i][0] == who and t[i][1] == who and t[i][2] == who:  # 竖直方向
                return True
            if t[0][i] == who and t[1][i] == who and t[2][i] == who:  # 水平方向
                return True
        if t[0][0] == who and t[1][1] == who and t[2][2] == who:
            return True
        if t[0][2] == who and t[1][1] == who and t[2][0] == who:
            return True
        return False

    def isGameOver(self, who):
        # 游戏结束 1、满格平局  2、电脑胜 3、玩家胜
        t = self.chess.copy()
        empty_cells = [(i, j) for i in range(3) for j in range(3) if t[i][j] == 0]

        if self.win(t, who):
            return True
        else:
            if len(empty_cells) == 0:  # 没有人赢,但是没有空格了,游戏结束
                return True
            else:  # 游戏没有结束
                return False

    def drawcircle(self, i, j):
        x = 25 + 60 * j
        y = 25 + 60 * i
        self.canvas.create_oval(x + 30 - 20, y + 30 - 20, x + 30 + 20, y + 30 + 20)

    def drawcha(self, i, j):
        x = 25 + 60 * j
        y = 25 + 60 * i
        self.canvas.create_line(x, y, x + 60, y + 60)
        self.canvas.create_line(x + 60, y, x, y + 60)

    def computer(self):  #
        # 进行 a-b剪枝 ,返回下一步该下的坐标
        if not self.iscircle:

            i, j, score = self.alphabeta2(self.chess, -10000, 10000, True, 2000000000)
            if 0 <= i < 3 and 0 <= j < 3:
                print('Computer下棋的位置 ,i ,j ', i, j, score)
                self.drawcha(i, j)
                self.chess[i][j] = 1
                self.isGameOver(1)
                self.iscircle = True
            '''
            score = self.alphabeta3(self.chess, -10000, 10000, True, True, 6)
            print('Computer下棋的位置 ,i ,j ', self.select_i, self.select_j, score)
            if 0 <= self.select_i < 3 and 0 <= self.select_j < 3:
                self.drawcha(self.select_i
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是实现AI井字棋alpha-beta剪枝算法的Python代码: ```python import math MAX = math.inf MIN = -math.inf def minimax(board, depth, alpha, beta, is_maximizing): result = check_winner(board) if result != None: return result if is_maximizing: best_score = MIN for i in range(3): for j in range(3): if board[i][j] == ' ': board[i][j] = 'X' score = minimax(board, depth+1, alpha, beta, False) board[i][j] = ' ' best_score = max(score, best_score) alpha = max(alpha, best_score) if beta <= alpha: break return best_score else: best_score = MAX for i in range(3): for j in range(3): if board[i][j] == ' ': board[i][j] = 'O' score = minimax(board, depth+1, alpha, beta, True) board[i][j] = ' ' best_score = min(score, best_score) beta = min(beta, best_score) if beta <= alpha: break return best_score def find_best_move(board): best_score = MIN best_move = None for i in range(3): for j in range(3): if board[i][j] == ' ': board[i][j] = 'X' score = minimax(board, 0, MIN, MAX, False) board[i][j] = ' ' if score > best_score: best_score = score best_move = (i, j) return best_move def check_winner(board): for i in range(3): if board[i][0] == board[i][1] == board[i][2] != ' ': return 1 if board[i][0] == 'X' else -1 if board[0][i] == board[1][i] == board[2][i] != ' ': return 1 if board[0][i] == 'X' else -1 if board[0][0] == board[1][1] == board[2][2] != ' ': return 1 if board[0][0] == 'X' else -1 if board[0][2] == board[1][1] == board[2][0] != ' ': return 1 if board[0][2] == 'X' else -1 for i in range(3): for j in range(3): if board[i][j] == ' ': return None return 0 board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] while True: print(board[0]) print(board[1]) print(board[2]) winner = check_winner(board) if winner != None: if winner == 0: print("Tie!") else: print("You win!" if winner == 1 else "AI wins!") break row = int(input("Enter row (0, 1, or 2): ")) col = int(input("Enter column (0, 1, or 2): ")) if board[row][col] != ' ': print("Invalid move!") continue board[row][col] = 'O' winner = check_winner(board) if winner != None: if winner == 0: print("Tie!") else: print("You win!" if winner == 1 else "AI wins!") break row, col = find_best_move(board) board[row][col] = 'X' ``` 这个代码实现了一个简单的人机对战井字棋游戏,其中AI使用了alpha-beta剪枝算法来下棋。当玩家输入坐标时,程序会将玩家的棋子放在对应的位置,并检查是否有获胜者。如果没有获胜者,AI会使用minimax算法和alpha-beta剪枝来找到最好的下棋位置,并将棋子放在那里。程序会不断循环,直到游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值