Esempio n. 1
0
    def simulate(self, robots, locs1, next_locs1, locs2, next_locs2, turns=1):
        players = [game.Player(robot=robot()) for robot in robots]
        map['start1'], map['start2'] = locs1, locs2
        game.init_settings(map)
        self._game = game.Game(*players, unit_testing=True, record_turns=True)

        for i in range(turns):
            self._game.run_turn()

        pprint.pprint(self._game._robots)

        return [[self._game.robot_at_loc(loc) for loc in locs]
                for locs in (next_locs1, next_locs2)]
Esempio n. 2
0
File: run.py Progetto: ramk13/rgkit
    def play(self, match_seed):
        if self.options.play_in_thread:
            g = game.ThreadedGame(self._players,
                                  print_info=self.options.print_info,
                                  record_actions=self.options.print_info,
                                  record_history=True,
                                  seed=match_seed,
                                  quiet=self.options.quiet,
                                  delta_callback=self._delta_callback,
                                  symmetric=self.options.symmetric)
        else:
            g = game.Game(self._players,
                          print_info=self.options.print_info,
                          record_actions=self.options.print_info,
                          record_history=True,
                          seed=match_seed,
                          quiet=self.options.quiet,
                          delta_callback=self._delta_callback,
                          symmetric=self.options.symmetric)

        if self.options.print_info and not self.options.curses:
            # only import render if we need to render the game;
            # this way, people who don't have tkinter can still
            # run headless
            from rgkit.render import render

        g.run_all_turns()

        if self.options.print_info and not self.options.curses:
            # print "rendering %s animations" % ("with"
            #                                    if animate_render
            #                                    else "without")
            render.Render(g, self.options.animate_render, names=self._names)

        # TODO: Displaying multiple games using curses is still a little bit
        # buggy but at least it doesn't completely screw up the state of the
        # terminal anymore.  The plan is to show each game sequentially.
        # Concurrency in run.py needs some more work before the bugs can be
        # fixed. Need to make sure nothing is printing when curses is running.
        if self.options.print_info and self.options.curses:
            from rgkit import rgcurses
            rgc = rgcurses.RGCurses(g, self._names)
            if self._rgcurses_lock:
                self._rgcurses_lock.acquire()
            rgc.run()
            if self._rgcurses_lock:
                self._rgcurses_lock.release()

        return g.get_scores()
Esempio n. 3
0
def comparison_worker(identity, input, output):
    logfile = open(
        os.getcwd() + "/" + "rgcompare" + "." + str(identity) +
        ".log", 'w') if log else open(os.devnull, 'w')
    print "Starting worker {0} (logging to {1})".format(identity, logfile.name)
    try:
        with RedirectStdStreams(stdout=logfile, stderr=logfile):
            for match_id, player_fnames, map_fname, turns in iter(
                    input.get, 'STOP'):

                map_data = ast.literal_eval(open(map_fname).read())
                settings.init_map(map_data)
                players = [game.Player(x) for x in player_fnames]
                g = game.Game(players)

                t_start = time.clock()
                for i in range(turns):
                    print(' running turn %d ' % g._state.turn).center(70, '-')
                    g.run_turn()
                t_end = time.clock()

                output.put([g.get_scores(), t_end - t_start])
    finally:
        print "Terminating worker {0}...".format(identity)
Esempio n. 4
0
File: run.py Progetto: ramk13/rgkit
 def game(self, record_turns=False, unit_testing=False):
     return game.Game(self._players, record_turns=record_turns,
                      unit_testing=unit_testing)
Esempio n. 5
0
def run_game(db, match, output_file):
    global settings

    proxy_process1, proxy_process2 = None, None
    try:
        load_map('rgkit/maps/default.py')

        output_file.write('---Starting Robot 1---\n')
        proxy_process1 = ProxyProcess(match['r1_code'])
        p1 = make_player(proxy_process1, output_file)
        if p1 is None:
            db.update('robots',
                      passed=False,
                      where='id=$id',
                      vars={'id': match['r1_id']})
            raise Exception('Robot 1 not able to be instantiated.')

        output_file.write('---Starting Robot 2---\n')
        proxy_process2 = ProxyProcess(match['r2_code'])
        p2 = make_player(proxy_process2, output_file)
        if p2 is None:
            db.update('robots',
                      passed=False,
                      where='id=$id',
                      vars={'id': match['r2_id']})
            raise Exception('Robot 2 not able to be instantiated.')

        if not (proxy_process1 is not None and proxy_process2 is not None
                and proxy_process1.alive() and proxy_process2.alive()):
            raise ProxyDeadError('the process is dead')
        g = game.Game([p1, p2],
                      record_actions=False,
                      record_history=True,
                      print_info=True,
                      seed=match['seed'],
                      symmetric=SYMMETRIC)
        g.run_all_turns()
        if not (proxy_process1 is not None and proxy_process2 is not None
                and proxy_process1.alive() and proxy_process2.alive()):
            raise ProxyDeadError('the process is dead')

        game_scores = g.get_scores()
        r1_score, r2_score = game_scores
        score = calc_score(game_scores)
        history = g.history
        match_data = shorten.dumps({'history': history, 'score': game_scores})
        winner = {1: match['r1_id'], 0: match['r2_id'], 0.5: 0}[score]

        output_file.write('---Time Taken---\n')
        r1_time = None
        r2_time = None
        try:
            r1_time = get_cpu_time(proxy_process1.pid)
            r2_time = get_cpu_time(proxy_process2.pid)
            output_file.write('R1: {0}\nR2: {1}\n'.format(r1_time, r2_time))
        except Exception:
            traceback.print_exc(file=output_file)

        # turn off printing here because the output for data is huge
        old_print = db.printing
        db.printing = False
        db.insert('history',
                  match_id=match['id'],
                  data=match_data,
                  timestamp=int(time.time()))
        db.update('matches',
                  where='id=$id',
                  vars={'id': match['id']},
                  winner=winner,
                  state=ms.DONE,
                  r1_score=r1_score,
                  r2_score=r2_score,
                  r1_time=r1_time,
                  r2_time=r2_time,
                  timestamp=int(time.time()))
        db.printing = old_print

        return score, r1_time, r2_time
    finally:
        if proxy_process1 is not None:
            proxy_process1.cleanup()
        if proxy_process2 is not None:
            proxy_process2.cleanup()