예제 #1
0
 def setUp(self):
     self.grid = [
         [0, 2, 4, 0],
         [2, 2, 4, 4],
         [2, 0, 2, 2],
     ]
     self.game = Game(board=self.grid)
예제 #2
0
 def test_add_new_value_to_empty_spot(self):
     grid = [
         [0, 2],
         [2, 2],
     ]
     game = Game(board=grid)
     empty = game.find_empty()
     game.add_new_tile(empty)
     self.assertIn(game.board[0][0], (2, 4))
예제 #3
0
 def test_find_empty_spots_to_add_new_tiles_in(self):
     grid = [
         [0, 2],
         [2, 2],
         [2, 0],
     ]
     game = Game(board=grid)
     expected = [(0, 0), (2, 1)]
     result = game.find_empty()
     self.assertEqual(expected, result)
예제 #4
0
 def initialize_game(self):
     default = (4, 4)
     size = input(
         'What size of game board do you want? \nFormat: x, y  Example: 2, 3\n(press Enter for default of 4x4) >>'
     )
     x, y = [int(q.strip()) for q in size.split(',')] if size else default
     self.game = Game(width=x, height=y)
     amount = int(x * y / 3) or 1
     for _ in range(amount):
         empty = self.game.find_empty()
         self.game.add_new_tile(empty)
     self.display_board()
예제 #5
0
class GameTests(unittest.TestCase):
    def setUp(self):
        self.grid = [[2, 2, 0, 0], [0, 0, 4, 0], [8, 8, 0, 16]]
        self.game = Game(board=self.grid)

    def test_game_initializes_with_blank_board_of_right_size_if_one_not_provided(
            self):
        game = Game(width=2, height=3)
        expected = [
            [0, 0],
            [0, 0],
            [0, 0],
        ]
        self.assertEqual(expected, game.board)

    def test_initializes_with_passed_in_board_and_sets_width_and_height(self):
        self.assertEqual(self.game.board, self.grid)
        self.assertEqual(self.game.width, 4)
        self.assertEqual(self.game.height, 3)

    def test_shift_left(self):
        expected = [[4, 0, 0, 0], [4, 0, 0, 0], [16, 16, 0, 0]]
        self.game.take_turn('left')
        self.assertEqual(expected, self.game.board)

    def test_shift_right(self):
        expected = [[0, 0, 0, 4], [0, 0, 0, 4], [0, 0, 16, 16]]
        self.game.take_turn('right')
        self.assertEqual(expected, self.game.board)

    def test_shift_up(self):
        expected = [[2, 2, 4, 16], [8, 8, 0, 0], [0, 0, 0, 0]]
        self.game.take_turn('up')
        self.assertEqual(expected, self.game.board)

    def test_shift_down(self):
        expected = [[0, 0, 0, 0], [2, 2, 0, 0], [8, 8, 4, 16]]
        self.game.take_turn('down')
        self.assertEqual(expected, self.game.board)

    def test_find_empty_spots_to_add_new_tiles_in(self):
        grid = [
            [0, 2],
            [2, 2],
            [2, 0],
        ]
        game = Game(board=grid)
        expected = [(0, 0), (2, 1)]
        result = game.find_empty()
        self.assertEqual(expected, result)

    def test_add_new_value_to_empty_spot(self):
        grid = [
            [0, 2],
            [2, 2],
        ]
        game = Game(board=grid)
        empty = game.find_empty()
        game.add_new_tile(empty)
        self.assertIn(game.board[0][0], (2, 4))
예제 #6
0
 def test_display_board_displays_game_with_different_dimensions(self):
     grid = [
         [0, 2],
         [2, 2],
         [2, 0],
     ]
     game = Game(board=grid)
     expected = '\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\n'
     expected += '\u2502  0  \u2502  2  \u2502\n'
     expected += '\u251c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2524\n'
     expected += '\u2502  2  \u2502  2  \u2502\n'
     expected += '\u251c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2524\n'
     expected += '\u2502  2  \u2502  0  \u2502\n'
     expected += '\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\n'
     self.assertEqual(expected, game.display_board())
예제 #7
0
 def test_game_initializes_with_blank_board_of_right_size_if_one_not_provided(
         self):
     game = Game(width=2, height=3)
     expected = [
         [0, 0],
         [0, 0],
         [0, 0],
     ]
     self.assertEqual(expected, game.board)
예제 #8
0
class Cli2048GridTests(unittest.TestCase):
    def setUp(self):
        self.grid = [
            [0, 2, 4, 0],
            [2, 2, 4, 4],
            [2, 0, 2, 2],
        ]
        self.game = Game(board=self.grid)

    def test_display_board_displays_game_object_correctly(self):
        expected = '┌─────┬─────┬─────┬─────┐\n'
        expected += '│  0  │  2  │  4  │  0  │\n'
        expected += '├─────┼─────┼─────┼─────┤\n'
        expected += '│  2  │  2  │  4  │  4  │\n'
        expected += '├─────┼─────┼─────┼─────┤\n'
        expected += '│  2  │  0  │  2  │  2  │\n'
        expected += '└─────┴─────┴─────┴─────┘\n'
        self.assertEqual(expected, self.game.display_board())

    def test_display_board_displays_game_with_different_dimensions(self):
        grid = [
            [0, 2],
            [2, 2],
            [2, 0],
        ]
        game = Game(board=grid)
        expected = '\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\n'
        expected += '\u2502  0  \u2502  2  \u2502\n'
        expected += '\u251c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2524\n'
        expected += '\u2502  2  \u2502  2  \u2502\n'
        expected += '\u251c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2524\n'
        expected += '\u2502  2  \u2502  0  \u2502\n'
        expected += '\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\n'
        self.assertEqual(expected, game.display_board())

    def test_str_returns_game_board(self):
        self.assertEqual(str(self.grid), str(self.game))
예제 #9
0
class GameLoop(cmd.Cmd):

    welcome = "Command Line 2048 "
    prompt = '>>> '

    def preloop(self):
        print(self.welcome)
        self.initialize_game()

    def initialize_game(self):
        default = (4, 4)
        size = input(
            'What size of game board do you want? \nFormat: x, y  Example: 2, 3\n(press Enter for default of 4x4) >>'
        )
        x, y = [int(q.strip()) for q in size.split(',')] if size else default
        self.game = Game(width=x, height=y)
        amount = int(x * y / 3) or 1
        for _ in range(amount):
            empty = self.game.find_empty()
            self.game.add_new_tile(empty)
        self.display_board()

    def display_board(self):
        print(self.game.display_board())

    def do_h(self, args):
        'Shift tiles left'
        self.game.take_turn('left')

    def do_left(self, args):
        self.do_h(args)

    def do_l(self, args):
        'Shift tiles right'
        self.game.take_turn('right')

    def do_right(self, args):
        self.do_l(args)

    def do_j(self, args):
        'Shift tiles up'
        self.game.take_turn('up')

    def do_up(self, args):
        self.do_j(args)

    def do_k(self, args):
        'Shift tiles down'
        self.game.take_turn('down')

    def do_down(self, args):
        self.do_k(args)

    def do_q(self, args):
        return True

    def do_quit(self, args):
        return True

    def postcmd(self, stop, line):
        if line in ('q', 'quit'):
            return True
        empty = self.game.find_empty()
        if not empty:
            return self.lose_game()
        self.game.add_new_tile(empty)
        self.display_board()
        return stop

    def lose_game(self):
        print('You have failed!!! ')
        return True
예제 #10
0
 def setUp(self):
     self.grid = [[2, 2, 0, 0], [0, 0, 4, 0], [8, 8, 0, 16]]
     self.game = Game(board=self.grid)