Example #1
0
 def test_show_cursor_windows(self):
     g = Game(scores_file=None, store_file=None, clear_screen=True)
     self.setWindows(g)
     g.hideCursor()
     g.showCursor()
     # these don't do anything for now
     self.assertEqual(sys.stdout.read(), '')
Example #2
0
 def test_save_best_score_with_file(self):
     scores_file = NamedTemporaryFile(delete=True)
     g = Game(scores_file=scores_file.name)
     g.best_score = 0
     g.score = 1000
     g.saveBestScore()
     self.assertEqual(g.best_score, 1000)
Example #3
0
 def test_store_non_empty_file(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     g = Game(scores_file=None, store_file=store.name)
     self.assertTrue(g.store())
     self.assertFileIsNotEmpty(store.name)
     remove(store.name)
Example #4
0
 def test_store_non_empty_file(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     g = Game(scores_file=None, store_file=store.name)
     self.assertTrue(g.store())
     self.assertFileIsNotEmpty(store.name)
     remove(store.name)
Example #5
0
 def test_simple_win_loop(self):
     kp._setNextKey(kp.UP)
     g = Game(goal=4, size=2, clear_screen=False)
     g.board.cells = [
         [2, 0],
         [2, 0]
     ]
     g.loop()
Example #6
0
 def test_store_fail_return_false(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     os.chmod(store.name, 0)  # no rights at all
     g = Game(scores_file=None, store_file=store.name)
     self.assertFalse(g.store())
     os.chmod(store.name, 0o200)  # give me writing rights back
     remove(store.name)
Example #7
0
 def test_store_fail_return_false(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     os.chmod(store.name, 0)  # no rights at all
     g = Game(scores_file=None, store_file=store.name)
     self.assertFalse(g.store())
     os.chmod(store.name, 0o200)  # give me writing rights back
     remove(store.name)
Example #8
0
    def test_start_game_no_resume(self):
        g1 = Game(scores_file=None)
        g1.board.setCell(0, 0, 16)
        self.assertTrue(g1.store())

        sys.argv = ['term2048']
        g2 = ui.start_game(debug=True)
        self.assertIn(g2.board.getCell(0, 0), [0, 2, 4])
Example #9
0
    def test_start_game_no_resume(self):
        g1 = Game(scores_file=None)
        g1.board.setCell(0, 0, 16)
        self.assertTrue(g1.store())

        sys.argv = ['term2048']
        g2 = ui.start_game(debug=True)
        self.assertIn(g2.board.getCell(0, 0), [0, 2, 4])
Example #10
0
 def test_loop_pause(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     kp._setNextKey(kp.SPACE)
     g = Game(scores_file=None, store_file=store.name)
     self.assertEqual(g.loop(), 0)
     self.assertFileIsNotEmpty(store.name)
     remove(store.name)
Example #11
0
 def test_loop_pause(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     kp._setNextKey(kp.SPACE)
     g = Game(scores_file=None, store_file=store.name)
     self.assertEqual(g.loop(), 0)
     self.assertFileIsNotEmpty(store.name)
     remove(store.name)
Example #12
0
 def test_simple_win_loop(self):
     kp._setNextKey(kp.UP)
     g = Game(goal=4, size=2, clear_screen=False)
     g.board.cells = [
         [2, 0],
         [2, 0]
     ]
     g.loop()
Example #13
0
 def test_loop_pause_error(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     os.chmod(store.name, 0)  # no rights at all
     kp._setNextKey(kp.SPACE)
     g = Game(scores_file=None, store_file=store.name)
     self.assertIs(None, g.loop())
     os.chmod(store.name, 0o200)  # give me writing rights back
     remove(store.name)
Example #14
0
 def test_loop_pause_error(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     os.chmod(store.name, 0)  # no rights at all
     kp._setNextKey(kp.SPACE)
     g = Game(scores_file=None, store_file=store.name)
     self.assertIs(None, g.loop())
     os.chmod(store.name, 0o200)  # give me writing rights back
     remove(store.name)
Example #15
0
 def test_simple_win_loop_clear(self):
     kp._setNextKey(kp.UP)
     g = Game(goal=4, size=2, scores_file=None)
     g.board.cells = [[2, 0], [2, 0]]
     self.assertEqual(g.loop(), 4)
     if os.name == 'nt':
         self.assertEqual(self.sys_cmd, ('cls', ))
     else:
         self.assertEqual(self.sys_cmd, ('clear', ))
Example #16
0
 def test_store_restore_empty_game(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     g1 = Game(scores_file=None, store_file=store.name)
     self.assertTrue(g1.store())
     g2 = Game(scores_file=None, store_file=store.name)
     g2.board.setCell(0, 0, 16)
     self.assertTrue(g2.restore())
     self.assertIn(g2.board.getCell(0, 0), [0, 2, 4])
     remove(store.name)
Example #17
0
    def test_start_game_resume(self):
        cellvalue = 2
        g1 = Game(scores_file=None)
        g1.board.setCell(0, 0, cellvalue)
        g1.score = 42
        self.assertTrue(g1.store())

        sys.argv = ['term2048', '--resume']
        g2 = ui.start_game(debug=True)
        self.assertEqual(cellvalue, g2.board.getCell(0, 0))
        self.assertEqual(g1.score, g2.score)
Example #18
0
    def test_start_game_resume(self):
        cellvalue = 2
        g1 = Game(scores_file=None)
        g1.board.setCell(0, 0, cellvalue)
        g1.score = 42
        self.assertTrue(g1.store())

        sys.argv = ['term2048', '--resume']
        g2 = ui.start_game(debug=True)
        self.assertEqual(cellvalue, g2.board.getCell(0, 0))
        self.assertEqual(g1.score, g2.score)
Example #19
0
 def test_simple_win_loop_clear(self):
     kp._setNextKey(kp.UP)
     g = Game(goal=4, size=2)
     g.board.cells = [
         [2, 0],
         [2, 0]
     ]
     self.assertEqual(g.loop(), 4)
     if os.name == 'nt':
         self.assertEqual(self.sys_cmd, ('cls',))
     else:
         self.assertEqual(self.sys_cmd, ('clear',))
Example #20
0
def run_game(q1, q2):
    while True:
        g = Game()
        while g.board.canMove():
            # m = randomMove.next_move()
            # m = greedyMove.next_move(g.board)
            m = nStepLookAhead.next_move(g.board,5)
            # m = expectimaxMove.next_move(g.board)
            g.incScore(g.board.move(m))

        if g.board.won():
            print("\n")
            print(g)
        q1.put(g.score)
        q2.put(g.board.won())
Example #21
0
    def setUp(self):
        def fake_system(*cmd):
            self.sys_cmd = cmd

        Board.SIZE = _BSIZE
        Game.SCORES_FILE = None
        self.g = Game(scores_file=None, store_file=None)
        self.b = self.g.board
        # don't print anything on stdout
        self.stdout = sys.stdout
        sys.stdout = DevNull()
        # mock os.system
        self.system = os.system
        self.sys_cmd = None
        os.system = fake_system
Example #22
0
    def test_init_with_local_scores_file_fail(self):
        scores_file = NamedTemporaryFile(delete=False)
        scores_file.close()

        g = Game(scores_file=scores_file.name)

        remove(scores_file.name)
Example #23
0
 def setUp(self):
     Board.SIZE = _BSIZE
     Game.SCORES_FILE = None
     self.g = Game(scores_file=None)
     self.b = self.g.board
     # don't print anything on stdout
     self.stdout = sys.stdout
     sys.stdout = DevNull()
Example #24
0
	def __init__(self, iterations=1, print_interval=1, delay=0, heuristic='most_empty', **kws):
		self.delay = delay
		self.heuristic = getattr(self, heuristic)
		total_won, total_score, start_time = 0, 0, time()

		for i in range(1, iterations):
			game = Game(self.get_move, **kws)
			self.board = game.board
			won, score = game.play()
			total_score += score
			total_won += won

			if i % print_interval == 0:
				system('cls')
				print 'iterations completed: ' + str(i)
				print 'simulations per second: ' + str(i / (time() - start_time))
				print 'average score: ' + str(total_score / i)
				print 'total won: ' + str(total_won)
				print 'proportion won: ' + str(total_won / i)
Example #25
0
    def test_init_with_local_scores_file(self):
        s = 4241
        scores_file = NamedTemporaryFile(delete=False)
        scores_file.write(str(s).encode())
        scores_file.close()

        g = Game(scores_file=scores_file.name)
        self.assertEqual(g.best_score, s)

        remove(scores_file.name)
Example #26
0
def start_ai():
    #make a game to use
    g = Game(scores_file='./score.txt')
    #start the ai loop
    numMoves = 0
    try:
        while True:
            numMoves += 1
            print "\nMove %d\n" % numMoves
            print g.__str__(margins={'left': 4, 'top': 1, 'bottom': 1})
            if g.board.won() or not g.board.canMove():
                break
            m = chooseMove(g)
            g.incScore(g.board.move(m))
        g.saveBestScore()
    except KeyboardInterrupt:
        g.saveBestScore()
Example #27
0
def start_game(debug=False):
    """
    Start a new game. If ``debug`` is set to ``True``, the game object is
    returned and the game loop isn't fired.
    """
    args = parse_cli_args()

    if args['version']:
        print_version_and_exit()

    if args['rules']:
        print_rules_and_exit()

    game = Game(**args)
    if args['resume']:
        game.restore()

    if debug:
        return game

    return game.loop()
Example #28
0
def start_game():
    """start a new game"""
    args = parse_cli_args()

    if args['version']:
        print_version_and_exit()

    if args['rules']:
        print_rules_and_exit()

    if not debug:
        Game(**args).loop()
Example #29
0
def start_game(debug=False):
    """
    Start a new game. If ``debug`` is set to ``True``, the game object is
    returned and the game loop isn't fired.
    """
    args = parse_cli_args()

    if args['version']:
        print_version_and_exit()

    if args['rules']:
        print_rules_and_exit()

    game = Game(**args)
    if args['resume']:
        game.restore()

    if debug:
        return game

    return game.loop()
Example #30
0
 def test_show_cursor_windows(self):
     g = Game(scores_file=None, store_file=None, clear_screen=True)
     self.setWindows(g)
     g.hideCursor()
     g.showCursor()
     # these don't do anything for now
     self.assertEqual(sys.stdout.read(), '')
Example #31
0
def start_game():
    """start a new game"""
    if not __has_argparse:
        __print_argparse_warning()
        args = {}
    else:
        args = parse_cli_args()

        if args['version']:
            print_version_and_exit()

    if not debug:
        Game(**args).loop()
Example #32
0
 def test_save_best_score_with_file(self):
     s = 1000
     scores_file = NamedTemporaryFile(delete=True)
     g = Game(scores_file=scores_file.name)
     g.best_score = 0
     g.score = s
     g.saveBestScore()
     self.assertEqual(g.best_score, s)
Example #33
0
    def setUp(self):
        Board.SIZE = _BSIZE
        Game.SCORES_FILE = None
        self.g = Game(scores_file=None, store_file=None)
        self.b = self.g.board
        # don't print anything on stdout
        self.stdout = sys.stdout
        sys.stdout = DevNull()
        # mock os.system
        self.system = os.system
        self.sys_cmd = None
        def fake_system(*cmd):
            self.sys_cmd = cmd

        os.system = fake_system
Example #34
0
 def test_store_restore_empty_game(self):
     store = NamedTemporaryFile(delete=False)
     store.close()
     g1 = Game(scores_file=None, store_file=store.name)
     self.assertTrue(g1.store())
     g2 = Game(scores_file=None, store_file=store.name)
     g2.board.setCell(0, 0, 16)
     self.assertTrue(g2.restore())
     self.assertIn(g2.board.getCell(0, 0), [0, 2, 4])
     remove(store.name)
Example #35
0
class TestGame(unittest.TestCase):

    def setUp(self):
        Board.SIZE = _BSIZE
        Game.SCORES_FILE = None
        self.g = Game(scores_file=None)
        self.b = self.g.board

    def test_init_with_size_3_goal_4(self):
        g = Game(size=3, goal=4, scores_file=None)
        self.assertEqual(g.board.size(), 3)

    # == .saveBestScore == #

    def test_save_best_score_no_file(self):
        self.g.score = 42
        self.g.saveBestScore()
        self.assertEqual(self.g.best_score, 42)

    # == .end == #

    def test_end_can_play(self):
        self.assertFalse(self.g.end())

    # == .getCellStr == #

    def test_getCellStr_unknown_number(self):
        self.b.setCell(0, 0, 0)
        self.assertEqual(self.g.getCellStr(0, 0), '  .')

    def test_getCellStr_unknown_number(self):
        self.b.setCell(0, 0, 42)
        self.assertEqual(self.g.getCellStr(0, 0),
                '%s 42%s' % (Fore.RESET, Fore.RESET))

    # == .boardToString == #

    def test_boardToString_height_no_margins(self):
        s = self.g.boardToString()
        self.assertEqual(len(s.split("\n")), self.b.size())

    # == .__str__ == #

    def test_str_height_no_margins(self):
        s = str(self.g)
        self.assertEqual(len(s.split("\n")), self.b.size())
Example #36
0
 def test_getCellStr_1k(self):
     g = Game()
     g.board.setCell(0, 0, 1024)
     self.assertRegexpMatches(g.getCellStr(0, 0), r' 1k\x1b\[0m$')
Example #37
0
 def test_loop_interrupt(self):
     kp._setCtrlC(True)
     g = Game(goal=4, size=2, scores_file=None)
     self.assertEqual(g.loop(), None)
Example #38
0
 def test_getCellStr_2k(self):
     g = Game()
     g.board.setCell(0, 0, 2048)
     self.assertRegexpMatches(g.getCellStr(0, 0), r' 2k\x1b\[0m$')
Example #39
0
 def test_show_cursor_no_clear_screen_linux(self):
     g = Game(scores_file=None, store_file=None, clear_screen=False)
     self.setWindows(g, False)
     g.hideCursor()
     g.showCursor()
     self.assertEqual(sys.stdout.read(), '\033[?25h')
Example #40
0
class TestGame(unittest.TestCase):
    def setUp(self):
        def fake_system(*cmd):
            self.sys_cmd = cmd

        Board.SIZE = _BSIZE
        Game.SCORES_FILE = None
        self.g = Game(scores_file=None, store_file=None)
        self.b = self.g.board
        # don't print anything on stdout
        self.stdout = sys.stdout
        sys.stdout = DevNull()
        # mock os.system
        self.system = os.system
        self.sys_cmd = None
        os.system = fake_system

    def tearDown(self):
        sys.stdout = self.stdout
        os.system = self.system
        kp._setCtrlC(False)

    def setWindows(self, game, isWin=True):
        setattr(game, '_Game__is_windows', isWin)

    def assertFileIsNotEmpty(self, path):
        with open(path, 'r') as f:
            f.seek(0, 2)
            size = f.tell()
        self.assertNotEqual(0, size)

    # == .init == #

    def test_init_with_size_3_goal_4(self):
        g = Game(size=3, goal=4, scores_file=None)
        self.assertEqual(g.board.size(), 3)

    # == .saveBestScore == #

    def test_save_best_score_no_file(self):
        s = 42
        self.g.score = s
        self.g.saveBestScore()
        self.assertEqual(self.g.best_score, s)

    def test_save_best_score_with_file(self):
        s = 1000
        scores_file = NamedTemporaryFile(delete=True)
        g = Game(scores_file=scores_file.name)
        g.best_score = 0
        g.score = s
        g.saveBestScore()
        self.assertEqual(g.best_score, s)

    # == .loadBestScore == #

    def test_init_with_local_scores_file(self):
        s = 4241
        scores_file = NamedTemporaryFile(delete=False)
        scores_file.write(str(s).encode())
        scores_file.close()

        g = Game(scores_file=scores_file.name)
        self.assertEqual(g.best_score, s)

        remove(scores_file.name)

    def test_init_with_local_scores_file_fail(self):
        scores_file = NamedTemporaryFile(delete=False)
        scores_file.close()

        g = Game(scores_file=scores_file.name)
        self.assertEqual(g.best_score, 0)

        remove(scores_file.name)

    # == .incScore == #

    def test_inc_0_score(self):
        s = 3
        self.g.score = s
        self.g.best_score = s
        self.g.incScore(0)
        self.assertEqual(self.g.score, s)
        self.assertEqual(self.g.best_score, s)

    def test_inc_2_score(self):
        s = 3
        i = 2
        self.g.score = s
        self.g.best_score = s
        self.g.incScore(i)
        self.assertEqual(self.g.score, s + i)
        self.assertEqual(self.g.best_score, s + i)

    def test_inc_score_update_best_score(self):
        s = 3
        i = 2
        self.g.score = s
        self.g.best_score = 0
        self.g.incScore(i)
        self.assertEqual(self.g.score, s + i)
        self.assertEqual(self.g.best_score, s + i)

    def test_inc_score_dont_update_best_score_if_higher(self):
        s = 3
        bs = 80
        i = 2
        self.g.score = s
        self.g.best_score = bs
        self.g.incScore(i)
        self.assertEqual(self.g.score, s + i)
        self.assertEqual(self.g.best_score, bs)

    # == .store/.restore == #

    def test_store_non_empty_file(self):
        store = NamedTemporaryFile(delete=False)
        store.close()
        g = Game(scores_file=None, store_file=store.name)
        self.assertTrue(g.store())
        self.assertFileIsNotEmpty(store.name)
        remove(store.name)

    def test_store_fail_return_false(self):
        store = NamedTemporaryFile(delete=False)
        store.close()
        os.chmod(store.name, 0)  # no rights at all
        g = Game(scores_file=None, store_file=store.name)
        self.assertFalse(g.store())
        os.chmod(store.name, 0o200)  # give me writing rights back
        remove(store.name)

    def test_store_restore_empty_game(self):
        store = NamedTemporaryFile(delete=False)
        store.close()
        g1 = Game(scores_file=None, store_file=store.name)
        self.assertTrue(g1.store())
        g2 = Game(scores_file=None, store_file=store.name)
        g2.board.setCell(0, 0, 16)
        self.assertTrue(g2.restore())
        self.assertIn(g2.board.getCell(0, 0), [0, 2, 4])
        remove(store.name)

    def test_restore_fail_return_false(self):
        store_name = '/i/dont/%s/exist/%s' % (uuid4(), uuid4())
        g = Game(scores_file=None, store_file=store_name)
        self.assertFalse(g.restore())

    # == .readMove == #

    def test_read_unknown_move(self):
        kp._setNextKey(-1)
        self.assertEqual(self.g.readMove(), None)

    def test_read_known_move(self):
        kp._setNextKey(kp.LEFT)
        self.assertEqual(self.g.readMove(), Board.LEFT)

    # == .clear == #

    def test_clear_with_no_clear_screen(self):
        g = Game(scores_file=None, store_file=None, clear_screen=False)
        g.clearScreen()
        self.assertEqual(sys.stdout.read(), '\n\n')  # \n + print's \n

    # == .hideCursor == #

    def test_hide_cursor_linux(self):
        g = Game(scores_file=None, store_file=None, clear_screen=True)
        self.setWindows(g, False)
        g.hideCursor()
        self.assertEqual(sys.stdout.read(), '\033[?25l')
        g.showCursor()

    def test_hide_cursor_windows(self):
        g = Game(scores_file=None, store_file=None, clear_screen=True)
        self.setWindows(g)
        g.hideCursor()
        # this doesn't do anything for now
        self.assertEqual(sys.stdout.read(), '')
        g.showCursor()

    def test_hide_cursor_no_clear_screen_linux(self):
        g = Game(scores_file=None, store_file=None, clear_screen=False)
        self.setWindows(g, False)
        g.hideCursor()
        self.assertEqual(sys.stdout.read(), '')

    def test_hide_cursor_no_clear_screen_windows(self):
        g = Game(scores_file=None, store_file=None, clear_screen=False)
        self.setWindows(g)
        g.hideCursor()
        self.assertEqual(sys.stdout.read(), '')

    # == .showCursor == #

    def test_show_cursor_linux(self):
        g = Game(scores_file=None, store_file=None, clear_screen=True)
        self.setWindows(g, False)
        g.hideCursor()
        g.showCursor()
        self.assertEqual(sys.stdout.read(), '\033[?25l\033[?25h')

    def test_show_cursor_windows(self):
        g = Game(scores_file=None, store_file=None, clear_screen=True)
        self.setWindows(g)
        g.hideCursor()
        g.showCursor()
        # these don't do anything for now
        self.assertEqual(sys.stdout.read(), '')

    def test_show_cursor_no_clear_screen_linux(self):
        g = Game(scores_file=None, store_file=None, clear_screen=False)
        self.setWindows(g, False)
        g.hideCursor()
        g.showCursor()
        self.assertEqual(sys.stdout.read(), '\033[?25h')

    # == .loop == #

    def test_simple_win_loop(self):
        kp._setNextKey(kp.UP)
        g = Game(goal=4, size=2, clear_screen=False)
        g.board.cells = [[2, 0], [2, 0]]
        self.assertEqual(4, g.loop())

    def test_simple_win_loop_clear(self):
        kp._setNextKey(kp.UP)
        g = Game(goal=4, size=2, scores_file=None)
        g.board.cells = [[2, 0], [2, 0]]
        self.assertEqual(g.loop(), 4)
        if os.name == 'nt':
            self.assertEqual(self.sys_cmd, ('cls', ))
        else:
            self.assertEqual(self.sys_cmd, ('clear', ))

    def test_loop_interrupt(self):
        kp._setCtrlC(True)
        g = Game(goal=4, size=2, scores_file=None)
        self.assertEqual(g.loop(), None)

    def test_loop_pause(self):
        store = NamedTemporaryFile(delete=False)
        store.close()
        kp._setNextKey(kp.SPACE)
        g = Game(scores_file=None, store_file=store.name)
        self.assertEqual(g.loop(), 0)
        self.assertFileIsNotEmpty(store.name)
        remove(store.name)

    def test_loop_pause_error(self):
        store = NamedTemporaryFile(delete=False)
        store.close()
        os.chmod(store.name, 0)  # no rights at all
        kp._setNextKey(kp.SPACE)
        g = Game(scores_file=None, store_file=store.name)
        self.assertIs(None, g.loop())
        os.chmod(store.name, 0o200)  # give me writing rights back
        remove(store.name)

    # == .getCellStr == #

    def test_getCellStr_0(self):
        self.b.setCell(0, 0, 0)
        self.assertEqual(self.g.getCellStr(0, 0), '  .')

    def test_getCellStr_unknown_number(self):
        self.b.setCell(0, 0, 42)
        self.assertEqual(self.g.getCellStr(0, 0),
                         '%s 42%s' % (Fore.RESET, Style.RESET_ALL))

    def test_getCellStr_0_azmode(self):
        g = Game(azmode=True)
        g.board.setCell(0, 0, 0)
        self.assertEqual(g.getCellStr(0, 0), '.')

    def test_getCellStr_2(self):
        g = Game()
        g.board.setCell(0, 0, 2)
        self.assertRegexpMatches(g.getCellStr(0, 0), r'  2\x1b\[0m$')

    def test_getCellStr_1k(self):
        g = Game()
        g.board.setCell(0, 0, 1024)
        self.assertRegexpMatches(g.getCellStr(0, 0), r' 1k\x1b\[0m$')

    def test_getCellStr_2k(self):
        g = Game()
        g.board.setCell(0, 0, 2048)
        self.assertRegexpMatches(g.getCellStr(0, 0), r' 2k\x1b\[0m$')

    def test_getCellStr_2_azmode(self):
        g = Game(azmode=True)
        g.board.setCell(0, 0, 2)
        self.assertRegexpMatches(g.getCellStr(0, 0), r'a\x1b\[0m$')

    def test_getCellStr_unknown_number_azmode(self):
        g = Game(azmode=True)
        g.board.setCell(0, 0, 42)
        self.assertEqual(g.getCellStr(0, 0), '?')

    # == .boardToString == #

    def test_boardToString_height_no_margins(self):
        s = self.g.boardToString()
        self.assertEqual(len(s.split("\n")), self.b.size())

    # == .__str__ == #

    def test_str_height_no_margins(self):
        s = str(self.g)
        self.assertEqual(len(s.split("\n")), self.b.size())
Example #41
0
class TestGame(unittest.TestCase):

    def setUp(self):
        Board.SIZE = _BSIZE
        Game.SCORES_FILE = None
        self.g = Game(scores_file=None)
        self.b = self.g.board
        # don't print anything on stdout
        self.stdout = sys.stdout
        sys.stdout = DevNull()
        # mock os.system
        self.system = os.system
        self.sys_cmd = None

        def fake_system(*cmd):
            self.sys_cmd = cmd

        os.system = fake_system

    def tearDown(self):
        sys.stdout = self.stdout
        os.system = self.system
        kp._setCtrlC(False)

    def test_init_with_size_3_goal_4(self):
        g = Game(size=3, goal=4, scores_file=None)
        self.assertEqual(g.board.size(), 3)

    # == .saveBestScore == #

    def test_save_best_score_no_file(self):
        s = 42
        self.g.score = s
        self.g.saveBestScore()
        self.assertEqual(self.g.best_score, s)

    def test_save_best_score_with_file(self):
        s = 1000
        scores_file = NamedTemporaryFile(delete=True)
        g = Game(scores_file=scores_file.name)
        g.best_score = 0
        g.score = s
        g.saveBestScore()
        self.assertEqual(g.best_score, s)

    # == .loadBestScore == #

    def test_init_with_local_scores_file(self):
        s = 4241
        scores_file = NamedTemporaryFile(delete=False)
        scores_file.write(str(s).encode())
        scores_file.close()

        g = Game(scores_file=scores_file.name)
        self.assertEqual(g.best_score, s)

        remove(scores_file.name)

    def test_init_with_local_scores_file_fail(self):
        scores_file = NamedTemporaryFile(delete=False)
        scores_file.close()

        Game(scores_file=scores_file.name)

        remove(scores_file.name)

    # == .incScore == #

    def test_inc_0_score(self):
        s = 3
        self.g.score = s
        self.g.best_score = s
        self.g.incScore(0)
        self.assertEqual(self.g.score, s)
        self.assertEqual(self.g.best_score, s)

    def test_inc_2_score(self):
        s = 3
        i = 2
        self.g.score = s
        self.g.best_score = s
        self.g.incScore(i)
        self.assertEqual(self.g.score, s + i)
        self.assertEqual(self.g.best_score, s + i)

    def test_inc_score_update_best_score(self):
        s = 3
        i = 2
        self.g.score = s
        self.g.best_score = 0
        self.g.incScore(i)
        self.assertEqual(self.g.score, s + i)
        self.assertEqual(self.g.best_score, s + i)

    def test_inc_score_dont_update_best_score_if_higher(self):
        s = 3
        bs = 80
        i = 2
        self.g.score = s
        self.g.best_score = bs
        self.g.incScore(i)
        self.assertEqual(self.g.score, s + i)
        self.assertEqual(self.g.best_score, bs)

    # == .end == #

    def test_end_can_play(self):
        self.assertFalse(self.g.end())

    # == .readMove == #

    def test_read_unknown_move(self):
        kp._setNextKey(-1)
        self.assertEqual(self.g.readMove(), None)

    def test_read_known_move(self):
        kp._setNextKey(kp.LEFT)
        self.assertEqual(self.g.readMove(), Board.LEFT)

    # == .loop == #

    def test_simple_win_loop(self):
        kp._setNextKey(kp.UP)
        g = Game(goal=4, size=2, clear_screen=False)
        g.board.cells = [
            [2, 0],
            [2, 0]
        ]
        g.loop()

    def test_simple_win_loop_clear(self):
        kp._setNextKey(kp.UP)
        g = Game(goal=4, size=2)
        g.board.cells = [
            [2, 0],
            [2, 0]
        ]
        self.assertEqual(g.loop(), 4)
        if os.name == 'nt':
            self.assertEqual(self.sys_cmd, ('cls',))
        else:
            self.assertEqual(self.sys_cmd, ('clear',))

    def test_loop_interrupt(self):
        kp._setCtrlC(True)
        g = Game(goal=4, size=2)
        self.assertEqual(g.loop(), None)

    # == .getCellStr == #

    def test_getCellStr_0(self):
        self.b.setCell(0, 0, 0)
        self.assertEqual(self.g.getCellStr(0, 0), '  .')

    def test_getCellStr_unknown_number(self):
        self.b.setCell(0, 0, 42)
        self.assertEqual(self.g.getCellStr(0, 0),
                '%s 42%s' % (Fore.RESET, Style.RESET_ALL))

    def test_getCellStr_0_azmode(self):
        g = Game(azmode=True)
        g.board.setCell(0, 0, 0)
        self.assertEqual(g.getCellStr(0, 0), '.')

    def test_getCellStr_2(self):
        g = Game()
        g.board.setCell(0, 0, 2)
        self.assertRegexpMatches(g.getCellStr(0, 0), r'  2\x1b\[0m$')

    def test_getCellStr_1k(self):
        g = Game()
        g.board.setCell(0, 0, 1024)
        self.assertRegexpMatches(g.getCellStr(0, 0), r' 1k\x1b\[0m$')

    def test_getCellStr_2k(self):
        g = Game()
        g.board.setCell(0, 0, 2048)
        self.assertRegexpMatches(g.getCellStr(0, 0), r' 2k\x1b\[0m$')

    def test_getCellStr_2_azmode(self):
        g = Game(azmode=True)
        g.board.setCell(0, 0, 2)
        self.assertRegexpMatches(g.getCellStr(0, 0), r'a\x1b\[0m$')

    def test_getCellStr_unknown_number_azmode(self):
        g = Game(azmode=True)
        g.board.setCell(0, 0, 42)
        self.assertEqual(g.getCellStr(0, 0), '?')

    # == .boardToString == #

    def test_boardToString_height_no_margins(self):
        s = self.g.boardToString()
        self.assertEqual(len(s.split("\n")), self.b.size())

    # == .__str__ == #

    def test_str_height_no_margins(self):
        s = str(self.g)
        self.assertEqual(len(s.split("\n")), self.b.size())
Example #42
0
 def setUp(self):
     Board.SIZE = _BSIZE
     Game.SCORES_FILE = None
     self.g = Game(scores_file=None)
     self.b = self.g.board
Example #43
0
 def __init__(self):
     Game.__init__(self)
     self.mdirs=[1,2,3,4]
Example #44
0
 def test_getCellStr_unknown_number_azmode(self):
     g = Game(azmode=True)
     g.board.setCell(0, 0, 42)
     self.assertEqual(g.getCellStr(0, 0), '?')
Example #45
0
 def test_getCellStr_unknown_number_azmode(self):
     g = Game(azmode=True)
     g.board.setCell(0, 0, 42)
     self.assertEqual(g.getCellStr(0, 0), '?')
Example #46
0
 def test_getCellStr_2k(self):
     g = Game()
     g.board.setCell(0, 0, 2048)
     self.assertRegexpMatches(g.getCellStr(0, 0), r' 2k\x1b\[0m$')
Example #47
0
 def test_getCellStr_0_azmode(self):
     g = Game(azmode=True)
     g.board.setCell(0, 0, 0)
     self.assertEqual(g.getCellStr(0, 0), '.')
Example #48
0
 def __init__(self, step):
     Game.__init__(self, scores_file=None, store_file=None)
     self.mdirs=[1,2,3,4]
     self.step=step
     self.count=0
     self.cell=[]
Example #49
0
 def test_getCellStr_0_azmode(self):
     g = Game(azmode=True)
     g.board.setCell(0, 0, 0)
     self.assertEqual(g.getCellStr(0, 0), '.')
Example #50
0
 def test_hide_cursor_no_clear_screen_windows(self):
     g = Game(scores_file=None, store_file=None, clear_screen=False)
     self.setWindows(g)
     g.hideCursor()
     self.assertEqual(sys.stdout.read(), '')
Example #51
0
 def test_loop_interrupt(self):
     kp._setCtrlC(True)
     g = Game(goal=4, size=2)
     self.assertEqual(g.loop(), None)
Example #52
0
 def __init__(self, vis=False):
     if vis: Game.__init__(self, scores_file=None, store_file=None)
     self.best_score=0
     self.vis=vis
     self.reset()
Example #53
0
 def test_getCellStr_1k(self):
     g = Game()
     g.board.setCell(0, 0, 1024)
     self.assertRegexpMatches(g.getCellStr(0, 0), r' 1k\x1b\[0m$')
Example #54
0
 def test_hide_cursor_no_clear_screen_windows(self):
     g = Game(scores_file=None, store_file=None, clear_screen=False)
     self.setWindows(g)
     g.hideCursor()
     self.assertEqual(sys.stdout.read(), '')
Example #55
0
 def test_getCellStr_2_azmode(self):
     g = Game(azmode=True)
     g.board.setCell(0, 0, 2)
     self.assertRegexpMatches(g.getCellStr(0, 0), r'a\x1b\[0m$')
Example #56
0
 def test_restore_fail_return_false(self):
     store_name = '/i/dont/%s/exist/%s' % (uuid4(), uuid4())
     g = Game(scores_file=None, store_file=store_name)
     self.assertFalse(g.restore())
Example #57
0
 def test_init_with_size_3_goal_4(self):
     g = Game(size=3, goal=4, scores_file=None)
     self.assertEqual(g.board.size(), 3)
Example #58
0
 def test_clear_with_no_clear_screen(self):
     g = Game(scores_file=None, store_file=None, clear_screen=False)
     g.clearScreen()
     self.assertEqual(sys.stdout.read(), '\n\n')  # \n + print's \n
Example #59
0
 def test_getCellStr_2_azmode(self):
     g = Game(azmode=True)
     g.board.setCell(0, 0, 2)
     self.assertRegexpMatches(g.getCellStr(0, 0), r'a\x1b\[0m$')
Example #60
0
 def test_show_cursor_no_clear_screen_linux(self):
     g = Game(scores_file=None, store_file=None, clear_screen=False)
     self.setWindows(g, False)
     g.hideCursor()
     g.showCursor()
     self.assertEqual(sys.stdout.read(), '\033[?25h')