Beispiel #1
0
def test_board_with_half_mines_prints(capsys):
    # arrange
    board_input = board.Board(2, 2)

    for y in range(2):
        for x in range(2):
            board_input.reveal(y, x)

    sut = view.View(board_input)

    # act
    sut.print()
    out, err = capsys.readouterr()

    # assert
    expected_chars = []
    for cell in board_input.board:
        if cell.mine:
            expected_chars.append(constants.display_char_mine)
        else:
            expected_chars.append(cell.n_surrounding_mines)

    a, b, c, d = expected_chars

    actual = re.findall(f'{a}|{b}|{c}|{d}', out)
    # this test is slightly imprecise
    assert len(actual) >= 4
Beispiel #2
0
    def __init__(self, players):
        """Domyślne ustawienia klasy

            Args:
                players (int): Ilość wszystkich graczy

        """
        super(Game, self).__init__()
        qApp.installEventFilter(self)
        self.released = True
        self.players = players

        self.board = board.Board(players)
        self.timers = []
        self.timer = QTimer()
        self.timer.setInterval(const.GAME_SPEED)
        self.timer.timeout.connect(self.repaint)
        self.timer.start()

        self.nr_frame = 0
        if self.players != 0:
            self.frames = []
            self.frame = np.ones((const.BOARD_WIDTH, const.BOARD_HEIGHT),
                                 dtype=int)
            self.doc = xml.Document()
            self.root = self.doc.createElement('save')
        else:
            doc = xml.parse("autosave.xml")
            self.frames = doc.getElementsByTagName("frame")
Beispiel #3
0
def test_board_is_square_with_correct_dimensions(expected_size):
    # arrange
    sut = board.Board(expected_size)

    # act
    actual = sut.board

    # assert
    assert len(actual) == expected_size**2
Beispiel #4
0
def test_corrent_number_of_mines_on_creation(expected_mines):
    # arrange
    sut = board.Board(10, expected_mines)

    # act
    actual_mines = sum(map(lambda x: x.mine, sut.board))

    # assert
    assert expected_mines == actual_mines
Beispiel #5
0
def test_flags_square():
    # arrange
    sut = board.Board(3)

    # act
    sut.toggle_flag(1, 2)  # y, x
    actual = sut.board

    # assert
    assert actual[5].flagged == True
Beispiel #6
0
def test_all_with_single_reveal_if_no_mines():
    # arrange
    mines = 0
    sut = board.Board(3, mines)

    # act
    sut.reveal(1, 2)  # y, x

    # assert
    assert all(cell.revealed for cell in sut.board)
Beispiel #7
0
def test_reveals_square():
    # arrange
    sut = board.Board(3)

    # act
    sut.reveal(1, 2)  # y, x
    actual = sut.board

    # assert
    assert actual[5].revealed == True
Beispiel #8
0
def test_flag_of_square_is_toggled_back():
    # arrange
    sut = board.Board(3)

    # act
    sut.toggle_flag(1, 2)
    sut.board[5].flagged = True
    sut.toggle_flag(1, 2)

    # assert
    assert sut.board[5].flagged == False
Beispiel #9
0
def test_new_board_printed_all_empty(capsys):
    # arrange
    board_input = board.Board(2)
    sut = view.View(board_input)

    # act
    sut.print()
    out, err = capsys.readouterr()

    # assert
    actual = re.findall('_', out)
    assert len(actual) == 4
Beispiel #10
0
def test_lost_if_revealed_mine():
    # arrange
    sut = board.Board(3)

    # act
    # reveal all
    for y in range(3):
        for x in range(3):
            sut.reveal(y, x)

    # assert
    assert sut.lost()
Beispiel #11
0
def test_adjacent_indices(index, expected):
    # arrange
    sut = board.Board(3)
    # 0 1 2
    # 3 4 5
    # 6 7 8

    # act
    actual = sut._get_adjacent_indices(index)

    # assert
    assert set(actual) == set(expected)
Beispiel #12
0
def test_board_with_flag_prints_flag(capsys):
    # arrange
    board_input = board.Board(2, 0)
    board_input.toggle_flag(0, 0)
    sut = view.View(board_input)

    # act
    sut.print()
    out, err = capsys.readouterr()

    # assert
    actual = re.findall('f', out)
    assert len(actual) == 1
Beispiel #13
0
def main():
    """
    Control flow for the game.
    :return: None.
    """

    game_board = board.Board()
    p1 = player.Player()
    comp = ai.AI()

    run = True
    clock = pygame.time.Clock()
    winner = None  # who won the game?

    while run:
        clock.tick(60)

        # if all the ships of the player or of the AI have been hit, end the game.
        if game_board.player_hit_ships == 20 or game_board.ai_hit_ships == 20:
            # declare the winner and end the game
            if game_board.player_hit_ships == 1:
                game_board.fullscreen_message("AI has won!")
                winner = "AI"
            else:
                game_board.fullscreen_message("Player has won!")
                winner = game_board.info.username
            time.sleep(3)
            run = False
            break

        # process each event.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = event.pos
                valid = register_click(game_board, pos, p1)

                if valid:
                    time.sleep(0.5)  # slight delay before AI makes a move
                    comp_move = comp.make_move()
                    game_board.register_ai_hit(comp_move)

    # display the top scores
    game_board.info.end_game(game_board.player_hit_ships, game_board.ai_hit_ships, winner)
    # end the game
    pygame.quit()
Beispiel #14
0
def test_board_with_all_mines_prints_all_mines(capsys):
    # arrange
    board_input = board.Board(2, 4)

    for y in range(2):
        for x in range(2):
            board_input.reveal(y, x)

    sut = view.View(board_input)

    # act
    sut.print()
    out, err = capsys.readouterr()

    # assert
    actual = re.findall('x', out)
    assert len(actual) == 4
Beispiel #15
0
def test_board_with_no_mines_prints_all_zeroes(capsys):
    # arrange
    board_input = board.Board(2, 0)

    for y in range(2):
        for x in range(2):
            board_input.reveal(y, x)

    sut = view.View(board_input)

    # act
    sut.print()
    out, err = capsys.readouterr()

    # assert
    two_zeroes_in_labels = 2
    actual = re.findall('0', out)
    assert len(actual) - two_zeroes_in_labels == 4
Beispiel #16
0
def average_time(height, width, num_of_mines):
    app = QApplication(sys.argv)

    count = 0
    ac = 0

    while (count < 20):
        board = b.Board(height, width, num_of_mines)
        start = time.time()
        board.play_game()
        end = time.time()

        if board.is_end_game():
            count += 1
            exe_time = end - start
            ac += exe_time

    print('Media de tiempo de ejecución: {0} seg'.format(ac / count))
    sys.exit()
Beispiel #17
0
def average_success(height, width, num_of_mines):
    app = QApplication(sys.argv)

    victory_count = 0
    loss_count = 0
    count = 0

    while (count < 5):
        count += 1
        board = b.Board(height, width, num_of_mines)
        start = time.time()
        board.play_game()
        end = time.time()

        if board.is_end_game():
            victory_count += 1
        else:
            loss_count += 1

    print('- Victorias: {0} de {1}'.format(victory_count, count))
    print('- Derrotas: {0} de {1}'.format(loss_count, count))
    print('- Porcentaje de victoria: {0}'.format(victory_count / count))
    sys.exit()
Beispiel #18
0
 def test_constructor(self):
     test_board = board.Board(self.board_id, self.cards)
     self.assertEqual(test_board.board_id, self.board_id)
Beispiel #19
0
def test_not_won_on_initialize():
    # arrange
    sut = board.Board(3)

    # assert
    assert sut.won() == False
Beispiel #20
0
def main(height, width, num_of_mines):
    app = QApplication(sys.argv)
    board = b.Board(height, width, num_of_mines)
    sys.exit(app.exec())