0
這裏的具體名單是主類:構建海峽
class GameStateNode:
'''
A tree of possible states for a two-player, sequential move, zero-sum,
perfect-information game.
value: GameState -- the game state at the root of this tree
children: list -- all possible game states that can be reached from this
game state via one legal move in the game. children is None until grow
is called.
'''
def __init__(self, game_state):
''' (GameStateNode, GameState) -> NoneType
Initialize a new game state tree consisting of a single root node
that contains game_state.
'''
self.value = game_state
self.children = []
這裏是我目前工作的功能:
def game_descriptions(root):
''' (GameStateNode) -> list of str
Return a list containing a str describing each complete game that is
possible from the game stored at root.
Assume root is the root of a game state tree specifically for the game
Subtract Square.
>>> s = SubtractSquareState('p1', current_total = 6)
>>> root = GameStateNode(s)
>>> root.grow()
>>> game_descriptions(root)
['p1:6 -> p2:2 -> p1:1 -> p2:0 = p1 wins!', 'p1:6 -> p2:5 -> p1:1 -> p2:0 = p1 wins!', 'p1:6 -> p2:5 -> p1:4 -> p2:0 = p1 wins!', 'p1:6 -> p2:5 -> p1:4 -> p2:3 -> p1:2 -> p2:1 -> p1:0 = p2 wins!']
'''
def _build_paths(root, L = []):
''' (GameStateNode) -> list of str '''
if root.children:
for child in root.children:
a = abbreviated(root.value)
a += ' -> {}'.format(abbreviated(child.value))
L.append(a)
_build_paths(child, L)
else:
pass
return L
def abbreviated(s):
'''(GameState) -> str
Return an abbreviated str representation of SubtractSquareState s.
'''
return "{}:{}".format(s.next_player, s.current_total)
正如你可以在FCN game_descriptions看到我需要返回按照最後的勝利者的順序列出遊戲狀態。我目前的問題是使用fcn _build_paths。我希望它能夠在沒有獲勝者的情況下返回一個遊戲描述列表,因爲我將執行在fcn game_descriptions中獲勝的人。 我想這一點,例如:
>>> root = GameStateNode(SubtractSquareState('p1', current_total = 4))
>>> root.grow()
>>> _build_paths(root)
['p1:4 -> p2:0', 'p1:4 -> p2:3 -> p1:2 -> p2:1 -> p1:0']
相反,我得到這個:
什麼是'SubtractSquareState:
你將與調用此('p1',current_total = 4))'? – 2015-04-03 21:44:37
SubtractSquareState是遊戲SubtractSquare的當前狀態,其中玩家從當前總數中減去數字的平方以試圖達到0(即1,4,9,16)。在這種情況下,在current_total = 4時,可以減去4或1,導致下一個狀態爲'p2:0'或'p2:3'。等等。 – FistLauncher 2015-04-03 21:47:07
我有不到4個小時的時間來完成這項任務,所以任何幫助/想法都是值得歡迎的! – FistLauncher 2015-04-03 22:11:34