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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值