def updateGame(msg, current_q): old_gamestate = copy.deepcopy(game.gamestate) new_state = copy.deepcopy(game.outcome(name, msg)) gamestr = game.createStateStr() sent_time = time.time() sendMsg( str(int(MsgType.update)) + "/" + name + "/" + gamestr + "/" + "{:f}".format(sent_time)) # check no-one else sent a message before (use max_latency as timeout) acked = tryUpdate(sent_time, delay) if game.state_shifted(new_state): #another player beat us return old_gamestate # Get a updateack if we're in client-server mode if not PEERMODE and not LEADER and not acked: response = udp_sock.recvmsg().split('/') if not (len(response) == 2 and int(response[0]) == MsgType.updateack): print "Error sending message" game.update_state(old_gamestate) # revert to old gamestate return processMessage('/'.join(response), current_state) if game.is_game_over(): game.output_eog_msg() sendMsg( str(int(MsgType.eog)) + "/" + name + "/" + game.createStateStr()) timelog.write("{:f}".format(time.time() - start) + '\n') timelog.close() exit(0) if game.state_shifted(old_gamestate): game.output_state_shifted_msg() return new_state
def main(): form = cgi.FieldStorage() try: user_data = data.read_users() user_info = user.get_session_from_cookie(user_data) selfplayer = user_info["username"] game_data = data.read_games() (p0, p1) = get_form_players(form) game_info = game.get_game_info(game_data, p0, p1) print_header() move = get_form_move(form) if move: try: game_data = game.move(game_data, game_info, selfplayer, move) data.write_games(game_data) except game.InvalidMove: print "<p>Invalid Move</p>" #print "<pre>" #print game_data #print "</pre>" game_over = game.is_game_over(game_info) if game_over[0]: print_game_over(game_info, game_over[1]) else: print_game(game_info, selfplayer) print_footer() except user.NotLoggedIn: print "Content-Type: text/html\n\n" print "<html><body>" print """<p>Not logged in. <a href="/cgi-bin/login.py">Log In</a> </p>""" print "</body></html>" # session not found, redirect back to login #print "Location: /cgi-bin/login.py\n\n" except (game.UnknownGame, MissingPlayers) as ex: print_header() print ex print """<a href="/cgi-bin/newgame.py">New Game</a></p>""" print_footer()
def processMessage(msg, current_q): if LEADER: forwardMessage(msg) msg = msg.split('/') old_state = game.gamestate new_state = old_state # received an update message, try to update the gamestate if len(msg) == 4 and int(msg[0]) == MsgType.update: try: game.update_state(game.parseStateStr(msg[2])) except ValueError: f = open('log', 'a') f.write("\n\nError parsing gamestate:\n") f.write(msg[2]) f.close() print "Bad message" if game.is_game_over(): game.output_eog_msg() timelog.write("{:f}".format(time.time() - start) + '\n') timelog.close() exit(0) if game.state_shifted(old_state): game.output_state_shifted_msg() new_state = game.gamestate # shutdown if we receive an eog message elif len(msg) == 3 and int(msg[0]) == MsgType.eog: game.update_state(game.parseStateStr(msg[2])) game.output_eog_msg() timelog.write("{:f}".format(time.time() - start) + '\n') timelog.close() exit(0) elif int(msg[0]) == MsgType.eog: timelog.write("{:f}".format(time.time() - start) + '\n') timelog.close() exit(0) else: print "Badly formatted message" return new_state
powers_of_two = {2**i: i for i in range(17)} powers_of_two[0] = 0 def transform_state_to_input(state): height, width = state.shape result = np.zeros([1, height, width, 17], dtype=np.float32) for i in range(height): for j in range(width): result[0][i][j][powers_of_two[state[i][j]]] = 1 return result state = game.initial_state() moved = True while not game.is_game_over(state): if moved: game.do_a_random_spawn(state) if not moved: action = random.randrange(number_of_actions) else: _, action = net.predict(transform_state_to_input(state)) action = action[0] state, moved, _ = game.action(state, action) print(state) for game_counter in range(game_iterations): state = game.initial_state() moved = True print('Game', game_counter) steps = 0