diff --git a/cli.py b/cli.py index d6a22bc..1148d29 100644 --- a/cli.py +++ b/cli.py @@ -1,8 +1,13 @@ #!/usr/bin/env python from board2 import Board +from board2 import BoardTools from pos import TwoDPos from pos import TwoDUtils +from node import Node + +class StateError(BaseException): + pass piecemaps = { 0: '.', @@ -11,6 +16,36 @@ piecemaps = { } mappings = TwoDUtils.reverse +def tree_of_node(node): + parent = node + print("considering board with moveset " + str(parent.inner.moves)) + + possible = parent.inner.possible() + if len(possible) != 0: + print("permuting!") + for i in possible: + copy = parent.inner.copy() + copy.append(i) + childnode = Node() + childnode.inner = copy + parent.children.append(childnode) + for i in parent.children: + tree_of_node(i) + else: + print("calculated score") + rawscore = BoardTools.winning(None, parent.inner.metaboard()) + if rawscore == Board.cross: + score = -1 + elif rawscore == Board.nought: + score = 1 + elif rawscore == Board.tie: + score = 0 + else: + score = None + raise StateError("aaaa") + + print("score" + str(score)) + def render(board): print("123 456 789") moves = board.possible() @@ -36,7 +71,7 @@ def render(board): print("to play: " + str(board.turn)) -def help(): +def help_text(): print("m - make a move") print("p - see coordinates of legal moves") print("t - see whose turn it is") @@ -59,7 +94,7 @@ def main(): b = Board() print("type h for help") while True: - try: +# try: cmd = input("> ") spl = cmd.split(' ') if spl[0] == "m": @@ -71,11 +106,18 @@ def main(): elif spl[0] == "r": render(b) elif spl[0] == "h": - render(b) + help_text() + elif spl[0] == "z": + # create node from current board + node = Node() + node.inner = b + + tree_of_node(node) + elif spl[0] == "q": break; - except: - print("error occured") +# except: +# print("error occured") return if __name__ == "__main__": diff --git a/node.py b/node.py index 72626b0..f2fc64c 100644 --- a/node.py +++ b/node.py @@ -4,6 +4,7 @@ class Node: def __init__(self): self.children = [] self.score = None + self.inner = None def calculate(self): if self.children == []: return self.score @@ -14,5 +15,3 @@ class Node: if score != None: items += 1 total += score - -