Beispiel #1
0
 def test_start_game_print_rules(self):
     sys.argv = ["term2048", "--rules"]
     try:
         ui.start_game()
     except helpers.FakeExit:
         pass
     else:
         self.assertFalse(True, "should exit after printing the version")
     self.assertEqual(self.exit_status, 0)
     self.assertRegexpMatches(self.output["output"], r".+")
Beispiel #2
0
 def test_start_game_print_rules(self):
     sys.argv = ['term2048', '--rules']
     try:
         ui.start_game()
     except helpers.FakeExit:
         pass
     else:
         self.assertFalse(True, "should exit after printing the version")
     self.assertEqual(self.exit_status, 0)
     self.assertRegexpMatches(self.output.read(), r'.+')
Beispiel #3
0
 def test_start_game_print_rules(self):
     sys.argv = ['term2048', '--rules']
     try:
         ui.start_game()
     except helpers.FakeExit:
         pass
     else:
         self.assertFalse(True, "should exit after printing the version")
     self.assertEqual(self.exit_status, 0)
     self.assertRegexpMatches(self.output.read(), r'.+')
Beispiel #4
0
 def test_start_game_print_version_over_rules(self):
     sys.argv = ['term2048', '--rules', '--version']
     try:
         ui.start_game()
     except helpers.FakeExit:
         pass
     else:
         self.assertFalse(True, "should exit after printing the version")
     self.assertEqual(self.exit_status, 0)
     self.assertRegexpMatches(self.output['output'],
                              r'^term2048 v\d+\.\d+\.\d+$')
Beispiel #5
0
 def test_start_game_print_version(self):
     sys.argv = ['term2048', '--version']
     try:
         ui.start_game()
     except helpers.FakeExit:
         pass
     else:
         self.assertFalse(True, "should exit after printing the version")
     self.assertEqual(self.exit_status, 0)
     self.assertRegexpMatches(self.output['output'],
             r'^term2048 v\d+\.\d+\.\d+$')
Beispiel #6
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])
Beispiel #7
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])
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
 def test_start_game_loop(self):
     ui.debug = False
     self.assertEqual(ui.start_game(), None)  # interrupted
Beispiel #11
0
 def test_start_game_print_argparse_warning(self):
     ui.start_game()
     self.assertIn('output', self.output)
     self.assertRegexpMatches(self.output['output'], r'^WARNING')
#!/usr/bin/env python

from term2048.ui import start_game

if __name__ == '__main__':
    start_game()
Beispiel #13
0
 def test_start_game_print_argparse_warning(self):
     ui.start_game()
     self.assertIn('output', self.output)
     self.assertRegexpMatches(self.output['output'], r'^WARNING')
Beispiel #14
0
 def test_start_game_loop(self):
     sys.argv = ['term2048']
     self.assertFalse(self._game_loop_started)
     ui.start_game()
     self.assertTrue(self._game_loop_started)
Beispiel #15
0
 def test_start_game_loop(self):
     ui.debug = False
     self.assertEqual(ui.start_game(), None)  # interrupted
Beispiel #16
0
 def test_start_game_print_argparse_warning(self):
     ui.start_game()
     self.assertIn("output", self.output)
     self.assertRegexpMatches(self.output["output"], r"^WARNING")
Beispiel #17
0
 def test_start_game_loop(self):
     sys.argv = ['term2048']
     self.assertFalse(self._game_loop_started)
     ui.start_game()
     self.assertTrue(self._game_loop_started)
Beispiel #18
0
from term2048.ui import start_game

start_game()
Beispiel #19
0
    bias[example_action] += example_reward
    update += bias
    print "update:", update
    train_value = old_value + self.learning_rate * (self.discount_factor *
                                                    update - old_value)
    self.train_step.run(feed_dict={self.x: np.matrix(example_input),
                                   self.y_: np.matrix(train_value),
                                   self.keep_prob: 0.5})
    print "example added: %s -> %s" % (example_input, train_value)

  def get_next_move(self, state):
    weights = self.y.eval(feed_dict={self.x: np.matrix(state)})[0]
    print "weights:", weights
    best_action = self.prediction.eval(feed_dict={self.x: np.matrix(state),
                                                  self.keep_prob: 1.0})[0]
    print "predicted action:", best_action
    self.counter += 1
    random_chance = 1 / (self.counter / self.counter**0.5)
    print "random chance:", random_chance
    if randint(1, self.counter) < self.counter**0.5:
      pass
      #best_action = randint(0, 3)
    print "taken action:", best_action
    return self.INDEX_TO_KEY[best_action]


if __name__ == "__main__":
  learner = Learner()
  while 1:
    ui.start_game(learner)