#!/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: '.', 1: 'x', 2: 'o', } 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() moves = [i.glob() for i in moves] for i in range(9): buf = [] for j in range(9): ri = mappings[i] string = ''.join([ri, str(j + 1)]) pos = TwoDPos(TwoDPos.g, string) item = board.get(*pos.local()) if string in moves: buf.append('?') else: buf.append(piecemaps[item]) if j in [2, 5]: buf.append(" | ") if j == 8: buf.append(" " + ri) print(''.join(buf)) if i in [2, 5]: print("----+-----+----") print("to play: " + str(board.turn)) def help_text(): print("m - make a move") print("p - see coordinates of legal moves") print("t - see whose turn it is") print("r - render the board to the screen") print("q - quit program") print("h - show help (this list)") def move(board, move): move = TwoDPos(TwoDPos.g, move) print(move) board.append(move) def possibilities(board): print(board.possible()) def turn(board): print("to play: " + str(board.turn)) def main(): b = Board() print("type h for help") while True: # try: cmd = input("> ") spl = cmd.split(' ') if spl[0] == "m": move(b, spl[1]) elif spl[0] == "p": possibilities(b) elif spl[0] == "t": turn(b) elif spl[0] == "r": render(b) elif spl[0] == "h": 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") return if __name__ == "__main__": main()