栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

人工智能作业 极大极小值和剪枝算法 tictactoe游戏

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

人工智能作业 极大极小值和剪枝算法 tictactoe游戏

import copy
import math
# import sys  # 导入sys模块
# sys.setrecursionlimit(3000000)  # 将默认的递归深度修改为3000
import random

X = "X"
O = "O"
EMPTY = None


def Counter(board):
    CounterX = 0
    CounterO = 0
    CounterE = 0
    ConuterList = []

    # if terminal(board):
    #     return 1

    for i in range(0, 3):
        for j in range(0, 3):
            if board[i][j] == X:
                CounterX += 1
            elif board[i][j] == O:
                CounterO += 1
            else:
                CounterE += 1
    ConuterList.append(CounterX)
    ConuterList.append(CounterO)
    ConuterList.append(CounterE)
    return ConuterList

def initial_state():
    """
    Returns starting state of the board.
    """
    return [[EMPTY, EMPTY, EMPTY],
            [EMPTY, EMPTY, EMPTY],
            [EMPTY, EMPTY, EMPTY]]


def player(board):
    """
    Returns player who has the next turn on a board.
    """
    CounterX = 0
    CounterO = 0
    CounterE = 0
    if terminal(board):
        return 1

    for i in range(0,3):
        for j in range(0, 3):
            if board[i][j] == X:
                CounterX += 1
            elif board[i][j] == O:
                CounterO += 1
            else:CounterE += 1
    if CounterE == 9:
        return X
    elif CounterX > CounterO:
        return O
    elif CounterX == CounterX:
        return X








def actions(board):
    """
    Returns set of all possible actions (i, j) available on the board.
    """
    if terminal(board):
        return 0
    ActionList=[]
    for i in range(0,3):
        for j in range(0, 3):
           if board[i][j] == EMPTY:
               ActionList.append((i,j))
    return ActionList










def result(board, action):
    """
    Returns the board that results from making move (i, j) on the board.
    """
    Newboard = copy.deepcopy(board)


    if player(Newboard) == X:
       Newboard[action[0]][action[1]] = X
       return Newboard
    elif player(Newboard) == O:
       Newboard[action[0]][action[1]] = O
       return Newboard
    else:
        raise Exception





def winner(board):
    """
    Returns the winner of the game, if there is one.
    """


    if terminal(board):
        counterList = Counter(board)
        for i in range(0, 3):
            if board[0][0] == board[1][1] == board[2][2] != EMPTY:
                if board[0][0] == X:
                    winner = 'X'
                else:
                    winner = 'O'
                return winner
            elif board[0][2] == board[1][1] == board[2][0] != EMPTY:
                if board[0][2] == X:
                    winner = 'X'
                else:
                    winner = 'O'
                return winner
            elif board[i][0] == board[i][1] == board[i][2] != EMPTY:
                if board[i][0] == X:
                    winner = 'X'
                else:
                    winner = 'O'
                return winner
            elif board[0][i] == board[1][i] == board[2][i] != EMPTY:
                if board[1][i] == X:
                    winner = 'X'
                else:
                    winner = 'O'
                return winner
        if counterList[2] == 0:
            winner = None
            return winner





def terminal(board):
    """
    Returns True if game is over, False otherwise.
    """
    if board[0][0] == board[1][1] == board[2][2] and board[0][0] != EMPTY:

        return True
    elif board[0][2] == board[1][1] == board[2][0] and board[0][2] != EMPTY:
        return True
    for i in range(0,3):
            if board[i][0] == board[i][1] == board[i][2]!= EMPTY:
                return True
            elif board[0][i] == board[1][i] == board[2][i]!= EMPTY:
                return True
            elif Counter(board)[2] == 0:

                return True
    return False











def utility(board):
    """
    Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
    """
    if terminal(board):

        win = winner(board)
        if win == 'X':
            return 1
        elif win == 'O':
            return  -1
        else:
            return 0



def minimax(board):
    """
    Returns the optimal action for the current player on the board.
    """




    if terminal(board):
        return None





    #X
    if player(board) == X:
        actionlist = actions(board)
        bestmove = [None]*9
        index = 0
        bestValue = -2
        for i in range(0,len(actionlist)):
            Newboard = copy.deepcopy(board)
            Newboard[actionlist[i][0]][actionlist[i][1]] = X
            value = Min(Newboard)
            if value > bestValue:
                bestValue = value
                index = 0
                bestmove[0] = actionlist[i]
            elif value == bestValue:
                index += 1
                bestmove[index] = (actionlist[i])
        if index > 1:
            index = random.randint(0,index)
        return bestmove[index]







    if player(board) == O:
        actionlist = actions(board)
        bestmove = [None]*9
        index = 0
        bestValue = 2
        for i in range(0, len(actionlist)):
            Newboard = copy.deepcopy(board)
            Newboard[actionlist[i][0]][actionlist[i][1]] = O
            value = Max(Newboard)
            if value < bestValue:
                bestValue = value
                index = 0
               #bestmove.append(actionlist[i])
                bestmove[0] = actionlist[i]
            elif value == bestValue:
                index += 1
                #bestmove.append(actionlist[i])
                bestmove[index] = (actionlist[i])


        if index > 0:
            index = random.randint(0, index)
        print(bestmove[index])
        return bestmove[index]





















def Max(board):
    """
    Returns the optimal action for the current player on the board.
    """

    if terminal(board):
        return utility(board)
    INF=-2
    actionList = actions(board)

    for i in  actionList:
        Newboard = result(board,i)
        val = Min(Newboard)
        if val > INF:
            INF = val


    return INF


















def Min(board):
    """
    Returns the optimal action for the current player on the board.
    """
    if terminal(board):
        return utility(board)
    INF = 2
    actionList = actions(board)

    for i in actionList:
        Newboard = result(board, i)
        val = Max(Newboard)
        if val < INF:
            INF = val
    return INF

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/276111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号