def test_reversi_reset(): game = Reversi() game.reset() assert game.current == reversi.BLACK assert len(game.history) == 0 assert len(game.board) == 8 assert all(len(col) == 8 for col in game.board)
def setUp(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ O │ O │ O │ │ │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ * │ │ │ │ │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │6 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(1, 3)] = self.game.BLACK self.game.board[Coord(0, 2)] = self.game.WHITE self.game.board[Coord(0, 3)] = self.game.WHITE self.game.board[Coord(0, 4)] = self.game.WHITE self.game.board[Coord(7, 7)] = self.game.BLACK self.game.board[Coord(7, 6)] = self.game.BLACK self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY
def test_black_wins(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ │0 ───┼───┼───┼───┼───┼───┼───┼───┼ O │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY for column in range(8): self.game.board[Coord(1, column)] = self.game.WHITE for row in range(6, 8): for column in range(8): self.game.board[Coord(row, column)] = self.game.BLACK self.game.black_player.result = len(self.game.black_player_discs()) self.game.white_player.result = len(self.game.white_player_discs()) self.assertEqual(self.game.outcome(), self.game.GAME_STATES["BLACK_WINS"])
def test_tie(self): # set board ''' 0 1 2 3 4 5 6 7 O │ O │ O │ O │ O │ O │ O │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ O │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY for row in range(2): for column in range(8): self.game.board[Coord(row, column)] = self.game.WHITE for row in range(6, 8): for column in range(8): self.game.board[Coord(row, column)] = self.game.BLACK self.assertEqual(self.game.outcome(), self.game.GAME_STATES['TIE'])
def main(): game = Reversi() run_game = True while run_game: print('Starting new game!', 'Black goes first, then white', sep = '\n') player_color = validate('Enter \'b\' to choose to play black, \'w\' to choose white: ', ['b', 'w']) difficulty = validate('Enter \'1\' to choose easy computer opponent, \'2\' for hard computer opponent: ', ['1', '2']) game.newGame() game.setPlayerColour(player_color) board_and_score(game) game_over = False while not game_over: # This loop is the entire round if player_color == 'w': # Computer gets to go first if not game.isGameOver(): computer_turn(difficulty, game) if not game.isGameOver(): game_over = human_turn(player_color, game) else: game_over = True else: game_over = True elif player_color == 'b': # Player gets to go first if not game.isGameOver(): game_over = human_turn(player_color, game) if not game.isGameOver() and not game_over: computer_turn(difficulty, game) else: game_over = True else: game_over = True print('Game over!') restart = validate('Do you want to play again (y/n)? ', ['y', 'n']) if restart == 'n': print('Goodbye!') run_game = False
def test_is_move_valid(self): r = Reversi(''' - - - - - - - - - - b w - - - - - - b w b - - - - - b w b - - - - w b w w w w - w w b b b - - - - - b b - - - - - - - b - - - - ''') self.assert_only_valid_moves(r, 'b', { 'A4', 'A5', 'A7', 'C1', 'D1', 'E1', 'E2', 'F4', 'F6', 'G4', 'G6', 'H5' }) r = Reversi(''' - - - - - - - - - - - b w - w - - - w b w b b - - - - b w - - - - - - b w b - - - - - - w - - - - - - - - - - - - - - - - - - - ''') self.assert_only_valid_moves(r, 'b', { 'B2', 'B3', 'B4', 'D1', 'D7', 'F1', 'F2', 'F4', 'F6', 'F7', 'G1', 'H1' }) r = Reversi(''' - - - - - w - - - - b - w - - - - - b b b - - - - - b w b w - - - - b w b w w - - - b w b b - - - - - b b - - - - - - b b - - - ''') self.assert_only_valid_moves( r, 'w', { 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'C8', 'D2', 'F2', 'F3', 'F7', 'F8', 'G6', 'G7' }) r = Reversi(''' - - - - - - - - - - - - - - - - - - w - w - - - - - w w w w - - - - w b b b b b - - - b b w - - - - - w b - - - - - w - b - - - ''') self.assert_only_valid_moves( r, 'w', {'C6', 'C7', 'D8', 'F7', 'F8', 'G4', 'G6', 'H4', 'H6'})
def test_white_wins(self): # set board ''' 0 1 2 3 4 5 6 7 O │ O │ O │ O │ O │ O │ O │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ O │ O │ O │ O │ O │ O │2 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ O │ O │ O │ O │ O │3 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ O │ O │ O │ O │4 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ O │ O │ O │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ O │ O │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ O │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() for row in range(0, 8): for column in range(row, 8): self.game.board[Coord(row, column)] = self.game.WHITE for row in range(7, 0, -1): for column in range(row - 1, -1, -1): self.game.board[Coord(row, column)] = self.game.BLACK self.game.black_player.result = len(self.game.black_player_discs()) self.game.white_player.result = len(self.game.white_player_discs()) self.assertEqual(self.game.outcome(), self.game.GAME_STATES["WHITE_WINS"])
def test_reversi_load_extra_file_no_file_error(self): app = Reversi() app.err_msg = TestErrMsg() with captured_stdout() as stdout: app._load_extra_file('./no_file') lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], '指定された登録ファイルが見つかりませんでした')
def test_reversi_load_extra_file_format_error(self): app = Reversi() app.err_msg = TestErrMsg() with captured_stdout() as stdout: app._load_extra_file('./not_json.json') lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'フォーマットエラーのため登録ファイルが読み込めませんでした')
def reset(self) -> torch.Tensor: # (num_worker, 3, SIZE, SIZE) self.historys = [[] for _ in range(self.num_workers)] self.end = [False for _ in range(self.num_workers)] black = Reversi() white = Reversi() white.place(Minimax.brain(white, 1), 1) self.reversis = [deepcopy(black) for _ in range(self.num_workers // 2)] + [deepcopy(white) for _ in range(self.num_workers // 2)] states = self.pool.map(getBoardState, self.reversis) return torch.Tensor(states)
def __init__(self, width, height): self.height = height self.width = width self.game = Reversi() self.is_clicked = False self.end = False self.last_move = [] self.in_menu = True self.player = -1 self.computer = 1
def test_reversi_hash(): import random game = Reversi() s = set() s.add(game) assert len(s) == 1 for i in range(12): game.put(random.choice(game.getAvailables())) assert game not in s s.add(game)
def test_reversi_game_start(self): def test_thread_start(game_thread): print(game_thread.daemon) app = Reversi() app._thread_start = test_thread_start with captured_stdout() as stdout: app.game_start() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'True')
def test_reversi_thread_start(self): class TestThreadStart: def start(self): print('thread.start()') app = Reversi() with captured_stdout() as stdout: app._thread_start(TestThreadStart()) lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'thread.start()')
def test_reversi_gameloop(self): def test_game(): print('test_game') return True app = Reversi() app.game = test_game with captured_stdout() as stdout: app.gameloop() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'test_game')
def jogar(): data = json.loads(request.data) pos = data['cell'].split() reversi = Reversi(data['tabuleiro']) jogadorAtual = 'P' jogadaValida = reversi.fazerMovimento(jogadorAtual, (int(pos[0]), int(pos[1]))) jogo = reversi.getJogo() jogo['jogadaValida'] = jogadaValida return jsonify(jogo)
def ia(): data = json.loads(request.data) reversi = Reversi(data['tabuleiro']) jogadorAtual = 'B' ia = Arvore(reversi, jogadorAtual) jogadaIA = ia.getJogada() jogadaValida = reversi.fazerMovimento(jogadorAtual, jogadaIA) jogo = reversi.getJogo() return jsonify(jogo)
def test_reversi_state(self): app = Reversi() app.state = 'INIT' self.assertEqual(app.state, app.INIT) self.assertEqual(app.game, app._Reversi__init) app.state = 'DEMO' self.assertEqual(app.state, app.DEMO) self.assertEqual(app.game, app._Reversi__demo) app.state = 'PLAY' self.assertEqual(app.state, app.PLAY) self.assertEqual(app.game, app._Reversi__play) app.state = 'END' self.assertEqual(app.state, app.END) self.assertEqual(app.game, app._Reversi__end) app.state = 'REINIT' self.assertEqual(app.state, app.REINIT) self.assertEqual(app.game, app._Reversi__reinit) app.state = 'UNDEFINED' self.assertEqual(app.state, 'UNDEFINED') self.assertEqual(app.game, app._Reversi__reinit)
class ReversiDataGenerator: def __init__(self): self.game = Reversi() self.round = 0 self.current_player = 1 def get_state_action(self, resp): state = copy.deepcopy(self.game.state()) player = self.current_player x, y = resp["response"]["x"], resp["response"]["y"] action = y * 8 + x if x != -1 and y != -1 else -1 self.game.step(action) self.current_player *= -1 return player, state, action
def test_does_south_east_traversal_allow_valid_move(self): r = Reversi(''' - - - - - - - - - - b w - - - - - - b w b - - - - - b w b - - - - w b w w w w - w w b b b - - - - - b b - - - - - - - b - - - - ''') self.assertTrue(r.is_traversal_valid_for('south_east', 'b', 'A4')) r = Reversi(''' - - - - - - - - - - - - - - - - - - - - - - - - - - - w b - - - - - - b w - - - - - - - - - - - - - - - - - - - - - - - - - - - ''') moves_invalid_for_south_east_traversal = self.all_moves for move in moves_invalid_for_south_east_traversal: self.assertFalse(r.is_traversal_valid_for('south_east', 'b', move), 'error for position: ' + move) r = Reversi(''' - - - - - - - - - - - - - - - - - - - - - - - - - - - w w w - - - - - w b b - - - - - b - - - - - - b - - - - - - - - - - - - - ''') moves_valid_for_south_east_traversal = {'C3', 'D3'} moves_invalid_for_south_east_traversal = self.all_moves - moves_valid_for_south_east_traversal for move in moves_valid_for_south_east_traversal: self.assertTrue(r.is_traversal_valid_for('south_east', 'b', move), 'error for position: ' + move) for move in moves_invalid_for_south_east_traversal: self.assertFalse(r.is_traversal_valid_for('south_east', 'b', move), 'error for position: ' + move)
class AIHelper(): MAX_PLAYER = 'w' MIN_PLAYER = 'b' INFINITY = 1.0e+10 """ Helper interface class for the AI $1. available moves (board, player) $2. get_resulting_board -> (board, player, coord) $3. player pools (board, player) $4. check if game has ended (board) """ # it is created when the game starts def __init__(self, board=None): self.game = Reversi() if board: self.set_board(board) # changes to board form ai to game board def set_board(self, board): self.game.board = FormatConverter.ai_to_game_board(board) # sets a player def set_player(self, player): self.game.player = Player(player) # finding available moves def available_moves(self, board, player): self.set_board(board) self.set_player(player) return self.game.available_fields() # gets the changes of the human player def get_resulting_board(self, board, player, coord): self.set_board(board) self.set_player(player) self.game.play(coord) return FormatConverter.game_to_ai_board(self.game.board) def player_pool(self, board, player): self.set_board(board) # probably this is an error return ''.join(''.join(row) for row in self.board).count(player) # defines if the game is over or not def is_game_over(self, board): self.set_board(board) return self.game.outcome() != self.game.GAME_STATES["IN_PROGRESS"]
def test_black_wins(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ │0 ───┼───┼───┼───┼───┼───┼───┼───┼ O │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY for column in range(8): self.game.board[Coord(1, column)] = self.game.WHITE for row in range(6, 8): for column in range(8): self.game.board[Coord(row, column)] = self.game.BLACK self.game.black_player.result = len(self.game.black_player_discs()) self.game.white_player.result = len(self.game.white_player_discs()) self.assertEqual( self.game.outcome(), self.game.GAME_STATES["BLACK_WINS"])
def test_white_wins(self): # set board ''' 0 1 2 3 4 5 6 7 O │ O │ O │ O │ O │ O │ O │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ O │ O │ O │ O │ O │ O │2 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ O │ O │ O │ O │ O │3 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ O │ O │ O │ O │4 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ O │ O │ O │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ O │ O │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ O │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() for row in range(0, 8): for column in range(row, 8): self.game.board[Coord(row, column)] = self.game.WHITE for row in range(7, 0, -1): for column in range(row - 1, -1, -1): self.game.board[Coord(row, column)] = self.game.BLACK self.game.black_player.result = len(self.game.black_player_discs()) self.game.white_player.result = len(self.game.white_player_discs()) self.assertEqual( self.game.outcome(), self.game.GAME_STATES["WHITE_WINS"])
def test_reversi_put(): game = Reversi() game.reset() assert game.put(2, 4) assert game.history[-1] == [(3, 4), (2, 4)] assert not game.put(2, 4) assert not game.put(0, 0) assert not game.put((0, 0)) # Tuple unpacking test assert game.put((2, 3))
def test_reversi__end(self): class TestWindow: def __init__(self): class Start: def __init__(self): class Event: def is_set(self): return True def clear(self): print('clear') self.event = Event() self.start = Start() def set_state(self, state): print(state) app = Reversi() app.window = TestWindow() # REINIT app._setting_changed = lambda: False with captured_stdout() as stdout: app._Reversi__end() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'normal') self.assertEqual(lines[1], 'clear') with self.assertRaises(IndexError): print(lines[2]) self.assertEqual(app.state, Reversi.REINIT) # INIT app.window.start.event.is_set = lambda: False app._setting_changed = lambda: True with captured_stdout() as stdout: app._Reversi__end() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'normal') with self.assertRaises(IndexError): print(lines[1]) self.assertEqual(app.state, Reversi.INIT)
def test_does_west_traversal_allow_valid_move(self): r = Reversi(''' - - - - - - - - - - - - - - - - - - - - - - - - - - - w b - - - - - - b w - - - - - - - - - - - - - - - - - - - - - - - - - - - ''') moves_valid_for_west_traversal = {'F5'} moves_invalid_for_west_traversal = self.all_moves - moves_valid_for_west_traversal for move in moves_valid_for_west_traversal: self.assertTrue(r.is_traversal_valid_for('west', 'b', move), 'error for position: ' + move) for move in moves_invalid_for_west_traversal: self.assertFalse(r.is_traversal_valid_for('west', 'b', move), 'error for position: ' + move) r = Reversi(''' - - - - - - - - - - - - - - - - - - w b w - - - - - w w b b - - - - w b b w - - b - b b w w - - b w w w - - - - b - - - - - - - ''') self.assertFalse(r.is_traversal_valid_for('west', 'w', 'B6'))
def test_reversi__reinit(self): class TestWindow: def init_screen(self): print('init_screen') app = Reversi() app.window = TestWindow() with captured_stdout() as stdout: app._Reversi__reinit() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'init_screen') with self.assertRaises(IndexError): print(lines[1]) self.assertEqual(app.state, Reversi.PLAY)
def board_images(data=None, size=1040): if data is None: return 'NG' r = Reversi() r.insert(data) r.create_board_images() r.update_board_images() buf = BytesIO() r.board_images[size].save(buf, 'png') buf.seek(0, 0) return send_file(buf, mimetype='image/png')
class AIHelper(): MAX_PLAYER = 'w' MIN_PLAYER = 'b' INFINITY = 1.0e+10 """ Helper interface class for the AI $1. available moves (board, player) $2. get_resulting_board -> (board, player, coord) $3. player pools (board, player) $4. check if game has ended (board) """ def __init__(self, board=None): self.game = Reversi() if board: self.set_board(board) def set_board(self, board): self.game.board = FormatConverter.ai_to_game_board(board) def set_player(self, player): self.game.player = Player(player) def available_moves(self, board, player): self.set_board(board) self.set_player(player) return self.game.available_fields() def get_resulting_board(self, board, player, coord): self.set_board(board) self.set_player(player) self.game.play(coord) return FormatConverter.game_to_ai_board(self.game.board) def player_pool(self, board, player): self.set_board(board) return ''.join(''.join(row) for row in self.board).count(player) def is_game_over(self, board): self.set_board(board) return self.game.outcome() != self.game.GAME_STATES["IN_PROGRESS"]
class ReversiSkipTurn(unittest.TestCase): def setUp(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ O │ O │ O │ │ │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ * │ │ │ │ │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │6 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(1, 3)] = self.game.BLACK self.game.board[Coord(0, 2)] = self.game.WHITE self.game.board[Coord(0, 3)] = self.game.WHITE self.game.board[Coord(0, 4)] = self.game.WHITE self.game.board[Coord(7, 7)] = self.game.BLACK self.game.board[Coord(7, 6)] = self.game.BLACK self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY def test_outcome(self): self.assertEqual( self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS']) def test_current_after_outcome(self): self.game.outcome() self.assertEqual(self.game.player.field, self.game.WHITE)
def test_reversi_any(): game = Reversi() game.reset() assert game.any() assert game.any(reversi.BLACK) assert game.any(reversi.WHITE) game.board[3][3] = game.board[3][4] = game.board[4][3] = game.board[4][4] = reversi.EMPTY assert not game.any(reversi.BLACK) assert not game.any(reversi.WHITE)
def test_reversi_keyword_arg(self): app = Reversi(turn_disc_wait=0.001, sleep_time_play=0.002, sleep_time_end=0.003, sleep_time_turn=0.004, sleep_time_move=0.005) self.assertEqual(app.turn_disc_wait, 0.001) self.assertEqual(app.sleep_time_play, 0.002) self.assertEqual(app.sleep_time_end, 0.003) self.assertEqual(app.sleep_time_turn, 0.004) self.assertEqual(app.sleep_time_move, 0.005)
def test_reversi_window_start(self): class TestWindow: def __init__(self): class Root: def deiconify(self): print('deiconify') def mainloop(self): print('mainloop') self.root = Root() app = Reversi() app.window = TestWindow() with captured_stdout() as stdout: app.window_start() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'deiconify') self.assertEqual(lines[1], 'mainloop')
class ReversiSkipTurn(unittest.TestCase): def setUp(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ O │ O │ O │ │ │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ * │ │ │ │ │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │6 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(1, 3)] = self.game.BLACK self.game.board[Coord(0, 2)] = self.game.WHITE self.game.board[Coord(0, 3)] = self.game.WHITE self.game.board[Coord(0, 4)] = self.game.WHITE self.game.board[Coord(7, 7)] = self.game.BLACK self.game.board[Coord(7, 6)] = self.game.BLACK self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY def test_outcome(self): self.assertEqual(self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS']) def test_current_after_outcome(self): self.game.outcome() self.assertEqual(self.game.player.field, self.game.WHITE)
def setUp(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ * │ │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ O │ │ │ * │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ * │ O │ * │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ * │ O │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ W │ * │ * │ O │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ │ * │ │ * │ │ │ │ │6 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ O │ │ │ │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(0, 7)] = self.game.WHITE self.game.board[Coord(1, 6)] = self.game.BLACK self.game.board[Coord(2, 2)] = self.game.WHITE self.game.board[Coord(2, 5)] = self.game.BLACK self.game.board[Coord(3, 2)] = self.game.BLACK self.game.board[Coord(5, 3)] = self.game.BLACK self.game.board[Coord(5, 4)] = self.game.BLACK self.game.board[Coord(5, 5)] = self.game.WHITE self.game.board[Coord(6, 1)] = self.game.BLACK self.game.board[Coord(6, 3)] = self.game.BLACK self.game.board[Coord(7, 4)] = self.game.WHITE self.game.player = self.game.white_player
def test_primeira_jogada_preto_3_1(self): reversi = Reversi() posicao = (3, 1) resultado = [[" ", " ", " ", " "], [" ", "P", "B", " "], [" ", "P", "P", " "], [" ", "P", " ", " "]] reversi.jogada("P", posicao) self.assertEqual(reversi.tabuleiro, resultado)
def __init__(self, board=None): self.game = Reversi() if board: self.set_board(board)
def test_primeira_jogada_branco_2_3(self): reversi = Reversi() posicao = (2, 3) resultado = [[" ", " ", " ", " "], [" ", "P", "B", " "], [" ", "B", "B", "B"], [" ", " ", " ", " "]] reversi.jogada("B", posicao) self.assertEqual(reversi.tabuleiro, resultado)
def setUp(self): self.game = Reversi()
class ReversiInitialBoardTest(unittest.TestCase): def setUp(self): self.game = Reversi() def test_initial_board(self): self.assertEqual(len(self.game.board), 64) self.assertEqual(self.game.board[Coord(0, 1)], ' ') self.assertEqual(self.game.board[Coord(3, 4)], self.game.BLACK) def test_valid_move(self): self.assertTrue(self.game.is_valid_move(Coord(4, 5))) self.assertTrue(self.game.is_valid_move(Coord(3, 2))) self.assertFalse(self.game.is_valid_move(Coord(3, 1))) self.assertFalse(self.game.is_valid_move(Coord(3, 3))) def test_is_enemy_disc(self): self.assertTrue(self.game.is_enemy_disc(Coord(3, 3))) self.assertFalse(self.game.is_enemy_disc(Coord(7, 0))) self.assertFalse(self.game.is_enemy_disc(Coord(4, 3))) def test_play(self): self.game.play(Coord(4, 5)) self.assertEqual(self.game.board[Coord(4, 4)], self.game.BLACK) self.assertEqual(self.game.board[Coord(4, 5)], self.game.BLACK) def test_available_fields(self): fields = {Coord(4, 5), Coord(5, 4), Coord(3, 2), Coord(2, 3)} self.assertEqual(fields, set(self.game.available_fields())) def test_outcome(self): self.assertEqual( self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS']) def test_outcome_after_play(self): self.game.play(Coord(5, 4)) self.assertEqual( self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS']) def test_players_result(self): self.assertEqual(self.game.black_player.result, 2) self.assertEqual(self.game.white_player.result, 2) def test_change_current_player(self): self.game.change_current_player() self.assertEqual(self.game.player.field, self.game.WHITE)
class ReversiEndGame(unittest.TestCase): def test_tie(self): # set board ''' 0 1 2 3 4 5 6 7 O │ O │ O │ O │ O │ O │ O │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ O │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY for row in range(2): for column in range(8): self.game.board[Coord(row, column)] = self.game.WHITE for row in range(6, 8): for column in range(8): self.game.board[Coord(row, column)] = self.game.BLACK self.assertEqual(self.game.outcome(), self.game.GAME_STATES['TIE']) def test_black_wins(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ │0 ───┼───┼───┼───┼───┼───┼───┼───┼ O │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ * │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(3, 3)] = self.game.EMPTY self.game.board[Coord(3, 4)] = self.game.EMPTY self.game.board[Coord(4, 3)] = self.game.EMPTY self.game.board[Coord(4, 4)] = self.game.EMPTY for column in range(8): self.game.board[Coord(1, column)] = self.game.WHITE for row in range(6, 8): for column in range(8): self.game.board[Coord(row, column)] = self.game.BLACK self.game.black_player.result = len(self.game.black_player_discs()) self.game.white_player.result = len(self.game.white_player_discs()) self.assertEqual( self.game.outcome(), self.game.GAME_STATES["BLACK_WINS"]) def test_white_wins(self): # set board ''' 0 1 2 3 4 5 6 7 O │ O │ O │ O │ O │ O │ O │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ O │ O │ O │ O │ O │ O │ O │1 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ O │ O │ O │ O │ O │ O │2 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ O │ O │ O │ O │ O │3 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ O │ O │ O │ O │4 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ O │ O │ O │5 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ O │ O │6 ───┼───┼───┼───┼───┼───┼───┼───┼ * │ * │ * │ * │ * │ * │ * │ O │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() for row in range(0, 8): for column in range(row, 8): self.game.board[Coord(row, column)] = self.game.WHITE for row in range(7, 0, -1): for column in range(row - 1, -1, -1): self.game.board[Coord(row, column)] = self.game.BLACK self.game.black_player.result = len(self.game.black_player_discs()) self.game.white_player.result = len(self.game.white_player_discs()) self.assertEqual( self.game.outcome(), self.game.GAME_STATES["WHITE_WINS"])
from reversi import Reversi a = Reversi(10,10,10) estado = a.iniciarJogo()
class ReversiTest(unittest.TestCase): def setUp(self): # set board ''' 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ * │ │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ O │ │ │ * │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ * │ O │ * │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ * │ O │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ W │ * │ * │ O │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ │ * │ │ * │ │ │ │ │6 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ O │ │ │ │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' self.game = Reversi() self.game.board[Coord(0, 7)] = self.game.WHITE self.game.board[Coord(1, 6)] = self.game.BLACK self.game.board[Coord(2, 2)] = self.game.WHITE self.game.board[Coord(2, 5)] = self.game.BLACK self.game.board[Coord(3, 2)] = self.game.BLACK self.game.board[Coord(5, 3)] = self.game.BLACK self.game.board[Coord(5, 4)] = self.game.BLACK self.game.board[Coord(5, 5)] = self.game.WHITE self.game.board[Coord(6, 1)] = self.game.BLACK self.game.board[Coord(6, 3)] = self.game.BLACK self.game.board[Coord(7, 4)] = self.game.WHITE self.game.player = self.game.white_player def test_valid_move(self): self.assertTrue(self.game.is_valid_move(Coord(7, 3))) self.assertTrue(self.game.is_valid_move(Coord(4, 2))) self.assertTrue(self.game.is_valid_move(Coord(2, 4))) self.assertTrue(self.game.is_valid_move(Coord(6, 4))) self.assertTrue(self.game.is_valid_move(Coord(6, 2))) self.assertFalse(self.game.is_valid_move(Coord(6, 0))) def test_play_new_board(self): self.game.play(Coord(5, 2)) # board after the turn ''' 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ O │0 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ │ │ O │ │1 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ O │ │ │ O │ │ │2 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ * │ O │ O │ │ │ │3 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ O │ O │ │ │ │4 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ O │ O │ O │ O │ │ │5 ───┼───┼───┼───┼───┼───┼───┼───┼ │ * │ │ O │ │ │ │ │6 ───┼───┼───┼───┼───┼───┼───┼───┼ │ │ │ │ O │ │ │ │7 ───┼───┼───┼───┼───┼───┼───┼───┼ ''' new_board = self.game.board.copy() new_board[Coord(5, 2)] = self.game.WHITE new_board[Coord(1, 6)] = self.game.WHITE new_board[Coord(2, 5)] = self.game.WHITE new_board[Coord(4, 3)] = self.game.WHITE new_board[Coord(3, 4)] = self.game.WHITE new_board[Coord(6, 3)] = self.game.WHITE new_board[Coord(5, 3)] = self.game.WHITE new_board[Coord(5, 4)] = self.game.WHITE self.assertEqual(new_board, self.game.board) self.assertEqual(self.game.black_player.result, 2) self.assertEqual(self.game.white_player.result, 14) def test_available_fields(self): fields = {Coord(7, 3), Coord(4, 2), Coord(2, 4), Coord(6, 4), Coord(6, 2), Coord(5, 2), Coord(3, 1), Coord(3, 5)} self.assertEqual(fields, set(self.game.available_fields())) def test_outcome(self): self.assertEqual( self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS']) def test_outcome_after_play(self): self.game.play(Coord(2, 4)) self.assertEqual( self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS']) def black_player_discs_test(self): self.assertEqual(len(self.game.black_player_discs()), 9) def white_player_discs_test(self): self.assertEqual(len(self.game.white_player_discs()), 6)
def test_segunda_jogada_branco_1_0(self): reversi = Reversi() reversi.jogada("P", (3, 1)) reversi.jogada("B", (1, 0)) resultado = [[" ", " ", " ", " "], ["B", "B", "B", " "], [" ", "P", "P", " "], [" ", "P", " ", " "]] self.assertEqual(reversi.tabuleiro, resultado)