def test_gtp_state(tc): fx = Gtp_state_fixture(tc) fx.check_command('nonsense', [''], "unknown command", expect_failure=True) fx.check_command('protocol_version', [''], "2") fx.player.set_next_move("A3", "preprogrammed move 0") fx.check_command('genmove', ['B'], "A3") game_state = fx.player.last_game_state # default board size is min(acceptable_sizes) tc.assertEqual(game_state.size, 9) b = boards.Board(9) b.play(2, 0, 'b') tc.assertEqual(game_state.board, b) tc.assertEqual(game_state.komi, 0.0) tc.assertEqual(game_state.history_base, boards.Board(9)) tc.assertEqual(game_state.move_history, []) tc.assertIsNone(game_state.ko_point) tc.assertIsNone(game_state.handicap) tc.assertIs(game_state.for_regression, False) tc.assertIsNone(game_state.time_settings) tc.assertIsNone(game_state.time_remaining) tc.assertIsNone(game_state.canadian_stones_remaining) fx.check_command('gomill-explain_last_move', [], "preprogrammed move 0") fx.check_command('play', ['W', 'A4'], "") fx.check_command('komi', ['5.5'], "") fx.player.set_next_move("C9") fx.check_command('genmove', ['B'], "C9") game_state = fx.player.last_game_state tc.assertEqual(game_state.komi, 5.5) tc.assertEqual(game_state.history_base, boards.Board(9)) tc.assertEqual(len(game_state.move_history), 2) tc.assertEqual( game_state.move_history[0], gtp_states.History_move('b', (2, 0), "preprogrammed move 0")) tc.assertEqual(game_state.move_history[1], gtp_states.History_move('w', (3, 0))) fx.check_command('genmove', ['B'], "pass") fx.check_command('gomill-explain_last_move', [], "") fx.check_command('genmove', ['W'], "pass") fx.check_command( 'showboard', [], dedent(""" 9 . . # . . . . . . 8 . . . . . . . . . 7 . . . . . . . . . 6 . . . . . . . . . 5 . . . . . . . . . 4 o . . . . . . . . 3 # . . . . . . . . 2 . . . . . . . . . 1 . . . . . . . . . A B C D E F G H J""")) fx.player.set_next_move_resign() fx.check_command('genmove', ['B'], "resign") fx.check_command('quit', [''], "", expect_end=True)
def test_full_board_selfcapture(tc): b = boards.Board(9) tc.assertTrue(b.is_empty()) tc.assertItemsEqual(b.list_occupied_points(), []) for row in range(9): for col in range(9): b.play(row, col, 'b') tc.assertEqual(b, boards.Board(9)) tc.assertIs(b.is_empty(), True)
def reset(self): self.board = boards.Board(self.board_size) # None, or a small integer self.handicap = None self.simple_ko_point = None # Player that any simple_ko_point is banned for self.simple_ko_player = None self.history_base = boards.Board(self.board_size) # list of History_move objects self.move_history = []
def test_range_checks(tc): b = boards.Board(9) tc.assertRaises(IndexError, b.get, -1, 2) tc.assertRaises(IndexError, b.get, 9, 2) tc.assertRaises(IndexError, b.get, 2, -1) tc.assertRaises(IndexError, b.get, 2, 9) tc.assertRaises(IndexError, b.play, -1, 2, 'b') tc.assertRaises(IndexError, b.play, 9, 2, 'b') tc.assertRaises(IndexError, b.play, 2, -1, 'b') tc.assertRaises(IndexError, b.play, 2, 9, 'b') tc.assertEqual(b, boards.Board(9))
def test_get_setup_and_moves_board_provided(tc): b = boards.Board(9) g1 = sgf.Sgf_game.from_string(SAMPLE_SGF) board1, plays1 = sgf_moves.get_setup_and_moves(g1, b) tc.assertIs(board1, b) tc.assertBoardEqual(board1, DIAGRAM1) tc.assertEqual(plays1, [('b', (2, 3)), ('w', (3, 4)), ('b', None), ('w', None)]) tc.assertRaisesRegexp(ValueError, "board not empty", sgf_moves.get_setup_and_moves, g1, b) b2 = boards.Board(19) tc.assertRaisesRegexp(ValueError, "wrong board size, must be 9$", sgf_moves.get_setup_and_moves, g1, b2)
def test_result_from_unscored_game(tc): game1 = gameplay.Game(boards.Board(19)) game1.record_resignation_by('w') result = gameplay.Result.from_unscored_game(game1) tc.assertEqual(result.sgf_result, "B+R") game2 = gameplay.Game(boards.Board(19)) tc.assertRaisesRegexp(ValueError, "^game is not over$", gameplay.Result.from_unscored_game, game2) game3 = gameplay.Game(boards.Board(19)) game3.record_move('b', None) game3.record_move('w', None) tc.assertRaisesRegexp(ValueError, "^game is passed out$", gameplay.Result.from_unscored_game, game3)
def test_basics(tc): tc.assertRaises(ValueError, boards.Board, 1) tc.assertRaises(ValueError, boards.Board, 0) tc.assertRaises(ValueError, boards.Board, -1) tc.assertRaises((TypeError, ValueError), boards.Board, (19, 19)) b = boards.Board(9) tc.assertTrue(b.is_empty()) tc.assertItemsEqual(b.list_occupied_points(), []) tc.assertEqual(b.get(2, 3), None) b.play(2, 3, 'b') tc.assertEqual(b.get(2, 3), 'b') tc.assertFalse(b.is_empty()) b.play(3, 4, 'w') with tc.assertRaises(ValueError): b.play(3, 4, 'w') with tc.assertRaises(ValueError): b.play(5, 2, None) tc.assertItemsEqual(b.list_occupied_points(), [('b', (2, 3)), ('w', (3, 4))])
def interpret_diagram(diagram, size, board=None): """Set up the position from a diagram. diagram -- board representation as from render_board() size -- int Returns a Board. If the optional 'board' parameter is provided, it must be an empty board of the right size; the same object will be returned. """ if board is None: board = boards.Board(size) else: if board.side != size: raise ValueError("wrong board size, must be %d" % size) if not board.is_empty(): raise ValueError("board not empty") lines = diagram.split("\n") colours = {'#': 'b', 'o': 'w', '.': None} if size > 9: extra_offset = 1 else: extra_offset = 0 try: for (row, col) in board.board_points: colour = colours[lines[size - row - 1][3 * (col + 1) + extra_offset]] if colour is not None: board.play(row, col, colour) except Exception: raise ValueError return board
def reset_position(self): self.curnode = self.game.get_root() self.boards = {} self.varcache = {} board = boards.Board(self.game.size) board, instructions = apply_node_to_board(board, self.curnode) self.boards[self.curnode] = board return instructions
def test_attributes(tc): b = boards.Board(5) tc.assertEqual(b.side, 5) tc.assertEqual(b.board_points, [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4)])
def reset_position(self): self.curnode = self.game.get_root() self.boards = {} self.varcache = {} board = boards.Board(self.game.size) board, instructions = apply_node_to_board(board, self.curnode) self.boards[self.curnode] = board node_index = self.current_node_index() instructions.update({'nodeindex': node_index}) return instructions
def _make_game(self): board = boards.Board(self.board_size) if self.handicap_stones: board.apply_setup(self.handicap_stones, [], []) first_player = 'w' else: first_player = 'b' game = Game(board, first_player) game.set_move_limit(self.move_limit) game.set_game_over_callback(self.backend.end_game) return game
def get_setup_and_moves(sgf_game, board=None): """Return the initial setup and the following moves from an Sgf_game. Returns a pair (board, plays) board -- boards.Board plays -- list of pairs (colour, move) moves are (row, col), or None for a pass. The board represents the position described by AB and/or AW properties in the root node. The moves are from the game's 'leftmost' variation. Raises ValueError if this position isn't legal. Raises ValueError if there are any AB/AW/AE properties after the root node. Doesn't check whether the moves are legal. If the optional 'board' parameter is provided, it must be an empty board of the right size; the same object will be returned. """ size = sgf_game.get_size() if board is None: board = boards.Board(size) else: if board.side != size: raise ValueError("wrong board size, must be %d" % size) if not board.is_empty(): raise ValueError("board not empty") root = sgf_game.get_root() nodes = sgf_game.main_sequence_iter() ab, aw, ae = root.get_setup_stones() if ab or aw: is_legal = board.apply_setup(ab, aw, ae) if not is_legal: raise ValueError("setup position not legal") colour, raw = root.get_raw_move() if colour is not None: raise ValueError("mixed setup and moves in root node") nodes.next() moves = [] for node in nodes: if node.has_setup_stones(): raise ValueError("setup properties after the root node") colour, raw = node.get_raw_move() if colour is not None: moves.append((colour, sgf_properties.interpret_go_point(raw, size))) return board, moves
def main(): model = load_model("model.h5", custom_objects={'board_loss': board_loss}) board = boards.Board(19) moves = [] with open("play_st.sgf", "r") as fh: g = sgf.Sgf_game.from_string(fh.read()) mainseq = g.get_main_sequence() mainseq = mainseq[1:] for node in mainseq: if len(moves) >= 0: break if node.has_property("W"): move = node.get("W") board.play(move[0], move[1], 'w') elif node.has_property("B"): move = node.get("B") board.play(move[0], move[1], 'b') moves.append(move) prob_disp = [' ', ' ', '░', '░', '▒', '▒', '▓', '▓', '█', '█'] while True: print ascii_boards.render_board(board) if not SELFPLAY: m = raw_input("move: ") print m playerpos = common.move_from_vertex(m, 19) moves.append(playerpos) board.play(row=playerpos[0], col=playerpos[1], colour='b') else: if len(moves) > 200: break y = model.predict(np.array([board_to_nn(board, 'w', moves)])) zero_illegal(board, y, 'w') probs = np.reshape(normalized(y[0]), (19, 19)) print probs print sum(sum(probs)) for ax in range(19): r = "" for ay in range(19): r += str(prob_disp[int(probs[ax][ay] * 10)]) * 2 print r i = np.argmax(y[0]) moves.append((i % 19, i // 19)) comp_color = 'w' if SELFPLAY and len(moves) % 2 == 1: comp_color = 'b' board.play(row=i % 19, col=i // 19, colour=comp_color)
def test_copy(tc): b1 = boards.Board(9) b1.play(2, 3, 'b') b1.play(3, 4, 'w') b2 = b1.copy() tc.assertEqual(b1, b2) b2.play(5, 5, 'b') b2.play(2, 1, 'b') tc.assertNotEqual(b1, b2) b1.play(5, 5, 'b') b1.play(2, 1, 'b') tc.assertEqual(b1, b2)
def test_interpret_diagram(tc): b1 = boards.Board(9) b1.play(2, 3, 'b') b1.play(3, 4, 'w') b2 = ascii_boards.interpret_diagram(_9x9_expected, 9) tc.assertEqual(b1, b2) b3 = boards.Board(9) b4 = ascii_boards.interpret_diagram(_9x9_expected, 9, b3) tc.assertIs(b3, b4) tc.assertEqual(b1, b3) tc.assertRaisesRegexp(ValueError, "board not empty", ascii_boards.interpret_diagram, _9x9_expected, 9, b3) b5 = boards.Board(19) tc.assertRaisesRegexp(ValueError, "wrong board size, must be 9$", ascii_boards.interpret_diagram, _9x9_expected, 9, b5) tc.assertRaises(ValueError, ascii_boards.interpret_diagram, "nonsense", 9) b6 = ascii_boards.interpret_diagram(_13x13_expected, 13) tc.assertDiagramEqual(ascii_boards.render_board(b6), _13x13_expected) padded = "\n\n" + _9x9_expected + "\n\n" tc.assertEqual(b1, ascii_boards.interpret_diagram(padded, 9))
def runTest(self): def _interpret(moves): return [move_from_vertex(v, b.side) for v in moves] b = boards.Board(9) is_legal = b.apply_setup(_interpret(self.black_points), _interpret(self.white_points), _interpret(self.empty_points)) self.assertBoardEqual(b, self.diagram) if self.is_legal: self.assertTrue(is_legal, "setup should be considered legal") else: self.assertFalse(is_legal, "setup should be considered illegal")
def test_game_initial_board(tc): board = boards.Board(9) board.play(1, 2, 'b') board.play(5, 3, 'b') board.play(8, 8, 'b') fx = Game_fixture(tc, board=board, first_player='w') game = fx.game fx.check_not_over() tc.assertEqual(game.move_count, 0) tc.assertEqual(game.next_player, 'w') game.record_move('w', (2, 3)) fx.check_not_over() tc.assertEqual(game.move_count, 1) tc.assertIs(game.board, board) tc.assertBoardEqual(game.board, DIAGRAM2)
def runTest(self): b = boards.Board(9) ko_point = None for move in self.moves: colour, vertex = move.split() colour = colour.lower() row, col = move_from_vertex(vertex, b.side) ko_point = b.play(row, col, colour) self.assertBoardEqual(b, self.diagram) if ko_point is None: ko_vertex = None else: ko_vertex = format_vertex(ko_point) self.assertEqual(ko_vertex, self.ko_vertex, "wrong ko point") self.assertEqual(b.area_score(), self.score, "wrong score")
def __init__(self, game=None, gridsize=19): if game is None: game = sgf.Sgf_game(gridsize) print 'abstractboard initialised with size', game.size, gridsize self.game = game self.prisoners = [0, 0] self.variation_index = 0 self.boards = {} self.curnode = game.get_root() board = boards.Board(self.game.size) board, instructions = apply_node_to_board(board, self.curnode) self.boards[self.curnode] = board self.varcache = {} self.filepath = ''
def build_boards_to_node(self, node, replace=False): precursor_nodes = self.game.get_sequence_above(node) board = boards.Board(self.game.size) board, instructions = apply_node_to_board(board, precursor_nodes[0]) self.boards[precursor_nodes[0]] = board for i in range(1, len(precursor_nodes)): curnode = precursor_nodes[i] if (not self.boards.has_key(curnode)) or replace: board, instructions = apply_node_to_board(board, curnode) self.boards[curnode] = board else: board = self.boards[curnode] curnode = node board, instructions = apply_node_to_board(board, node) self.boards[node] = board
def parse_sgf(file): with open(file) as fh: g = sgf.Sgf_game.from_string(fh.read()) mainseq = g.get_main_sequence() mainseq = mainseq[1:] datax = [] datay = [] curr = boards.Board(19) moves = [] for node in mainseq: if node.has_property("W"): move = node.get("W") if move is None: break pos, move = make_case(curr, move, 'w', moves) datax.append(pos) datay.append(move) curr.play(move[0], move[1], 'w') elif node.has_property("B"): move = node.get("B") if move is None: break pos, move = make_case(curr, move, 'w', moves) datax.append(pos) datay.append(move) curr.play(move[0], move[1], 'b') else: break moves.append(move) zipped = zip(datax, datay) zipped = rnd.sample(zipped, 20) datax, datay = zip(*zipped) return (datax, datay)
def __init__(self, board_size, komi=0.0, move_limit=1000): self.players = {'b': 'b', 'w': 'w'} self.game_id = None self.controllers = {} self.claim_allowed = {'b': False, 'w': False} self.after_move_callback = None self.board_size = board_size self.komi = komi self.move_limit = move_limit self.allowed_scorers = [] self.internal_scorer = False self.handicap_compensation = "no" self.handicap = 0 self.first_player = "b" self.engine_names = {} self.engine_descriptions = {} self.moves = [] self.player_scores = {'b': None, 'w': None} self.additional_sgf_props = [] self.late_errors = [] self.handicap_stones = None self.result = None self.board = boards.Board(board_size) self.simple_ko_point = None
def __init__(self, tc, **kwargs): self.tc = tc kwargs.setdefault('board', boards.Board(9)) self.game = gameplay.Game(**kwargs)
def test_render_board_9x9(tc): b = boards.Board(9) b.play(2, 3, 'b') b.play(3, 4, 'w') tc.assertDiagramEqual(ascii_boards.render_board(b), _9x9_expected)
def test_render_board_13x13(tc): b = boards.Board(13) b.play(2, 3, 'b') b.play(3, 4, 'w') tc.assertDiagramEqual(ascii_boards.render_board(b), _13x13_expected)
def __init__(self): self.board = boards.Board(19) self.moves = [] self.policy = NNPolicy('model.h5')
def __init__(self, size=19): self.scoringboard = boards.Board(size) self.board = [[[] for j in range(size)] for i in range(size)] self.size = size
def test_apply_setup_range_checks(tc): b = boards.Board(9) tc.assertRaises(IndexError, b.apply_setup, [(1, 1), (9, 2)], [], []) tc.assertRaises(IndexError, b.apply_setup, [], [(2, 2), (2, -3)], []) tc.assertRaises(IndexError, b.apply_setup, [], [], [(3, 3), (-3, 2)]) tc.assertEqual(b, boards.Board(9))
def clear_board(self, cmd): self.board = boards.Board(19) return "="