示例#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".+")
示例#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'.+')
示例#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'.+')
示例#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+$')
示例#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+$')
示例#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])
示例#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])
示例#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)
示例#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)
示例#10
0
 def test_start_game_loop(self):
     ui.debug = False
     self.assertEqual(ui.start_game(), None)  # interrupted
示例#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()
示例#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')
示例#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)
示例#15
0
 def test_start_game_loop(self):
     ui.debug = False
     self.assertEqual(ui.start_game(), None)  # interrupted
示例#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")
示例#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)
示例#18
0
from term2048.ui import start_game

start_game()
示例#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)