def test_game_ko_violation(tc): # After these moves, b E5 is a ko violation ko_setup_moves = [ ('b', 'C5'), ('w', 'F5'), ('b', 'D6'), ('w', 'E4'), ('b', 'D4'), ('w', 'E6'), ('b', 'E5'), ('w', 'D5'), ] fx = Game_fixture(tc) fx.check_legal_moves(ko_setup_moves) fx.game.record_move('b', move_from_vertex('E5', 9)) fx.check_over('seen_forfeit') tc.assertEqual(fx.game.forfeit_reason, "attempted move to ko-forbidden point E5") tc.assertEqual(fx.game.move_count, 8) Game_fixture(tc).check_legal_moves(ko_setup_moves + [ ('b', 'A1'), ('w', 'E5'), ]) Game_fixture(tc).check_legal_moves(ko_setup_moves + [ ('b', 'pass'), ('w', 'E5'), ]) Game_fixture(tc).check_legal_moves(ko_setup_moves + [ ('b', 'A1'), ('w', 'A2'), ('b', 'E5'), ])
def response2move(response): """ handles frisbee rules. returns tuple (flag, move) where flag is True iff the move actually played is the same that player wanted. Move is either 'skip', 'pass', or tuple (row, col). """ # normal pass if response.lower() == 'pass': return True, 'pass' # when we do not allow throwing to invalid moves # and player does anyway, this is counted as skip move = move_from_vertex(response, b.side) if not args.allow_invalid and not is_move_valid(move, player.color): logging.warn("Invalid move played by bot, even though" " --allow-invalid-moves was not specified!") return False, 'skip' # landed on invalid position (involuntary pass) move_rnd = randomize_move(move) if not is_move_valid(move_rnd, player.color): return False, 'skip' return move == move_rnd, move_rnd
def test_game_move_limit(tc): fx = Game_fixture(tc) game = fx.game game.set_move_limit(5) fx.check_legal_moves([ ('b', 'C5'), ('w', 'F5'), ('b', 'D6'), ('w', 'E4'), ]) game.record_move('b', move_from_vertex('C3', 9)) fx.check_over('hit_move_limit')
def notify_move(self, colour, move): if (self.reject_vertex is not None and move == move_from_vertex(self.reject_vertex, self._size)): if self.reject_as_error: self.log.append("notify_move -> %s [error]" % colour) return 'error', "programmed error" else: self.log.append("notify_move -> %s [rejecting]" % colour) return 'reject', "programmed reject" self.log.append("notify_move -> %s %s" % (colour, format_vertex(move))) return 'accept', None
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 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 check_legal_moves(self, moves): for colour, vertex in moves: self.game.record_move(colour, move_from_vertex(vertex, 9)) self.check_not_over()
def get_free_handicap(self, handicap): self.log.append("get_free_handicap: %r" % (handicap,)) return [move_from_vertex(s, self._size) for s in "e4 e2 c2".split()]
def _action_for_vertex(self, vertex): if vertex in ('resign', 'claim'): return vertex, None if vertex == 'forfeit': return 'forfeit', "programmed forfeit" return 'move', move_from_vertex(vertex, self._size)
def get_free_handicap(self, handicap): self.log.append("get_free_handicap: %r" % (handicap, )) return [ move_from_vertex(s, self._size) for s in "e4 e2 c2".split() ]
def play(self, cmd): x, y = common.move_from_vertex(cmd[2], 19) self.board.play(row=x, col=y, colour=cmd[1].lower()) self.moves.append((x, y)) return "="
def _interpret(moves): return [move_from_vertex(v, b.side) for v in moves]