def test_is_playable(): board = Board() assert board.is_playable(0, 0) == False assert board.is_playable(4, 2) == True assert board.is_playable(5, 3) == True assert board.is_playable(3, 3) == False assert board.is_playable(4, 5) == False
def test_is_valid_position(): board = Board() is_valid = board.is_valid_position(0, 0) assert is_valid == True is_valid = board.is_valid_position(8, 0) assert is_valid == False
def test_play(): board = Board() board.play(4, 2) squares = board.squares print(board) assert squares[4][2] == squares[3][3] == squares[4][3] == squares[4][ 4] == Square.BLACK assert squares[3][4] == Square.WHITE
def test_init(self): # none patterns = [m(), M] for pattern in patterns: self.assertIsNone(pattern._Move__x) self.assertIsNone(pattern._Move__y) self.assertEqual(pattern._Move__str, '') self.assertEqual(pattern._Move__case, LOWER) # x, y, str patterns = [m(0, 1), m('a2')] for pattern in patterns: self.assertEqual(pattern._Move__x, 0) self.assertEqual(pattern._Move__y, 1) self.assertEqual(pattern._Move__str, 'a2') # case move = m(case='upper') self.assertEqual(move._Move__case, 'upper') # put_disc board = Board() board.put_disc('black', *m('f5')) self.assertEqual(board.get_bitboard_info(), (34829500416, 68719476736)) board.put_disc('white', *m('d6')) self.assertEqual(board.get_bitboard_info(), (34561064960, 68988960768))
def test_table_force_import_error(self): import os import importlib import reversi # ------------------------------- # switch environ and reload module os.environ['FORCE_TABLEMETHODS_IMPORT_ERROR'] = 'RAISE' importlib.reload(reversi.strategies.TableMethods) self.assertTrue(reversi.strategies.TableMethods.SLOW_MODE) # ------------------------------- # size8 table = Table(8) board = Board(8) board.put_disc('black', 3, 2) board.put_disc('white', 2, 2) board.put_disc('black', 2, 3) board.put_disc('white', 4, 2) board.put_disc('black', 1, 1) board.put_disc('white', 0, 0) self.assertEqual(table.get_score(board), -22) # ------------------------------- # recover environment and reload module del os.environ['FORCE_TABLEMETHODS_IMPORT_ERROR'] importlib.reload(reversi.strategies.TableMethods) self.assertFalse(reversi.strategies.TableMethods.SLOW_MODE)
def test_switch_next_move(self): class Put_5_4(AbstractStrategy): def next_move(self, color, board): return (5, 4) class Put_5_5(AbstractStrategy): def next_move(self, color, board): return (5, 5) class Put_4_5(AbstractStrategy): def next_move(self, color, board): return (4, 5) switch = Switch( turns=[0, 1, 2], strategies=[Put_5_4(), Put_5_5(), Put_4_5()], ) board = Board() move = switch.next_move('black', board) self.assertEqual(move, (5, 4)) board.put_disc('black', *move) move = switch.next_move('white', board) self.assertEqual(move, (5, 5)) board.put_disc('white', *move) move = switch.next_move('black', board) self.assertEqual(move, (4, 5)) board.put_disc('black', *move)
def test_randomopening_next_move(self): class TestStrategy: def next_move(self, color, board): return (0, 0) board = Board() randomopening = RandomOpening(depth=4, base=TestStrategy()) # depth = 1 - 4 for _ in range(2): for color in ('black', 'white'): move = randomopening.next_move(color, board) self.assertNotEqual(move, (0, 0)) board.put_disc(color, *move) # depth = 5 move = randomopening.next_move('black', board) self.assertEqual(move, (0, 0))
def test_slowstarter(self): slowstarter = SlowStarter() board = Board() board.put_disc('black', 5, 4) # 5 board.put_disc('white', 3, 5) # 6 board.put_disc('black', 2, 4) # 7 board.put_disc('white', 5, 3) # 8 board.put_disc('black', 3, 6) # 9 # unselfish self.assertTrue( slowstarter.next_move('white', board) in [(6, 4), (1, 5), ( 2, 5), (5, 5), (6, 5), (2, 6)]) board.put_disc('white', 6, 4) # 10 # greedy self.assertTrue(slowstarter.next_move('black', board) in [(7, 4)])
def test_number_scorer(self): board = Board() board.put_disc('black', 3, 2) board.put_disc('white', 2, 2) board.put_disc('black', 2, 3) board.put_disc('white', 4, 2) board.put_disc('black', 1, 1) board.put_disc('white', 0, 0) scorer = coord.NumberScorer() self.assertEqual(scorer.get_score(None, board, None, None), -6)
def test_opening_scorer(self): board = Board() board.put_disc('black', 3, 2) board.put_disc('white', 2, 2) board.put_disc('black', 2, 3) board.put_disc('white', 4, 2) board.put_disc('black', 1, 1) board.put_disc('white', 0, 0) scorer = coord.OpeningScorer() self.assertEqual(scorer.get_score(None, board, None, None), -8.25)
def test_winlose_scorer(self): board = Board() board.put_disc('black', 3, 2) board.put_disc('white', 2, 2) board.put_disc('black', 2, 3) board.put_disc('white', 4, 2) board.put_disc('black', 1, 1) board.put_disc('white', 0, 0) scorer = coord.WinLoseScorer() self.assertEqual(scorer.get_score(None, board, None, None), -10006) possibility_b = board.get_legal_moves('black') possibility_w = board.get_legal_moves('white') self.assertEqual( scorer.get_score(None, board, possibility_b, possibility_w), None)
def test_init(): board = Board() squares = board.squares for r in range(8): for c in range(8): s = Square.EMPTY if (r == 3 and c == 3) or (r == 4 and c == 4): s = Square.BLACK elif (r == 4 and c == 3) or (r == 3 and c == 4): s = Square.WHITE assert squares[r][c] == s
def test_4x4(self): board = Board(size=4) mask_bb = board.mask_bb self.assertEqual(0x6666, mask_bb.H) self.assertEqual(0x0FF0, mask_bb.V) self.assertEqual(0x0660, mask_bb.D) self.assertEqual(0xFFF0, mask_bb.U) self.assertEqual(0x7770, mask_bb.UR) self.assertEqual(0x7777, mask_bb.R) self.assertEqual(0x0777, mask_bb.BR) self.assertEqual(0x0FFF, mask_bb.B) self.assertEqual(0x0EEE, mask_bb.BL) self.assertEqual(0xEEEE, mask_bb.L) self.assertEqual(0xEEE0, mask_bb.UL)
def test_8x8(self): board = Board(size=8) mask_bb = board.mask_bb self.assertEqual(0x7E7E7E7E7E7E7E7E, mask_bb.H) self.assertEqual(0x00FFFFFFFFFFFF00, mask_bb.V) self.assertEqual(0x007E7E7E7E7E7E00, mask_bb.D) self.assertEqual(0xFFFFFFFFFFFFFF00, mask_bb.U) self.assertEqual(0x7F7F7F7F7F7F7F00, mask_bb.UR) self.assertEqual(0x7F7F7F7F7F7F7F7F, mask_bb.R) self.assertEqual(0x007F7F7F7F7F7F7F, mask_bb.BR) self.assertEqual(0x00FFFFFFFFFFFFFF, mask_bb.B) self.assertEqual(0x00FEFEFEFEFEFEFE, mask_bb.BL) self.assertEqual(0xFEFEFEFEFEFEFEFE, mask_bb.L) self.assertEqual(0xFEFEFEFEFEFEFE00, mask_bb.UL)
def test_none_display(self): board8x8 = Board() class Test(): def next_move(self, disc, board): return (3, 2) black_player = Player('black', 'Test', Test()) white_player = Player('white', 'User', ConsoleUserInput()) display = NoneDisplay() with captured_stdout() as stdout: display.progress(board8x8, black_player, white_player) legal_moves = board8x8.get_legal_moves('black') display.turn(black_player, legal_moves) black_player.put_disc(board8x8) display.move(black_player, legal_moves) display.progress(board8x8, black_player, white_player) display.foul(black_player) display.win(black_player) display.draw() lines = stdout.getvalue().splitlines() self.assertEqual(lines, [])
def test_table8_score(self): table = Table(8) board = Board(8) board.put_disc('black', 3, 2) board.put_disc('white', 2, 2) board.put_disc('black', 2, 3) board.put_disc('white', 4, 2) board.put_disc('black', 1, 1) board.put_disc('white', 0, 0) self.assertEqual(table.get_score(board), -22) self.assertEqual(table.next_move('black', board), (5, 2)) self.assertEqual(table.next_move('white', board), (2, 5)) table.next_move('black', Board(4)) init = [ [50, -20, -20, 50], # noqa: E201 [-20, -1, -1, -20], [-20, -1, -1, -20], [50, -20, -20, 50], # noqa: E201 ] self.assertEqual(table.table, init)
def test_consoleuserinput_next_move(self): board = Board() console_user_input = ConsoleUserInput() move = None with captured_stdin() as stdin: stdin.write('1') stdin.seek(0) move = console_user_input.next_move('black', board) self.assertEqual(move, (3, 2)) move = None with captured_stdin() as stdin: stdin.write('2') stdin.seek(0) move = console_user_input.next_move('black', board) self.assertEqual(move, (2, 3))
def test_to_xy(self): patterns = [ ('a1', (0, 0)), ('A1', (0, 0)), # upper case is also ok. ('a2', (0, 1)), ('a8', (0, 7)), ('b1', (1, 0)), ('b2', (1, 1)), ('h1', (7, 0)), ('h8', (7, 7)), ('z1', (25, 0)), ('a26', (0, 25)), ('z26', (25, 25)), ] for pattern, expected in patterns: self.assertEqual(M.to_xy(pattern), expected) board = Board() board.put_disc('black', *M.to_xy('f5')) self.assertEqual(board.get_bitboard_info(), (34829500416, 68719476736)) board.put_disc('white', *M.to_xy('d6')) self.assertEqual(board.get_bitboard_info(), (34561064960, 68988960768))
def __init__(self): class Board: def __init__(self): class Event: def __init__(self): self.is_set = lambda: True def clear(self): print('clear') self.move = (3, 2) self.event = Event() def selectable_moves(self, moves): print('selectable_moves', moves) def unselectable_moves(self, moves): print('unselectable_moves', moves) self.board = Board() class Menu: def __init__(self): class Event: def __init__(self): self.count = 0 def is_set(self): self.count += 1 print('is_set', self.count) if self.count >= 2: return True return False self.event = Event() self.menu = Menu()
def test_console_display(self): board8x8 = Board() class Test(): def next_move(self, disc, board): return (3, 2) black_player = Player('black', 'Test', Test()) white_player = Player('white', 'User', ConsoleUserInput()) display = ConsoleDisplay() self.assertEqual(display.sleep_time_turn, 1) self.assertEqual(display.sleep_time_move, 1) display = ConsoleDisplay(sleep_time_turn=0.001, sleep_time_move=0.001) self.assertEqual(display.sleep_time_turn, 0.001) self.assertEqual(display.sleep_time_move, 0.001) with captured_stdout() as stdout: display.progress(board8x8, black_player, white_player) legal_moves = board8x8.get_legal_moves('black') display.turn(black_player, legal_moves) black_player.put_disc(board8x8) display.move(black_player, legal_moves) display.progress(board8x8, black_player, white_player) display.foul(black_player) display.win(black_player) display.draw() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], "〇Test:2 ●User:2") self.assertEqual(lines[1], " a b c d e f g h") self.assertEqual(lines[2], " 1□□□□□□□□") self.assertEqual(lines[3], " 2□□□□□□□□") self.assertEqual(lines[4], " 3□□□□□□□□") self.assertEqual(lines[5], " 4□□□●〇□□□") self.assertEqual(lines[6], " 5□□□〇●□□□") self.assertEqual(lines[7], " 6□□□□□□□□") self.assertEqual(lines[8], " 7□□□□□□□□") self.assertEqual(lines[9], " 8□□□□□□□□") self.assertEqual(lines[10], "") self.assertEqual(lines[11], "〇Test's turn") self.assertEqual(lines[12], " 1: ('d', '3')") self.assertEqual(lines[13], " 2: ('c', '4')") self.assertEqual(lines[14], " 3: ('f', '5')") self.assertEqual(lines[15], " 4: ('e', '6')") self.assertEqual(lines[16], "putted on ('d', '3')") self.assertEqual(lines[17], "") self.assertEqual(lines[18], "〇Test:4 ●User:1") self.assertEqual(lines[19], " a b c d e f g h") self.assertEqual(lines[20], " 1□□□□□□□□") self.assertEqual(lines[21], " 2□□□□□□□□") self.assertEqual(lines[22], " 3□□□〇□□□□") self.assertEqual(lines[23], " 4□□□〇〇□□□") self.assertEqual(lines[24], " 5□□□〇●□□□") self.assertEqual(lines[25], " 6□□□□□□□□") self.assertEqual(lines[26], " 7□□□□□□□□") self.assertEqual(lines[27], " 8□□□□□□□□") self.assertEqual(lines[28], "") self.assertEqual(lines[29], "〇Test foul") self.assertEqual(lines[30], "〇Test win") self.assertEqual(lines[31], "draw")
def test_window_display(self): class TestInfo: def set_text(self, color, text1, text2): print('set_text', color, text1, text2) def set_turn_text_on(self, color): print('set_turn_text_on', color) def set_turn_text_off(self, color): print('set_turn_text_off', color) def set_move_text_on(self, color, x, y): print('set_move_text_on', color, x, y) def set_move_text_off(self, color): print('set_move_text_off', color) def set_foul_text_on(self, player): print('set_foul_text_on', player) def set_win_text_on(self, player): print('set_win_text_on', player) def set_lose_text_on(self, player): print('set_lose_text_on', player) def set_draw_text_on(self, player): print('set_draw_text_on', player) class TestBoard: def enable_move(self, x, y): print('enable_move', x, y) def disable_move(self, x, y): print('disable_move', x, y) def enable_moves(self, legal_moves): print('enable_moves', legal_moves) def disable_moves(self, legal_moves): print('disable_moves', legal_moves) def put_disc(self, color, x, y): print('put_disc', color, x, y) def turn_disc(self, color, captures): print('turn_disc', color, captures) class TestWindow: info = TestInfo() board = TestBoard() display = WindowDisplay(TestWindow()) # init self.assertIsInstance(display.info, TestInfo) self.assertIsInstance(display.board, TestBoard) self.assertEqual(display.sleep_time_turn, 0.3) self.assertEqual(display.sleep_time_move, 0.3) self.assertIsNone(display.pre_move) display = WindowDisplay(TestWindow(), sleep_time_turn=0.001, sleep_time_move=0.001) self.assertEqual(display.sleep_time_turn, 0.001) self.assertEqual(display.sleep_time_move, 0.001) # progress with captured_stdout() as stdout: display.progress(Board(), None, None) lines = stdout.getvalue().splitlines() self.assertEqual(lines, ['set_text black score 2', 'set_text white score 2']) # turn with captured_stdout() as stdout: display.turn(Player('black', 'TestPlayer', None), Board().get_legal_moves('black')) lines = stdout.getvalue().splitlines() self.assertEqual(lines, ['set_turn_text_on black', 'enable_moves [(3, 2), (2, 3), (5, 4), (4, 5)]']) # move player = Player('black', 'TestPlayer', None) player.move = (5, 4) player.captures = [(4, 4)] legal_moves = Board().get_legal_moves('black') display.pre_move = (0, 0) with captured_stdout() as stdout: display.move(player, legal_moves) lines = stdout.getvalue().splitlines() self.assertEqual(lines, [ 'set_turn_text_off black', 'set_move_text_off black', 'set_turn_text_off white', 'set_move_text_off white', 'disable_moves [(3, 2), (2, 3), (5, 4), (4, 5)]', 'disable_move 0 0', 'enable_move 5 4', 'put_disc black 5 4', 'set_move_text_on black f 5', 'turn_disc black [(4, 4)]', ]) # foul with captured_stdout() as stdout: display.foul(Player('black', 'TestPlayer', None)) lines = stdout.getvalue().splitlines() self.assertEqual(lines, ['set_foul_text_on black']) # win with captured_stdout() as stdout: display.win(Player('black', 'TestPlayer', None)) lines = stdout.getvalue().splitlines() self.assertEqual(lines, ['set_win_text_on black', 'set_lose_text_on white']) with captured_stdout() as stdout: display.win(Player('white', 'TestPlayer', None)) lines = stdout.getvalue().splitlines() self.assertEqual(lines, ['set_win_text_on white', 'set_lose_text_on black']) # draw with captured_stdout() as stdout: display.draw() lines = stdout.getvalue().splitlines() self.assertEqual(lines, ['set_draw_text_on black', 'set_draw_text_on white'])
def test_windowuserinput_next_move(self): class TestWindow: def __init__(self): class Board: def __init__(self): class Event: def __init__(self): self.is_set = lambda: True def clear(self): print('clear') self.move = (3, 2) self.event = Event() def selectable_moves(self, moves): print('selectable_moves', moves) def unselectable_moves(self, moves): print('unselectable_moves', moves) self.board = Board() class Menu: def __init__(self): class Event: def __init__(self): self.count = 0 def is_set(self): self.count += 1 print('is_set', self.count) if self.count >= 2: return True return False self.event = Event() self.menu = Menu() # window.board.event.is_set True with captured_stdout() as stdout: window_user_input = WindowUserInput(TestWindow()) move = window_user_input.next_move('black', Board()) lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'selectable_moves [(3, 2), (2, 3), (5, 4), (4, 5)]') self.assertEqual(lines[1], 'is_set 1') self.assertEqual(lines[2], 'clear') self.assertEqual( lines[3], 'unselectable_moves [(3, 2), (2, 3), (5, 4), (4, 5)]') with self.assertRaises(IndexError): print(lines[4]) self.assertEqual(move, (3, 2)) # window.board.event.is_set False, and window.menu.event.is_set True(at second time) with captured_stdout() as stdout: window_user_input = WindowUserInput(TestWindow()) window_user_input.window.board.event.is_set = lambda: False move = window_user_input.next_move('black', Board()) lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'selectable_moves [(3, 2), (2, 3), (5, 4), (4, 5)]') self.assertEqual(lines[1], 'is_set 1') self.assertEqual(lines[2], 'is_set 2') with self.assertRaises(IndexError): print(lines[3]) self.assertEqual(move, (3, 3))
def test_unselfish(self): unselfish = Unselfish() board = Board() board.put_disc('black', 5, 4) board.put_disc('white', 3, 5) board.put_disc('black', 2, 4) board.put_disc('white', 5, 3) board.put_disc('black', 3, 6) board.put_disc('white', 6, 4) board.put_disc('black', 3, 2) board.put_disc('white', 2, 6) board.put_disc('black', 6, 3) board.put_disc('white', 3, 7) self.assertTrue( unselfish.next_move('black', board) in [(7, 5), (4, 6)])
def test_greedy(self): greedy = Greedy() board = Board() board.put_disc('black', 5, 4) board.put_disc('white', 3, 5) board.put_disc('black', 2, 4) board.put_disc('white', 5, 3) board.put_disc('black', 3, 6) board.put_disc('white', 6, 4) board.put_disc('black', 3, 2) board.put_disc('white', 2, 6) board.put_disc('black', 6, 3) board.put_disc('white', 3, 7) self.assertTrue(greedy.next_move('black', board) in [(7, 4), (1, 7)])
def test_random(self): random = Random() board = Board() legal_moves = board.get_legal_moves('black') self.assertTrue(random.next_move('black', board) in legal_moves)
def test_possibility_scorer(self): board = Board() scorer = coord.PossibilityScorer() # initial value self.assertEqual(scorer._W, 5) possibility_b = board.get_bit_count( board.get_legal_moves_bits('black')) possibility_w = board.get_bit_count( board.get_legal_moves_bits('white')) self.assertEqual( scorer.get_score(None, None, possibility_b, possibility_w), 0) board.put_disc('black', 5, 4) possibility_b = board.get_bit_count( board.get_legal_moves_bits('black')) possibility_w = board.get_bit_count( board.get_legal_moves_bits('white')) self.assertEqual( scorer.get_score(None, None, possibility_b, possibility_w), 0) # check board = Board(8) board.put_disc('black', 3, 2) board.put_disc('white', 2, 2) board.put_disc('black', 2, 3) board.put_disc('white', 4, 2) board.put_disc('black', 1, 1) board.put_disc('white', 0, 0) possibility_b = board.get_bit_count( board.get_legal_moves_bits('black')) possibility_w = board.get_bit_count( board.get_legal_moves_bits('white')) self.assertEqual( scorer.get_score(None, None, possibility_b, possibility_w), 5)
def test_score(): board = Board() assert board.white_score == 2 assert board.black_score == 2
import sys import random sys.path.append("../") from reversi.board import Board # NOQA size = 8 board = Board(size) done = False pass_num = 0 while not done: print("=" * 80) print(board) board_info = board.get_board_info() legal_moves = board.get_legal_moves() print("board_info: ", board_info) print("legal_moves: ", legal_moves) if len(legal_moves) == 0: print("action: Pass") board.pass_move() pass_num += 1 else: action = random.choice(legal_moves) print("action: ", action) reversibles = board.get_flippable_discs(action) print("reversibles: ", reversibles) board.move(action) pass_num = 0
def test_game(self): class TestDisplay: def progress(self, board, black_player, white_player): print('display.progress', '\n' + str(board), black_player, white_player) def turn(self, player, legal_moves): print('display.turn', player, legal_moves) def move(self, player, legal_moves): print('display.move', player.move, legal_moves) def foul(self, player): print('display.foul', player) def win(self, player): print('display.win', player) def draw(self): print('display.draw') class TopLeft(AbstractStrategy): def next_move(self, color, board): return board.get_legal_moves(color)[0] class BottomRight(AbstractStrategy): def next_move(self, color, board): return board.get_legal_moves(color)[-1] class Foul(AbstractStrategy): def next_move(self, color, board): return (board.size // 2, board.size // 2) class TestCancel: class TestEvent: def is_set(self): print('cancel-event is set') return True event = TestEvent() p1 = Player('black', 'TopLeft', TopLeft()) p2 = Player('white', 'BottomRight', BottomRight()) p3 = Player('white', 'TopLeft', TopLeft()) p4 = Player('black', 'Foul', Foul()) p5 = Player('white', 'Foul', Foul()) # init game1 = Game(p1, p2, Board(4), TestDisplay()) self.assertIsInstance(game1.board, Board) self.assertIsInstance(game1.black_player.strategy, TopLeft) self.assertIsInstance(game1.white_player.strategy, BottomRight) self.assertEqual(game1.black_player, game1.players[0]) self.assertEqual(game1.white_player, game1.players[1]) self.assertEqual(game1.cancel, None) self.assertEqual(game1.result, []) game2 = Game(p1, p3, Board(4), TestDisplay()) self.assertIsInstance(game2.board, Board) self.assertIsInstance(game2.black_player.strategy, TopLeft) self.assertIsInstance(game2.white_player.strategy, TopLeft) self.assertEqual(game2.black_player, game2.players[0]) self.assertEqual(game2.white_player, game2.players[1]) self.assertEqual(game2.cancel, None) self.assertEqual(game2.result, []) game3 = Game(p1, p2, Board(4), TestDisplay(), 'white', TestCancel()) self.assertIsInstance(game3.board, Board) self.assertIsInstance(game3.black_player.strategy, TopLeft) self.assertIsInstance(game3.white_player.strategy, BottomRight) self.assertEqual(game3.black_player, game3.players[1]) self.assertEqual(game3.white_player, game3.players[0]) self.assertTrue(game3.cancel, TestCancel) self.assertEqual(game3.result, []) game4 = Game(p4, p2, Board(4), TestDisplay()) self.assertIsInstance(game4.board, Board) self.assertIsInstance(game4.black_player.strategy, Foul) self.assertIsInstance(game4.white_player.strategy, BottomRight) self.assertEqual(game4.black_player, game4.players[0]) self.assertEqual(game4.white_player, game4.players[1]) self.assertEqual(game4.cancel, None) self.assertEqual(game4.result, []) game5 = Game(p1, p5, Board(4), TestDisplay()) self.assertIsInstance(game5.board, Board) self.assertIsInstance(game5.black_player.strategy, TopLeft) self.assertIsInstance(game5.white_player.strategy, Foul) self.assertEqual(game5.black_player, game5.players[0]) self.assertEqual(game5.white_player, game5.players[1]) self.assertEqual(game5.cancel, None) self.assertEqual(game5.result, []) # play-black-win with captured_stdout() as stdout: game1.play() lines = stdout.getvalue().splitlines() output_ret = [ 'display.progress ', ' a b c d', ' 1□□□□', ' 2□●〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●BottomRight', 'display.turn 〇TopLeft [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.move (1, 0) [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.progress ', ' a b c d', ' 1□〇□□', ' 2□〇〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●BottomRight', 'display.turn ●BottomRight [(0, 0), (2, 0), (0, 2)]', 'display.move (0, 2) [(0, 0), (2, 0), (0, 2)]', 'display.progress ', ' a b c d', ' 1□〇□□', ' 2□〇〇□', ' 3●●●□', ' 4□□□□', ' 〇TopLeft ●BottomRight', 'display.turn 〇TopLeft [(0, 3), (1, 3), (2, 3), (3, 3)]', 'display.move (0, 3) [(0, 3), (1, 3), (2, 3), (3, 3)]', 'display.progress ', ' a b c d', ' 1□〇□□', ' 2□〇〇□', ' 3●〇●□', ' 4〇□□□', ' 〇TopLeft ●BottomRight', 'display.turn ●BottomRight [(0, 0), (2, 0)]', 'display.move (2, 0) [(0, 0), (2, 0)]', 'display.progress ', ' a b c d', ' 1□〇●□', ' 2□●●□', ' 3●〇●□', ' 4〇□□□', ' 〇TopLeft ●BottomRight', 'display.turn 〇TopLeft [(3, 0), (0, 1), (3, 2)]', 'display.move (3, 0) [(3, 0), (0, 1), (3, 2)]', 'display.progress ', ' a b c d', ' 1□〇〇〇', ' 2□●〇□', ' 3●〇●□', ' 4〇□□□', ' 〇TopLeft ●BottomRight', 'display.turn ●BottomRight [(3, 1), (1, 3)]', 'display.move (1, 3) [(3, 1), (1, 3)]', 'display.progress ', ' a b c d', ' 1□〇〇〇', ' 2□●〇□', ' 3●●●□', ' 4〇●□□', ' 〇TopLeft ●BottomRight', 'display.turn 〇TopLeft [(0, 1), (2, 3)]', 'display.move (0, 1) [(0, 1), (2, 3)]', 'display.progress ', ' a b c d', ' 1□〇〇〇', ' 2〇〇〇□', ' 3〇●●□', ' 4〇●□□', ' 〇TopLeft ●BottomRight', 'display.turn ●BottomRight [(0, 0)]', 'display.move (0, 0) [(0, 0)]', 'display.progress ', ' a b c d', ' 1●〇〇〇', ' 2〇●〇□', ' 3〇●●□', ' 4〇●□□', ' 〇TopLeft ●BottomRight', 'display.turn 〇TopLeft [(3, 2), (2, 3)]', 'display.move (3, 2) [(3, 2), (2, 3)]', 'display.progress ', ' a b c d', ' 1●〇〇〇', ' 2〇●〇□', ' 3〇〇〇〇', ' 4〇●□□', ' 〇TopLeft ●BottomRight', 'display.turn ●BottomRight [(3, 1), (3, 3)]', 'display.move (3, 3) [(3, 1), (3, 3)]', 'display.progress ', ' a b c d', ' 1●〇〇〇', ' 2〇●〇□', ' 3〇〇●〇', ' 4〇●□●', ' 〇TopLeft ●BottomRight', 'display.turn 〇TopLeft [(2, 3)]', 'display.move (2, 3) [(2, 3)]', 'display.progress ', ' a b c d', ' 1●〇〇〇', ' 2〇●〇□', ' 3〇〇〇〇', ' 4〇〇〇●', ' 〇TopLeft ●BottomRight', 'display.turn ●BottomRight [(3, 1)]', 'display.move (3, 1) [(3, 1)]', 'display.progress ', ' a b c d', ' 1●〇〇〇', ' 2〇●●●', ' 3〇〇〇●', ' 4〇〇〇●', ' 〇TopLeft ●BottomRight', 'display.win 〇TopLeft', ] self.assertEqual(lines, output_ret) self.assertEqual(game1.result.winlose, Game.BLACK_WIN) self.assertEqual(game1.result.black_name, 'TopLeft') self.assertEqual(game1.result.white_name, 'BottomRight') self.assertEqual(game1.result.black_num, 10) self.assertEqual(game1.result.white_num, 6) # play-white-win with captured_stdout() as stdout: game2.play() lines = stdout.getvalue().splitlines() output_ret = [ 'display.progress ', ' a b c d', ' 1□□□□', ' 2□●〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●TopLeft', 'display.turn 〇TopLeft [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.move (1, 0) [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.progress ', ' a b c d', ' 1□〇□□', ' 2□〇〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●TopLeft', 'display.turn ●TopLeft [(0, 0), (2, 0), (0, 2)]', 'display.move (0, 0) [(0, 0), (2, 0), (0, 2)]', 'display.progress ', ' a b c d', ' 1●〇□□', ' 2□●〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●TopLeft', 'display.turn 〇TopLeft [(0, 1), (3, 2), (2, 3)]', 'display.move (0, 1) [(0, 1), (3, 2), (2, 3)]', 'display.progress ', ' a b c d', ' 1●〇□□', ' 2〇〇〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●TopLeft', 'display.turn ●TopLeft [(2, 0), (0, 2)]', 'display.move (2, 0) [(2, 0), (0, 2)]', 'display.progress ', ' a b c d', ' 1●●●□', ' 2〇〇●□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●TopLeft', 'display.turn 〇TopLeft [(3, 0), (3, 1), (3, 2), (3, 3)]', 'display.move (3, 0) [(3, 0), (3, 1), (3, 2), (3, 3)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2〇〇〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●TopLeft', 'display.turn ●TopLeft [(0, 2), (3, 2), (1, 3)]', 'display.move (0, 2) [(0, 2), (3, 2), (1, 3)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2●●〇□', ' 3●●●□', ' 4□□□□', ' 〇TopLeft ●TopLeft', 'display.turn 〇TopLeft [(0, 3), (2, 3)]', 'display.move (0, 3) [(0, 3), (2, 3)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2●●〇□', ' 3●〇●□', ' 4〇□□□', ' 〇TopLeft ●TopLeft', 'display.turn ●TopLeft [(3, 1), (3, 2), (1, 3), (2, 3)]', 'display.move (3, 1) [(3, 1), (3, 2), (1, 3), (2, 3)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2●●●●', ' 3●〇●□', ' 4〇□□□', ' 〇TopLeft ●TopLeft', 'display.turn 〇TopLeft [(3, 2)]', 'display.move (3, 2) [(3, 2)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2●●●〇', ' 3●〇〇〇', ' 4〇□□□', ' 〇TopLeft ●TopLeft', 'display.turn ●TopLeft [(1, 3), (2, 3), (3, 3)]', 'display.move (1, 3) [(1, 3), (2, 3), (3, 3)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2●●●〇', ' 3●●〇〇', ' 4〇●□□', ' 〇TopLeft ●TopLeft', 'display.turn 〇TopLeft [(2, 3)]', 'display.move (2, 3) [(2, 3)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2●●●〇', ' 3●●〇〇', ' 4〇〇〇□', ' 〇TopLeft ●TopLeft', 'display.turn ●TopLeft [(3, 3)]', 'display.move (3, 3) [(3, 3)]', 'display.progress ', ' a b c d', ' 1●●●〇', ' 2●●●〇', ' 3●●●〇', ' 4〇〇〇●', ' 〇TopLeft ●TopLeft', 'display.win ●TopLeft', ] self.assertEqual(lines, output_ret) self.assertEqual(game2.result.winlose, Game.WHITE_WIN) self.assertEqual(game2.result.black_name, 'TopLeft') self.assertEqual(game2.result.white_name, 'TopLeft') self.assertEqual(game2.result.black_num, 6) self.assertEqual(game2.result.white_num, 10) # play-cancel-draw with captured_stdout() as stdout: game3.play() lines = stdout.getvalue().splitlines() output_ret = [ 'display.progress ', ' a b c d', ' 1□□□□', ' 2□●〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●BottomRight', 'cancel-event is set', 'display.draw', ] self.assertEqual(lines, output_ret) self.assertEqual(game3.result.winlose, Game.DRAW) self.assertEqual(game3.result.black_name, 'TopLeft') self.assertEqual(game3.result.white_name, 'BottomRight') self.assertEqual(game3.result.black_num, 2) self.assertEqual(game3.result.white_num, 2) # play-black-foul with captured_stdout() as stdout: game4.play() lines = stdout.getvalue().splitlines() output_ret = [ 'display.progress ', ' a b c d', ' 1□□□□', ' 2□●〇□', ' 3□〇●□', ' 4□□□□', ' 〇Foul ●BottomRight', 'display.turn 〇Foul [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.move (2, 2) [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.progress ', ' a b c d', ' 1□□□□', ' 2□●〇□', ' 3□〇〇□', ' 4□□□□', ' 〇Foul ●BottomRight', 'display.foul 〇Foul', 'display.win ●BottomRight', ] self.assertEqual(lines, output_ret) self.assertEqual(game4.result.winlose, Game.WHITE_WIN) self.assertEqual(game4.result.black_name, 'Foul') self.assertEqual(game4.result.white_name, 'BottomRight') self.assertEqual(game4.result.black_num, 3) self.assertEqual(game4.result.white_num, 1) # play-white-foul with captured_stdout() as stdout: game5.play() lines = stdout.getvalue().splitlines() output_ret = [ 'display.progress ', ' a b c d', ' 1□□□□', ' 2□●〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●Foul', 'display.turn 〇TopLeft [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.move (1, 0) [(1, 0), (0, 1), (3, 2), (2, 3)]', 'display.progress ', ' a b c d', ' 1□〇□□', ' 2□〇〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●Foul', 'display.turn ●Foul [(0, 0), (2, 0), (0, 2)]', 'display.move (2, 2) [(0, 0), (2, 0), (0, 2)]', 'display.progress ', ' a b c d', ' 1□〇□□', ' 2□〇〇□', ' 3□〇●□', ' 4□□□□', ' 〇TopLeft ●Foul', 'display.foul ●Foul', 'display.win 〇TopLeft', ] self.assertEqual(lines, output_ret) self.assertEqual(game5.result.winlose, Game.BLACK_WIN) self.assertEqual(game5.result.black_name, 'TopLeft') self.assertEqual(game5.result.white_name, 'Foul') self.assertEqual(game5.result.black_num, 4) self.assertEqual(game5.result.white_num, 1)
def test_player(self): class TestStrategy: def __init__(self): self.cnt = 0 def next_move(self, color, board): if self.cnt == 0: self.cnt += 1 return (5, 4) return (3, 2) # ----- # # Board # # ----- # board = Board(8) p = Player('black', 'TestPlayer', TestStrategy()) # init self.assertEqual(p.color, 'black') self.assertEqual(p.disc, d[p.color]) self.assertEqual(p.name, 'TestPlayer') self.assertIsInstance(p.strategy, TestStrategy) self.assertEqual(p.move, (None, None)) self.assertEqual(p.captures, []) # str self.assertEqual(str(p), d[p.color] + 'TestPlayer') # put_disc p.put_disc(board) self.assertEqual(p.move, (5, 4)) self.assertEqual(p.captures, [(4, 4)]) p.put_disc(board) self.assertEqual(p.move, (3, 2)) self.assertEqual(p.captures, [(3, 3)]) # -------- # # BitBoard # # -------- # board = BitBoard(8) p = Player('black', 'TestPlayer', TestStrategy()) # init self.assertEqual(p.color, 'black') self.assertEqual(p.disc, d[p.color]) self.assertEqual(p.name, 'TestPlayer') self.assertIsInstance(p.strategy, TestStrategy) self.assertEqual(p.move, (None, None)) self.assertEqual(p.captures, []) # str self.assertEqual(str(p), d[p.color] + 'TestPlayer') # put_disc p.put_disc(board) self.assertEqual(p.move, (5, 4)) self.assertEqual(p.captures, [(4, 4)]) p.put_disc(board) self.assertEqual(p.move, (3, 2)) self.assertEqual(p.captures, [(3, 3)])