def test_get_setup_and_moves(tc): g1 = sgf.Sgf_game.from_string(SAMPLE_SGF) board1, plays1 = sgf_moves.get_setup_and_moves(g1) tc.assertDiagramEqual(ascii_boards.render_board(board1), DIAGRAM1) tc.assertEqual(plays1, [('b', (2, 3)), ('w', (3, 4)), ('b', None), ('w', None)]) g2 = sgf.Sgf_game(size=9) root = g2.get_root() root.set("AB", [(1, 2), (3, 4)]); node = g2.extend_main_sequence() node.set("B", (5, 6)) node = g2.extend_main_sequence() node.set("W", (5, 7)) board2, plays2 = sgf_moves.get_setup_and_moves(g2) tc.assertDiagramEqual(ascii_boards.render_board(board2), DIAGRAM2) tc.assertEqual(plays2, [('b', (5, 6)), ('w', (5, 7))]) g3 = sgf.Sgf_game.from_string("(;AB[ab][ba]AW[aa])") tc.assertRaisesRegexp(ValueError, "setup position not legal", sgf_moves.get_setup_and_moves, g3) g4 = sgf.Sgf_game.from_string("(;SZ[9];B[ab];AW[bc])") tc.assertRaisesRegexp(ValueError, "setup properties after the root node", sgf_moves.get_setup_and_moves, g4) g5 = sgf.Sgf_game.from_string("(;SZ[26];B[ab];W[bc])") board5, plays5 = sgf_moves.get_setup_and_moves(g5) tc.assertEqual(plays5, [('b', (24, 0)), ('w', (23, 1))])
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 compare_boards(b1, b2): """Check whether two boards have the same position. returns a pair (position_is_the_same, message) """ if b1.side != b2.side: raise ValueError("size is different: %s, %s" % (b1.side, b2.side)) differences = [] for row, col in b1.board_points: if b1.get(row, col) != b2.get(row, col): differences.append((row, col)) if not differences: return True, None msg = "boards differ at %s" % " ".join(map(format_vertex, differences)) try: msg += "\n%s\n%s" % ( ascii_boards.render_board(b1), ascii_boards.render_board(b2)) except Exception: pass return False, msg
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.assertDiagramEqual(ascii_boards.render_board(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 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.assertDiagramEqual(ascii_boards.render_board(b), self.diagram.rstrip()) if self.is_legal: self.assertTrue(is_legal, "setup should be considered legal") else: self.assertFalse(is_legal, "setup should be considered illegal")
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.assertDiagramEqual(ascii_boards.render_board(b), self.diagram.rstrip()) 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 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)
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 handle_showboard(self, args): return "\n%s\n" % ascii_boards.render_board(self.board)
def get_score(self): self.scoringboard.board = self.remove_dead() print ascii_boards.render_board(self.scoringboard) return self.scoringboard.area_score()
except ValueError, e: raise StandardError(str(e)) if move_number is not None: move_number = max(0, move_number-1) plays = plays[:move_number] for colour, move in plays: if move is None: continue row, col = move try: board.play(row, col, colour) except ValueError: raise StandardError("illegal move in sgf file") print ascii_boards.render_board(board) print _description = """\ Show the position from an SGF file. If a move number is specified, the position before that move is shown (this is to match the behaviour of GTP loadsgf). """ def main(argv): parser = OptionParser(usage="%prog <filename> [move number]", description=_description) opts, args = parser.parse_args(argv) if not args: parser.error("not enough arguments") pathname = args[0] if len(args) > 2:
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 print_board(colour, move, board, **kwargs): print(colour.upper(), format_vertex(move)) print(ascii_boards.render_board(board)) print()
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 coerce(board, diagram): try: return board, ascii_boards.interpret_diagram(diagram, board.side) except ValueError: return ascii_boards.render_board(board), diagram