Ejemplo n.º 1
0
 def test_add_random_tile_one_empty_tile(self):
     """
     add_random_tile shall not mofify a full board
     """
     board = [[8, 16, 16, 32], [8, 16, 32, 1024], [128, 16, 0, 16],
              [8, 256, 16, 64]]
     add_random_tile(board)
     self.assertIn(board[2][2], INITIAL_VALUES)
Ejemplo n.º 2
0
    def test_add_random_tile_full_board(self):
        """
        add_random_tile shall not modify a full board
        """

        board = [[8, 512, 16, 32], [8, 16, 32, 1024], [128, 64, 16, 16],
                 [8, 256, 16, 64]]
        expected_board = [[8, 512, 16, 32], [8, 16, 32, 1024],
                          [128, 64, 16, 16], [8, 256, 16, 64]]

        tile_count = 0
        add_random_tile(board)
        self.assertEqual(board, expected_board)
Ejemplo n.º 3
0
 def test_add_random_tile_partial_board(self):
     board = [[8, 16, 0, 32], [8, 16, 0, 1024], [128, 16, 0, 16],
              [0, 256, 16, 64]]
     original_board = [[8, 16, 0, 32], [8, 16, 0, 1024], [128, 16, 0, 16],
                       [0, 256, 16, 64]]
     add_random_tile(board)
     new_tile_count = 0
     for row in range(SIZE):
         for col in range(SIZE):
             if board[row][col] != original_board[row][col]:
                 new_tile_count += 1
                 self.assertIn(board[row][col], INITIAL_VALUES)
     self.assertEqual(new_tile_count, 1)
Ejemplo n.º 4
0
    def test_add_random_tile_empty_board(self):
        """
        add_random_tile shall only add one tile to an empty board
        """

        board = [[0 for _ in range(SIZE)] for _ in range(SIZE)]
        tile_count = 0
        add_random_tile(board)
        for row in range(SIZE):
            for col in range(SIZE):
                if board[row][col] != 0:
                    tile_count += 1

        self.assertEqual(tile_count, 1)
Ejemplo n.º 5
0
def main(stdscr):

    init_curses()

    # centering
    height, width = stdscr.getmaxyx()

    offset_x = int(width // 2) - int(BOARD_WIDTH / 2)
    offset_y = int(height // 2) - int(BOARD_HEIGHT / 2)

    message_y = offset_y + BOARD_HEIGHT + 1

    score_y = offset_y - 1

    exit_message_x = int(width // 2) - int(len(EXIT_MESSAGE) // 2)
    end_message_x = int(width // 2) - int(len(END_MESSAGE) // 2)
    won_message_x = int(width // 2) - int(len(WON_MESSAGE) // 2)
    lost_message_x = int(width // 2) - int(len(LOST_MESSAGE) // 2)

    board = initialize_board()
    paint_board(stdscr, board, offset_y, offset_x)

    while True:

        game_over, game_won, score = is_game_over(board)

        if game_won:
            stdscr.addstr(message_y, won_message_x, WON_MESSAGE)
            stdscr.addstr(message_y + 2, end_message_x, END_MESSAGE)
            while True:
                c = stdscr.getch()
                if c == ord('q'):
                    return

        if game_over:
            stdscr.addstr(message_y, lost_message_x, LOST_MESSAGE)
            stdscr.addstr(message_y + 2, end_message_x, END_MESSAGE)
            while True:
                c = stdscr.getch()
                if c == ord('q'):
                    return

        stdscr.addstr(score_y, offset_x, f"Current Score: {score}")

        c = stdscr.getch()
        moved_tiles = False

        if c == curses.KEY_UP:
            moved_tiles = move_up(board)
        elif c == curses.KEY_DOWN:
            moved_tiles = move_down(board)
        elif c == curses.KEY_LEFT:
            moved_tiles = move_left(board)
        elif c == curses.KEY_RIGHT:
            moved_tiles = move_right(board)
        elif c == ord('q'):
            stdscr.addstr(message_y, exit_message_x, EXIT_MESSAGE)
            if stdscr.getch() == ord('y'):
                return
            stdscr.addstr(message_y, exit_message_x, " " * len(EXIT_MESSAGE))

        if moved_tiles:
            add_random_tile(board)
            paint_board(stdscr, board, offset_y, offset_x)