【问题标题】:Tic Tac Toe with recursion (Python)带有递归的井字游戏(Python)
【发布时间】:2013-04-18 05:12:57
【问题描述】:

我不知道如何将这些功能结合在一起以形成一个硬 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

【问题讨论】:

  • 我建议你 google minimax (minimax tree, min-max tree...) 和 alpha-beta pruning。
  • @shx2 你显然没有看问题:)
  • 我不能使用那些树,这很糟糕,因为关于它们的信息太多了。我应该使用上面提供的函数来做出决定

标签: python recursion tic-tac-toe


【解决方案1】:

跟进我的评论。

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

注意单词,深度优先。这意味着您的递归 minimax-implementing 函数首先调用它自己可以做的第一步。首先是它可以采取的第一步调用自己。依此类推,直到达到最大深度或最终移动,然后返回。您可以通过这个逻辑看到,除了现在正在考虑的单个移动链之外,您在内存或任何外部存储中永远不会有更多的板(并且,在每个级别,您将遍历可能的移动列表)它 - 所以还有关于你有多远的记忆,等等)。

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

【讨论】:

  • @Yblock 像 en.wikipedia.org/wiki/Minimaxen.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning 这样的页面有很好的伪代码可供分析。如果还是不行,拿出笔和纸,想想你会如何在头脑中/用英语/用你自己的话来做。然后将其转换为伪代码并进行比较。
  • 请不要为 SO 上的工作提供金钱奖励(现金、比特币等)。这不是它的工作原理。
猜你喜欢
  • 2019-04-25
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 2013-12-21
  • 1970-01-01
相关资源
最近更新 更多