Exemple #1
0
 def test_get_movable_tiles_1(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14  . 15
     """)
     self.assertEqual([11, 14, 15], board.get_movable_tiles())
Exemple #2
0
 def test_get_movable_tiles_2(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10  . 12
         13 14 11 15
     """)
     self.assertEqual([7, 10, 12, 11], board.get_movable_tiles())
Exemple #3
0
 def test_is_tile_movable_2(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14  . 15
     """)
     self.assertFalse(board.is_tile_movable(12))
Exemple #4
0
 def test_is_tile_movable_1(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14  . 15
     """)
     self.assertTrue(board.is_tile_movable(15))
Exemple #5
0
 def test_is_not_solved(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14  . 15
     """)
     self.assertFalse(board.is_solved())
Exemple #6
0
 def test_is_solved(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14 15  .
     """)
     self.assertTrue(board.is_solved())
Exemple #7
0
 def test_shuffle(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14 15  .
     """)
     board.shuffle()
     self.assertFalse(board.is_solved())
Exemple #8
0
 def test_get_tile(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     self.assertEqual(7, board.get_tile(row=1, col=2))
     self.assertEqual(15, board.get_tile(row=3, col=3))
 def test_mouse_motion__highlight_tile_when_mouse_is_over_that_tile(self):
     board = Board()
     TILE_POS = {'row': 0, 'col': 0}
     tile = board.get_tile(**TILE_POS)
     view = BoardView(board)
     controller = BoardController(board, view)
     mouse_position = view.get_screen_coords_of_tile(**TILE_POS)
     
     self.assertFalse(tile.is_highlighted())
     controller.mouse_motion(mouse_position)
     self.assertTrue(tile.is_highlighted())
 def test_mouse_motion__remove_highlight_when_mouse_leaves_tile_2(self):
     board = Board()
     TILE_POS_1 = {'row': 0, 'col': 1}
     view = BoardView(board)
     controller = BoardController(board, view)
     mouse_position = view.get_screen_coords_of_tile(**TILE_POS_1)
     
     controller.mouse_motion(mouse_position)
     self.assertTrue(board.get_tile(**TILE_POS_1).is_highlighted())
     
     TILE_POS_2 = {'row': 0, 'col': 2}
     mouse_position = view.get_screen_coords_of_tile(**TILE_POS_2)
     controller.mouse_motion(mouse_position)
     self.assertFalse(board.get_tile(**TILE_POS_1).is_highlighted())
     self.assertTrue(board.get_tile(**TILE_POS_2).is_highlighted())
Exemple #11
0
 def test_move_5(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     board.move(15)
     expected = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 15
         13 14 12  .
     """)
     self.assertEqual(str(expected), str(board))
Exemple #12
0
 def test_move_4(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14 15  .
     """)
     board.move(1)
     expected = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 12
         13 14 15  .
     """)
     self.assertEqual(str(expected), str(board))
Exemple #13
0
class Game:
    def __init__(self):
        self._board = Board()
        
    def run(self):
        clock = pygame.time.Clock()
        pygame.init()
        screen = pygame.display.set_mode(WINSIZE)
        backbuffer = pygame.Surface(screen.get_size(), pygame.SRCALPHA, 32)
        pygame.display.set_caption('Fifteen')
        
        board = self._board
        board.shuffle()
        board_view = BoardView(board, x=80, y=80)
        board_controller = BoardController(board, board_view)
        game_over_msg = GameOverMessage()
        play_again_btn = Button(title='Play Again', x=80, y=340)
        play_again_btn.attach(self)
    
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == MOUSEMOTION:
                    if not board.is_solved():
                        board_controller.mouse_motion(event.pos)
                    else:
                        play_again_btn.mouse_motion(event.pos)
                elif event.type == MOUSEBUTTONDOWN:
                    if not board.is_solved():
                        board_controller.mouse_click(event.pos)
                    else:
                        play_again_btn.mouse_click(event.pos)
            
            backbuffer.fill((0, 0, 0))
            board_view.draw(backbuffer)
            if (board.is_solved()):
                game_over_msg.draw(backbuffer)
                play_again_btn.draw(backbuffer)
            
            screen.blit(backbuffer, (0, 0))
            pygame.display.flip()
            clock.tick(30)
    
    def update(self, subject):
        if isinstance(subject, Button):
            self._board.shuffle()
Exemple #14
0
 def __init__(self):
     self._board = Board()