带递归的Tic-Tac-Toe(Python)

带递归的Tic-Tac-Toe(Python),python,recursion,tic-tac-toe,Python,Recursion,Tic Tac Toe,我不知道如何将这些功能结合在一起,形成一个硬AI,在这个AI中,它永远不会失败。我应该以某种形式使用递归,这些函数名和契约是预先编写的,我填写了实际的定义。过了很久,我在谷歌上找不到任何相关信息。伙计们,有什么想法吗 """ State S2 is a *successor* of state S1 if S2 can be the the next state after S1 in a legal game of tic tac toe. safe: state -> Bool

我不知道如何将这些功能结合在一起,形成一个硬AI,在这个AI中,它永远不会失败。我应该以某种形式使用递归,这些函数名和契约是预先编写的,我填写了实际的定义。过了很久,我在谷歌上找不到任何相关信息。伙计们,有什么想法吗

"""


State S2 is a *successor* of state S1 if S2 can be the the
next state after S1 in a legal game of tic tac toe.

safe: state -> Bool
successor: state x state -> Bool

1. If S is over, then S is safe if 'x' does not have 3 in a row in S.
2. If it is o's move in S, then S is safe iff at least one successor of S is safe.
3. If it is x's move in S, then S is safe iff all successors of S are safe.

A *stateList* is a list of states. 
"""


# safe: state-> Bool
#
# A state S is *safe* if player 'o' can force a win or tie from S.

def safe(S):
    if over(S): return not threeInRow('x',S)
    if turn(S)=='o': return someSafeSuccessor(S)
    if turn(S)=='x': return allSafeSuccessors(S)

def threeInRow(p,S):
    if p == 'x':
        if all(t in S[0] for t in (1,2,3)):
            return True
        elif all(t in S[0] for t in (4,5,6)):
            return True
        elif all(t in S[0] for t in (7,8,9)):
            return True
        elif all(t in S[0] for t in (1,4,7)):
            return True
        elif all(t in S[0] for t in (2,5,8)):
            return True
        elif all(t in S[0] for t in (3,6,9)):
            return True
        elif all(t in S[0] for t in (1,5,9)):
            return True
        elif all(t in S[0] for t in (3,5,7)):
            return True
    else:
        if all(t in S[1] for t in (1,2,3)):
            return True
        elif all(t in S[1] for t in (4,5,6)):
            return True
        elif all(t in S[1] for t in (7,8,9)):
            return True
        elif all(t in S[1] for t in (1,4,7)):
            return True
        elif all(t in S[1] for t in (2,5,8)):
            return True
        elif all(t in S[1] for t in (3,6,9)):
            return True
        elif all(t in S[1] for t in (1,5,9)):
            return True
        elif all(t in S[1] for t in (3,5,7)):
            return True

# someSafeSuccessor: state -> Bool
#
# If S is a state, someSafeSuccessor(S) means that S has
# at least one safe successor.

def someSafeSuccessor(S):
    flag = False
    # flag means we have found a safe successor
    for x in successors(S):
        if safe(x): flag = True
    return flag

# allSafeSuccessors: state -> Bool
#
# If S is a state, allSafeSuccessors(S) means that every
# successor of S is safe.
def allSafeSuccessors(S):
  flag = True
  for x in successors(S):
    if not safe(x): flag = False
  return flag    


# successors: state -> stateList
#
# successors(S) is a list whose members are all of the successors of S.
def successors(S):
  stateList=[]
  for i in range(1,10):
    if empty(i,S):
      stateList.extend(S[0],S[1]+[i])
  return stateList

跟进我的评论

描述minimax(/alpha-beta剪枝)算法时可视化的树不是“真正的树”,因为您在内存中构建了整个树。这是一个概念树,首先测试每个移动深度的结果,记录每个叶子的分数(alpha、beta等)并向上传播

注意单词,深度优先。这意味着您的递归minimax实现函数从调用它自己的第一步开始。从它能采取的第一个行动开始。依此类推,直到达到最大深度或终端移动,然后返回。你可以从这个逻辑中看出,除了目前正在考虑的单链移动之外,你的内存或任何外部存储中永远不会有更多的板(并且,在每个级别上,你都会迭代通过它进行的可能移动的列表-因此也有关于你到底走了多远的内存,等等)


tl;dr通过执行深度优先的极大极小递归,除了单个递归函数外,您不会生成任何新函数。

我建议您搜索极大极小(极大极小树,极大极小树…)和alpha-beta修剪。@shx2您显然没有读到问题:)我不能使用这些树,这很糟糕,因为关于它们的信息太多了。我应该使用上面提供的函数来创建decision@Yblock像和这样的页面有很好的伪代码进行分析。如果这仍然不起作用,拿出笔和纸,用你的头脑/用英语/用你自己的话想一想你会怎么做。然后将其转换为伪代码并进行比较。请不要提供金钱奖励(现金、比特币等)来进行此类工作。这不是它的工作原理。