Exemple #1
0
 def test_is_game_over(self):
     '''
     The game ends when a faller freezes but cannot be displayed entirely in the field because it didn't move down far enough.
     '''
     takein_list1 = [['   ', ' Y ', ' Y ', ' Y ', ' X ', ' X ', ' X '],
                     ['   ', '   ', '   ', '   ', '   ', '   ', '   '],
                     ['   ', '   ', '   ', '   ', '   ', '   ', '   '],
                     ['   ', '   ', '   ', '   ', '   ', '   ', '   ']]
     takein_list2 = [['   ', '   ', '   ', ' X ', ' X ', ' X ', ' X '],
                     ['   ', '   ', '   ', '   ', '   ', '   ', '   '],
                     ['   ', '   ', '   ', '   ', '   ', '   ', '   '],
                     ['   ', '   ', '   ', '   ', '   ', '   ', '   ']]
     self.assertEqual(True, game_logic.is_game_over(takein_list1))
     self.assertEqual(False, game_logic.is_game_over(takein_list2))
Exemple #2
0
 def test_game_is_not_won(self):
     """
     Tests that false is returned since tile 2048 is not present in board
     """
     board = [[2, 4, 8, 16], [16, 8, 4, 2], [32, 2, 128, 256],
              [256, 128, 64, 32]]
     _, won, _ = is_game_over(board)
     self.assertFalse(won)
Exemple #3
0
 def test_game_is_not_over(self):
     """
     Tests that false is returned since moves can still be made
     """
     board = [[32, 16, 16, 0], [16, 64, 8, 4], [4, 2, 2, 4],
              [8, 128, 2, 256]]
     over, _, _ = is_game_over(board)
     self.assertFalse(over)
Exemple #4
0
 def test_game_score(self):
     """
     Tests that the correct score is returned based on the sum of the tiles in board
     """
     expected_score = 3004
     board = [[2, 4, 8, 16], [16, 8, 4, 2], [32, 2048, 128, 256],
              [256, 128, 64, 32]]
     _, _, score = is_game_over(board)
     self.assertEqual(expected_score, score)
Exemple #5
0
 def test_game_is_over(self):
     """
     Tests the true is returned since game is over, no more moves can be made
     :return:
     """
     board = [[2, 4, 8, 16], [16, 8, 4, 2], [32, 64, 128, 256],
              [256, 128, 64, 32]]
     over, _, _ = is_game_over(board)
     self.assertTrue(over)
Exemple #6
0
 def test_game_complete(self):
     """
     Tests that the all 3 returns are correct; game_over, game_won, and score
     """
     expected_score = 3004
     board = [[2, 4, 8, 16], [16, 8, 4, 2], [32, 2048, 128, 256],
              [256, 128, 64, 32]]
     over, won, score = is_game_over(board)
     self.assertTrue(over)
     self.assertTrue(won)
     self.assertEqual(expected_score, score)
Exemple #7
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)