Exemplo n.º 1
0
 def new_game(self):
     """Alustaa uuden pelin asettamalla kellon ja pelistatuksen
     vastaamaan alkavaa peliä ja luo uuden peliruudukon.
     """
     self._game_clock.reset()
     self.game_status.set_status(Status.READY)
     self._grid = ViewGrid(self._difficulty, self.game_status)
     self._initialized_sprites()
     self._continue_game = True
Exemplo n.º 2
0
 def test_if_the_flag_in_a_wrong_places_gives_right_answer(self):
     self.random_view.push_left_button(0, 0)
     mine_locations = self.random_view.grid.give_mines_locations()
     coord_y = None
     coord_x = None
     not_found = True
     while not_found:
         for i in range(self.random_view.height):
             for j in range(self.random_view.width):
                 if not_found and (i, j) not in mine_locations:
                     if self.random_view.coordinates(i, j) == " ":
                         self.random_view.push_right_button(i, j)
                         coord_y = i
                         coord_x = j
                         not_found = False
         if not_found:
             self.game_status = GameStatus()
             self.random_view = ViewGrid(self.difficulty, self.game_status)
     self.random_view.push_left_button(mine_locations[0][0],
                                       mine_locations[0][1])
     result = self.random_view.coordinates(coord_y, coord_x)
     self.assertEqual(result, "w")
Exemplo n.º 3
0
    def test_if_difficulties_have_right_sizes_and_amoud_of_mines(self):
        self.random_view.push_left_button(0, 0)
        height1 = self.random_view.height
        width1 = self.random_view.width
        mines1 = len(self.random_view.grid.give_mines_locations())

        self.difficulty.medium()
        self.game_status = GameStatus()
        self.random_view = ViewGrid(self.difficulty, self.game_status)
        self.random_view.push_left_button(0, 0)
        height2 = self.random_view.height
        width2 = self.random_view.width
        mines2 = len(self.random_view.grid.give_mines_locations())

        self.difficulty.hard()
        self.game_status = GameStatus()
        self.random_view = ViewGrid(self.difficulty, self.game_status)
        self.random_view.push_left_button(0, 0)
        height3 = self.random_view.height
        width3 = self.random_view.width
        mines3 = len(self.random_view.grid.give_mines_locations())
        results = (width1, height1, mines1, width2, height2, mines2, width3,
                   height3, mines3)
        self.assertEqual(results, (9, 9, 10, 16, 16, 40, 30, 16, 99))
Exemplo n.º 4
0
class GameView:
    """Luokka vastaa pelinäkymän spritejen ylläpidosta, pelin jatkuvuudesta
    ja välittää käyttöliittymän painallukset peliruudukkoa
    ylläpitävälle ViewGrid-luokalle.
    """
    def __init__(self, difficulty, square_size, status):
        """[summary]

        Args:
            difficulty (Difficulty): Pelin vaikeustasoa kuvastava luokka.
            square_size (int): Peliruudukon yhden ruudun leveys/korkeus.
            status (GameStatus): Luokka, joka välittää tietoa pelin tilasta
            muille luokille.
        """
        self._difficulty = difficulty
        self._square_size = square_size
        self._sprites = pg.sprite.Group()
        self._game_clock = Clock()
        self.game_status = status
        self.new_game()
        self._initialized_sprites()
        self._continue_game = True

    def new_game(self):
        """Alustaa uuden pelin asettamalla kellon ja pelistatuksen
        vastaamaan alkavaa peliä ja luo uuden peliruudukon.
        """
        self._game_clock.reset()
        self.game_status.set_status(Status.READY)
        self._grid = ViewGrid(self._difficulty, self.game_status)
        self._initialized_sprites()
        self._continue_game = True

    def _initialized_sprites(self):
        """Päivittää pelin spritet peliruudukon tietojen mukaan.
        """
        self._sprites.empty()
        for y in range(self._difficulty.height()):
            for x in range(self._difficulty.width()):
                square_type = self._grid.coordinates(y, x)
                self._sprites.add(Square(y, x, square_type, self._square_size))

    def push_button(self, pos_x, pos_y, button):
        """Välittää tiedot napin painalluksesta peliruudukolle ja käynnistää
        pelikellon ensimmäisen painalluksen yhteydessä.

        Args:
            pos_x (int): painalluksen kohta näyttöliittymän pelialustan x-akselilla
            pos_y (int): painalluksen kohta näyttöliittymän pelialustan y-akselilla
            button ("str"]): tieto, onko oikean vai vasemman napin painallus
        """
        if self.game_status.get_status() == Status.READY:
            self._game_clock.start()
        if self._continue_game:
            coord_y = ((pos_x) // self._square_size)
            coord_x = ((pos_y) // self._square_size)
            if button == "left":
                self._grid.push_left_button(coord_x, coord_y)
            if button == "right":
                self._grid.push_right_button(coord_x, coord_y)
            self.check_status()
        self._initialized_sprites()

    def check_status(self):
        """Tarkistaa pelitilan ja pysäyttää pelin, jos tarpeen.
        """
        if self.game_status.get_status() == Status.GAMEOVER:
            self._game_clock.stop()
            self._continue_game = False
        if self.game_status.get_status() == Status.VICTORY:
            self._continue_game = False
            self._game_clock.stop()

    def pause(self):
        """Pysäyttää pelin.
        """
        if self._continue_game:
            self._continue_game = False

    def stop_pause(self):
        """Poistaa pelin pysäytyksen.
        """
        if not self._continue_game:
            self._continue_game = True

    def give_time(self):
        """Palauttaa pelikellon ajankohdan sekuntteina.

        Returns:
            int: pelikellon aika sekunttien tarkkuudella
        """
        return self._game_clock.give_time_in_seconds()

    def give_exact_time(self):
        """Palauttaa pelikellon täsmällisen ajankohdan

        Returns:
            float: pelikellon täsmällinen aika
        """
        return self._game_clock.give_exact_time()

    def give_flags(self):
        """Palauttaa pelin lippujen jäljelläolevan määrän.

        Returns:
            int: pelin lippujen käytettävissä oleva määrä
        """
        return self._grid.give_flags()
Exemplo n.º 5
0
 def setUp(self):
     self.difficulty = Difficulty()
     self.game_status = GameStatus()
     self.random_view = ViewGrid(self.difficulty, self.game_status)
Exemplo n.º 6
0
class TestViewGrid(unittest.TestCase):
    def setUp(self):
        self.difficulty = Difficulty()
        self.game_status = GameStatus()
        self.random_view = ViewGrid(self.difficulty, self.game_status)

    def test_the_view_is_empty_at_the_beginning(self):
        empty = True
        for i in self.random_view.view:
            for j in i:
                if j != " ":
                    empty = False
        self.assertEqual(empty, True)

    def test_if_the_first_openet_square_is_zero(self):
        self.random_view.push_left_button(4, 4)
        content = self.random_view.coordinates(4, 4)
        self.assertEqual(content, "0")

    def test_if_flags_on_mines_and_squares_opened_leads_to_victory(self):
        self.random_view.push_left_button(0, 0)
        mine_locations = self.random_view.grid.give_mines_locations()

        for location in mine_locations:
            self.random_view.push_right_button(location[0], location[1])
        for i in range(self.random_view.height):
            for j in range(self.random_view.width):
                self.random_view.push_left_button(i, j)

        status = self.random_view.give_game_status()
        flags = self.random_view.give_flags()
        unopened = self.random_view.give_unopened()
        self.assertEqual((status.get_status(), flags, unopened),
                         (Status.VICTORY, 0, 0))

    def test_if_the_victory_when_flags_are_putted_last(self):
        self.random_view.push_left_button(0, 0)
        mine_locations = self.random_view.grid.give_mines_locations()

        for i in range(self.random_view.height):
            for j in range(self.random_view.width):
                if (i, j) not in mine_locations:
                    self.random_view.push_left_button(i, j)
        for location in mine_locations:
            self.random_view.push_right_button(location[0], location[1])

        status = self.random_view.give_game_status()
        flags = self.random_view.give_flags()
        unopened = self.random_view.give_unopened()
        self.assertEqual((status.get_status(), flags, unopened),
                         (Status.VICTORY, 0, 0))

    def test_flag_cant_be_opened(self):
        self.random_view.push_right_button(0, 0)
        status1 = self.random_view.coordinates(0, 0)
        self.random_view.push_left_button(0, 0)
        status2 = self.random_view.coordinates(0, 0)
        self.random_view.push_right_button(0, 0)
        status3 = self.random_view.coordinates(0, 0)
        self.random_view.push_left_button(0, 0)
        status4 = self.random_view.coordinates(0, 0)
        self.assertEqual((status1, status2, status3, status4),
                         ("f", "f", " ", "0"))

    def test_flags_cant_be_placed_if_they_all_are_used(self):
        flags = self.random_view.give_flags()
        for i in range(2):
            for j in range(5):
                self.random_view.push_right_button(i, j)
        self.random_view.push_right_button(3, 6)
        status = self.random_view.coordinates(3, 6)

        self.assertEqual((flags, status), (10, " "))

    def test_mines_are_opened_when_game_over(self):
        self.random_view.push_left_button(0, 0)
        mines = self.random_view.grid.give_mines_locations()
        self.random_view.push_left_button(mines[0][0], mines[0][1])
        status_list = []
        for i in range(0, len(mines)):
            status_list.append(
                self.random_view.coordinates(mines[i][0], mines[i][1]))
        print(mines)
        print(status_list)
        self.assertEqual(status_list,
                         ["r", "x", "x", "x", "x", "x", "x", "x", "x", "x"])

    def test_if_difficulties_have_right_sizes_and_amoud_of_mines(self):
        self.random_view.push_left_button(0, 0)
        height1 = self.random_view.height
        width1 = self.random_view.width
        mines1 = len(self.random_view.grid.give_mines_locations())

        self.difficulty.medium()
        self.game_status = GameStatus()
        self.random_view = ViewGrid(self.difficulty, self.game_status)
        self.random_view.push_left_button(0, 0)
        height2 = self.random_view.height
        width2 = self.random_view.width
        mines2 = len(self.random_view.grid.give_mines_locations())

        self.difficulty.hard()
        self.game_status = GameStatus()
        self.random_view = ViewGrid(self.difficulty, self.game_status)
        self.random_view.push_left_button(0, 0)
        height3 = self.random_view.height
        width3 = self.random_view.width
        mines3 = len(self.random_view.grid.give_mines_locations())
        results = (width1, height1, mines1, width2, height2, mines2, width3,
                   height3, mines3)
        self.assertEqual(results, (9, 9, 10, 16, 16, 40, 30, 16, 99))

    def test_if_the_flag_in_a_wrong_places_gives_right_answer(self):
        self.random_view.push_left_button(0, 0)
        mine_locations = self.random_view.grid.give_mines_locations()
        coord_y = None
        coord_x = None
        not_found = True
        while not_found:
            for i in range(self.random_view.height):
                for j in range(self.random_view.width):
                    if not_found and (i, j) not in mine_locations:
                        if self.random_view.coordinates(i, j) == " ":
                            self.random_view.push_right_button(i, j)
                            coord_y = i
                            coord_x = j
                            not_found = False
            if not_found:
                self.game_status = GameStatus()
                self.random_view = ViewGrid(self.difficulty, self.game_status)
        self.random_view.push_left_button(mine_locations[0][0],
                                          mine_locations[0][1])
        result = self.random_view.coordinates(coord_y, coord_x)
        self.assertEqual(result, "w")
Exemplo n.º 7
0
class Ui:
    def __init__(self):
        self.game_view = None
        self.difficulty = Difficulty()
        self.height = self.difficulty.height()
        self.width = self.difficulty.width()

    def start(self):
        self.game_view = ViewGrid(self.difficulty)
        self.play_game()

    def play_game(self):
        print()
        print("miinaharava")
        print(f"size of grid: {self.width} x {self.height}")
        self.main_loop()

    def print_game_viewgrid(self):
        first_line = "  |"
        for i in range(self.width):
            first_line += f" {i} |"
        print(first_line)
        line = "---" + "----" * self.width
        print(line)

        for i in range(self.height):
            row = f"{i} |"
            for j in range(self.width):
                content = self.game_view.coordinates(i, j)
                row += f" {content} |"
            print(row)
            print(line)

    def main_loop(self):
        while True:
            print()
            print("1: new game")
            print("0: quit")
            command = input("command: ")
            if command == "0":
                break
            if command == "1":
                self.game_loop()

    def ask_coordinates(self):
        try:
            row = int(input("give an index of a row: "))
            seg = int(input("give an index of a column: "))
            return (row, seg)
        except TypeError:
            print("invalid command")

    def game_loop(self):
        while True:
            print()
            print("flags left:", self.game_view.give_flags())
            print("unopened squares:", self.game_view.give_unopened())
            print()
            self.print_game_viewgrid()
            if self.status() is False:
                break
            print()
            print("1: open a square")
            print("2: place/displace a flag")
            print("0: exit")
            command = input("command: ")

            if command == "0":
                return
            coord_x, coord_y = self.ask_coordinates()
            if command == "1":
                self.game_view.push_left_button(coord_x, coord_y)
            if command == "2":
                self.game_view.push_right_button(coord_x, coord_y)

    def status(self):
        status = self.game_view.give_game_status()
        if status == "game_over":
            print("game over")
            return False
        if status == "victory":
            print("victory!")
            return False
        return True
Exemplo n.º 8
0
 def start(self):
     self.game_view = ViewGrid(self.difficulty)
     self.play_game()