def test_play_node(): board = Board() assert board.current_node_id is None board.play(coord2d(4, 4)) assert board.current_node_id == 0 assert board.current_node.parent_id is None assert board.current_node.children == [] assert board.current_node.action == NODE_BLACK assert board.current_node.move == coord2d(4, 4) board.play(coord2d(5, 5)) assert board.current_node.id == 1 assert board.current_node.parent_id == 0 assert board.current_node.children == [] assert board.tree[0].children == [1] assert board.current_node.action == NODE_WHITE assert board.current_node.move == coord2d(5, 5) board.play(coord2d(6, 6)) assert board.current_node_id == 2 assert board.current_node.parent_id == 1 assert board.current_node.children == [] assert board.tree[0].children == [1] assert board.tree[1].children == [2] assert board.current_node.action == NODE_BLACK assert board.current_node.move == coord2d(6, 6)
def test_game_to_sgf_part_handicap(): board = Board(19, 2) board.play(coord2d(10, 10, 19)) sgf = sgf_part_from_node(board, 0) assert (sgf == ';AB[dp][pd];W[jj]' or sgf == ';AB[pd][dp];W[jj]')
def test_current(): board = Board() assert board.current == BLACK board.play(1) assert board.current == WHITE board.play(2) assert board.current == BLACK
def test_both_passed(): board = Board() assert not board.both_passed board.play(PASS) assert not board.both_passed board.play(PASS) assert board.both_passed
def test_game_to_sgf_part_labels(): board = Board(19) board.play(coord2d(10, 10, 19)) board.current_node.toggle_label(180) board.current_node.toggle_label(181) sgf = sgf_part_from_node(board, 0) assert (sgf == ';B[jj]LB[jj:A][kj:B]' or sgf == ';B[jj]LB[kj:B][jj:A]')
def test_game_to_sgf_part_variations(): board = Board(19) board.play(coord2d(4, 4, 19)) board.play(coord2d(16, 17, 19)) board.current_node_id = 0 board.play(coord2d(17, 16, 19)) sgf = sgf_part_from_node(board, 0) assert sgf == ';B[dd](;W[pq])(;W[qp])'
def test_game_to_sgf_part_symbols(): board = Board(19) board.play(coord2d(10, 10, 19)) board.current_node.toggle_symbol(180, 'TR') board.current_node.toggle_symbol(181, 'TR') board.current_node.toggle_symbol(182, 'SQ') sgf = sgf_part_from_node(board, 0) assert (sgf == ';B[jj]TR[jj][kj]SQ[lj]' or sgf == ';B[jj]TR[kj][jj]SQ[lj]')
def test_edit_cycle(): board = Board(9) board.toggle_edit(10, BLACK) board.toggle_edit(11, WHITE) board.edit_cycle(10) board.edit_cycle(11) board.edit_cycle(12) assert board.at(10) == WHITE assert board.at(11) == EMPTY assert board.at(12) == BLACK
def test_current_edits(): board = Board() board.current = BLACK board.play(1) board.add_edits([3], [], []) assert board.current == WHITE board.play(2) board.add_edits([4], [], []) board.add_edits([], [5], []) assert board.current == BLACK
def test_toggle_edit(): board = Board(9) board.toggle_edit(10, BLACK) board.toggle_edit(11, WHITE) board.toggle_edit(12, WHITE) assert board.at(10) == BLACK assert board.at(11) == WHITE assert board.at(12) == WHITE board.toggle_edit(11, WHITE) assert board.at(11) == EMPTY
def _create_game(self, ranked, correspondence, black, white, handicap, komi, size, timing_system, capped, maintime, overtime, overtime_count, private): board = Board(size, handicap) room = Room(type='game') ru = RoomUser(room=room, user=black) ru2 = RoomUser(room=room, user=white) game = Game(room=room, is_demo=False, is_ranked=ranked, is_correspondence=correspondence, is_private=private, board=board, stage='playing', komi=komi, black_user=black, black_display=black.display, black_rating=black.rating, white_user=white, white_display=white.display, white_rating=white.rating) start_at = datetime.utcnow() + settings.GAME_START_DELAY if timing_system == 'fischer': overtime_count = 0 timing = Timing(game=game, system=timing_system, start_at=start_at, timing_updated_at=start_at, next_move_at=start_at, capped=capped, main=maintime, overtime=overtime, overtime_count=overtime_count, black_main=maintime, black_overtime=overtime * overtime_count, white_main=maintime, white_overtime=overtime * overtime_count) self.db.add(room) self.db.add(ru) self.db.add(ru2) self.db.add(game) self.db.add(timing) return game
def game_from_node(node): game = Game(is_demo=True) game.board = Board(int(node.prop_one('SZ', '19'))) game.komi = float(node.prop_one('KM', 6.5)) game.result = node.prop_one('RE') game.black_display = node.prop_one('PB') game.white_display = node.prop_one('PW') game.title = node.prop_one('EV') _replay_game(game.board, node) if game.board.tree: game.board.current_node_id = 0 return game
def test_play(): board = Board() assert board.current == BLACK board.play(coord2d(4, 4)) assert board.current == WHITE board.play(coord2d(3, 3)) assert board.current == BLACK board.play(coord2d(6, 6)) assert board.at(coord2d(4, 4)) == BLACK assert board.at(coord2d(3, 3)) == WHITE assert board.at(coord2d(6, 6)) == BLACK
def test_game_to_sgf_basic(): game = Game(board=Board(19), black_display='black', white_display='white', created_at=datetime(2016, 5, 7), result='B+1.5', komi=7.5) game.board.play(coord2d(4, 4, 19)) game.board.play(coord2d(16, 17, 19)) sgf = game_to_sgf(game) expected = '(;SO[weiqi.gs]FF[4]DT[2016-05-07]PW[white]PB[black]KM[7.5]SZ[19]RE[B+1.5];B[dd];W[pq])' assert sgf.replace('\n', '') == expected
def play_one_game(node, db, socket): game = GameFactory(board=Board(19)) svc_black = GameService(db, socket, game.black_user) svc_white = GameService(db, socket, game.white_user) black_turn = True while True: coord = node.prop_one('B') if black_turn else node.prop_one('W') coord = coord_from_sgf(coord, 19) svc = svc_black if black_turn else svc_white svc.execute('move', {'game_id': game.id, 'move': coord}) if not node.children: break node = node.children[0] black_turn = not black_turn
class DemoGameFactory(SQLAlchemyModelFactory): class Meta: model = Game sqlalchemy_session = session force_flush = True room = factory.SubFactory(RoomFactory) is_demo = True is_ranked = False stage = 'finished' board = factory.lazy_attribute(lambda o: Board(9)) komi = 7.5 result = '' demo_owner = factory.SubFactory(UserFactory) demo_owner_display = factory.lazy_attribute(lambda o: o.demo_owner.display) demo_owner_rating = factory.lazy_attribute(lambda o: o.demo_owner.rating) demo_control = factory.lazy_attribute(lambda o: o.demo_owner) demo_control_display = factory.lazy_attribute( lambda o: o.demo_control.display)
def create_demo(self, title, size): room = Room(type='game') self.db.add(room) game = Game(is_demo=True, is_ranked=False, room=room, title=title, board=Board(int(size)), komi=settings.DEFAULT_KOMI, stage='finished', demo_owner=self.user, demo_owner_display=self.user.display, demo_owner_rating=self.user.rating, demo_control=self.user, demo_control_display=self.user.display) self.db.add(game) self.db.commit() return game.id
class GameFactory(SQLAlchemyModelFactory): class Meta: model = Game sqlalchemy_session = session room = factory.SubFactory(RoomFactory) timing = factory.RelatedFactory('weiqi.test.factories.TimingFactory', 'game') is_demo = False is_ranked = True is_private = False stage = 'playing' board = factory.lazy_attribute(lambda o: Board(9)) komi = 7.5 result = '' black_user = factory.SubFactory(UserFactory) black_display = factory.lazy_attribute(lambda o: o.black_user.display) black_rating = factory.lazy_attribute(lambda o: o.black_user.rating) white_user = factory.SubFactory(UserFactory) white_display = factory.lazy_attribute(lambda o: o.white_user.display) white_rating = factory.lazy_attribute(lambda o: o.white_user.rating)
def test_pass(): board = Board() board.play(PASS) assert board.tree[0].move == PASS assert board.current == WHITE
def test_current_handicap(): board = Board(9, 2) assert board.current == WHITE board.play(1) assert board.current == BLACK
def test_place_hc(): tests = { 0: ('.........' '.........' '.........' '.........' '.........' '.........' '.........' '.........' '.........'), 1: ('.........' '.........' '.........' '.........' '.........' '.........' '.........' '.........' '.........'), 2: ('.........' '.........' '......x..' '.........' '.........' '.........' '..x......' '.........' '.........'), 3: ('.........' '.........' '......x..' '.........' '.........' '.........' '..x...x..' '.........' '.........'), 4: ('.........' '.........' '..x...x..' '.........' '.........' '.........' '..x...x..' '.........' '.........'), 5: ('.........' '.........' '..x...x..' '.........' '....x....' '.........' '..x...x..' '.........' '.........'), 6: ('.........' '.........' '..x...x..' '.........' '..x...x..' '.........' '..x...x..' '.........' '.........'), 7: ('.........' '.........' '..x...x..' '.........' '..x.x.x..' '.........' '..x...x..' '.........' '.........'), 8: ('.........' '.........' '..x.x.x..' '.........' '..x...x..' '.........' '..x.x.x..' '.........' '.........'), 9: ('.........' '.........' '..x.x.x..' '.........' '..x.x.x..' '.........' '..x.x.x..' '.........' '.........'), } for hc, pos in tests.items(): board = Board(9) board.place_handicap(hc) assert str(board) == str(board_from_string(pos))
def test_toggle_edit_remove(): board = Board(9) board.play(10) board.toggle_edit(10, BLACK) assert board.at(10) == EMPTY
def board(size=9): board = Board(size) [board.play(i) for i in range(size + 1)] return board