def check_if_game_over(l1, l2, options, game, rounds): if options.single_player_mode: return check_if_game_over_single_player(l1, l2, options, game, rounds) if l1 > 0 and l2 > 0: return False if options.display: game_interface.curses_init_round(game) winner = 0 if l1 < l2: winner = 2 elif l1 > l2: winner = 1 game_interface.curses_declare_winner(winner) else: if l1 == l2: print 'Tie, remaining life: %d v. %d' % (l1, l2) elif l1 < l2: print 'Player 2 wins: %d v. %d' % (l1, l2) else: print 'Player 1 wins: %d v. %d' % (l1, l2) # Wait for input if options.display: sys.stdin.read(1) game_interface.curses_close() return True
def check_if_game_over(l1, l2, options, game, rounds): if options.single_player_mode: return check_if_game_over_single_player(l1, l2, options, game, rounds) if l1 > 0 and l2 > 0: return False if options.display: game_interface.curses_init_round(game) winner = 0 if l1 < l2: winner = 2 elif l1 > l2: winner = 1 game_interface.curses_declare_winner(winner) else: if l1 == l2: print "Tie, remaining life: %d v. %d" % (l1, l2) elif l1 < l2: print "Player 2 wins: %d v. %d" % (l1, l2) else: print "Player 1 wins: %d v. %d" % (l1, l2) # Wait for input if options.display: sys.stdin.read(1) game_interface.curses_close() return True
def run(options): game = game_interface.GameInterface(options.plant_bonus, options.plant_penalty, options.observation_cost, options.starting_life, options.life_per_turn) player1_view = game.GetPlayer1View() player2_view = game.GetPlayer2View() if options.display: if game_interface.curses_init() < 0: return game_interface.curses_draw_board(game) # Keep running until one player runs out of life. while True: (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1) (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2) game.ExecuteMoves(mv1, eat1, mv2, eat2) if options.display: game_interface.curses_draw_board(game) game_interface.curses_init_round(game) #else: #print mv1, eat1, mv2, eat2 #print player1_view.GetLife(), player2_view.GetLife() # Check whether someone's life is negative. l1 = player1_view.GetLife() l2 = player2_view.GetLife() if l1 <= 0 or l2 <= 0: if options.display: winner = 0 if l1 < l2: winner = 2 else: winner = 1 game_interface.curses_declare_winner(winner) else: with open('score.txt', 'r') as file: wins, losses = map(int, file.readline().split()[:2]) if l1 == l2: print 'Tie, remaining life: %d v. %d' % (l1, l2) elif l1 < l2: print 'Player 2 wins: %d v. %d' % (l1, l2) wins += 1 else: print 'Player 1 wins: %d v. %d' % (l1, l2) losses += 1 with open('score.txt', 'w') as file: file.write(str(wins) + ' ' + str(losses) + ' ' + str(1.0 * wins / (wins + losses)) + '\n') # Wait for input #sys.stdin.read(1) if options.display: game_interface.curses_close() break
def run(options): game = game_interface.GameInterface(options.plant_bonus, options.plant_penalty, options.observation_cost, options.starting_life, options.life_per_turn) player1_view = game.GetPlayer1View() player2_view = game.GetPlayer2View() if options.display: if game_interface.curses_init() < 0: return game_interface.curses_draw_board(game) # Keep running until one player runs out of life. while True: (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1) (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2) # time.sleep(0.1) game.ExecuteMoves(mv1, eat1, mv2, eat2) if options.display: game_interface.curses_draw_board(game) game_interface.curses_init_round(game) else: pass print mv1, eat1, mv2, eat2 print 'Life:', player1_view.GetLife(), player2_view.GetLife() # Check whether someone's life is negative. l1 = player1_view.GetLife() l2 = player2_view.GetLife() if l1 <= 0 or l2 <= 0: player1.player.print_board((player1_view.GetXPos(), player1_view.GetYPos())) if options.display: winner = 0 if l1 < l2: winner = 2 else: winner = 1 game_interface.curses_declare_winner(winner) else: if l1 == l2: print 'Tie, remaining life: %d v. %d' % (l1, l2) elif l1 < l2: print 'Player 2 wins: %d v. %d' % (l1, l2) else: print 'Player 1 wins: %d v. %d' % (l1, l2) # Wait for input if options.display: sys.stdin.read(1) game_interface.curses_close() break
def run(options): game = game_interface.GameInterface(options.plant_bonus, options.plant_penalty, options.observation_cost, options.starting_life, options.life_per_turn) player1_view = game.GetPlayer1View() player2_view = game.GetPlayer2View() if options.display: if game_interface.curses_init() < 0: return game_interface.curses_draw_board(game) # Keep running until one player runs out of life. while True: (mv1, eat1) = get_move(player1_view, player.player.get_move, options, 1) (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2) # hack to break early if mv1 == None: break game.ExecuteMoves(mv1, eat1, mv2, eat2) if options.display: game_interface.curses_draw_board(game) game_interface.curses_init_round(game) else: print mv1, eat1, mv2, eat2 print player1_view.GetLife(), player2_view.GetLife() # Check whether someone's life is negative. l1 = player1_view.GetLife() l2 = player2_view.GetLife() if l1 <= 0 or l2 <= 0: if options.display: winner = 0 if l1 < l2: winner = 2 else: winner = 1 game_interface.curses_declare_winner(winner) else: if l1 == l2: print 'Tie, remaining life: %d v. %d' % (l1, l2) elif l1 < l2: print 'Player 2 wins: %d v. %d' % (l1, l2) else: print 'Player 1 wins: %d v. %d' % (l1, l2) # Wait for input # sys.stdin.read(1) if options.display: game_interface.curses_close() break
def run(options): game = game_interface.GameInterface(20,10,1, 100,1) """ game = game_interface.GameInterface(options.plant_bonus, options.plant_penalty, options.observation_cost, options.starting_life, options.life_per_turn) if options.display: if game_interface.curses_init() < 0: return game_interface.curses_draw_board(game)""" player1_view = game.GetPlayer1View() player2_view = game.GetPlayer2View() # Keep running until one player runs out of life. roundNum = 0 while True: (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1) (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2) old_life1 = player1_view.GetLife() old_life2 = player2_view.GetLife() game.ExecuteMoves(mv1, eat1, mv2, eat2) old_xpos1 = player1_view.GetXPos() old_xpos2 = player2_view.GetXPos() old_ypos1 = player1_view.GetYPos() old_ypos2 = player2_view.GetYPos() if False: #OPTIONS.DISPLAY game_interface.curses_draw_board(game) game_interface.curses_init_round(game) else: #print mv1, eat1, mv2, eat2 #print player1_view.GetLife(), player2_view.GetLife() #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + ")\n" #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + ")\n" #print "RoundNum: " + str(player1_view.GetRound()) + " Coords: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + ")" # if eat1: # if player1_view.GetLife() - old_life1 > 0: # #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Nutritious" + "\n" # print "1 " + str(player1_view.GetXPos()) + " " + str(player1_view.GetYPos()) + " N" + "\n" # else: # #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Poisonous" + "\n" # print "1 " + str(player1_view.GetXPos()) + " " + str(player1_view.GetYPos()) + " P" + "\n" # if eat2: # if player2_view.GetLife() - old_life2 > 0: # #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Nutritious" + "\n" # print "2 " + str(player2_view.GetXPos()) + " " + str(player2_view.GetYPos()) + " N" + "\n" # else: # #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Poisonous" + "\n" # print "2 " + str(player2_view.GetXPos()) + " " + str(player2_view.GetYPos()) + " P" + "\n" if eat1: if player1_view.GetLife() - old_life1 > 0: #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Nutritious" + "\n" print "N 1 " + str(old_xpos1) + " " + str(old_ypos1) + " N" + "\n" else: #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Poisonous" + "\n" print "P 1 " + str(old_xpos1) + " " + str(old_ypos1) + " P" + "\n" #else: # print "1 " + str(old_xpos1) + " " + str(old_ypos1) + "\n" if eat2: if player2_view.GetLife() - old_life2 > 0: #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Nutritious" + "\n" print "N 2 " + str(old_xpos2) + " " + str(old_ypos2) + " N" + "\n" else: #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Poisonous" + "\n" print "P 2 " + str(old_xpos2) + " " + str(old_ypos2) + " P" + "\n" #else: # print "2 " + str(old_xpos2) + " " + str(old_ypos2) + "\n" # Check whether someone's life is negative. l1 = player1_view.GetLife() l2 = player2_view.GetLife() if l1 <= 0 or l2 <= 0: if False: # OPTIONS.DISPLAY winner = 0 if l1 < l2: winner = 2 else: winner = 1 game_interface.curses_declare_winner(winner) else: if l1 == l2: print 'Tie, remaining life: %d v. %d' % (l1, l2) return 2 elif l1 < l2: print 'Player 2 wins: %d v. %d' % (l1, l2) return 0 else: print 'Player 1 wins: %d v. %d' % (l1, l2) return 1 # Wait for input sys.stdin.read(1) if options.display: game_interface.curses_close() break
def run(options): game = game_interface.GameInterface(options.plant_bonus, options.plant_penalty, options.observation_cost, options.starting_life, options.life_per_turn) player1_view = game.GetPlayer1View() player2_view = game.GetPlayer2View() if options.display: if game_interface.curses_init() < 0: return game_interface.curses_draw_board(game) d = {} img_file = open('image_map_startlife=100.txt', 'r') img = pickle.load(img_file) img_file.close() # Keep running until one player runs out of life. while True: (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1) (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2) # --------------------------------- # Experiment Code # Get the coordinates of each run player1_coords = (player1_view.GetXPos(), player1_view.GetYPos()) player2_coords = (player2_view.GetXPos(), player2_view.GetYPos()) player1_status = player1_view.GetPlantInfo() player2_status = player2_view.GetPlantInfo() # d[player1_coords] = player1_status # d[player2_coords] = player2_status if (player1_status == game_interface.STATUS_NUTRITIOUS_PLANT) | (player1_status == game_interface.STATUS_POISONOUS_PLANT): img[player1_view.GetImage()] = player1_status if (player2_status == game_interface.STATUS_NUTRITIOUS_PLANT) | (player2_status == game_interface.STATUS_POISONOUS_PLANT): img[player2_view.GetImage()] = player2_status # ---------------------------------- game.ExecuteMoves(mv1, eat1, mv2, eat2) if options.display: game_interface.curses_draw_board(game) game_interface.curses_init_round(game) else: print mv1, eat1, mv2, eat2 print player1_view.GetLife(), player2_view.GetLife() # Check whether someone's life is negative. l1 = player1_view.GetLife() l2 = player2_view.GetLife() if l1 <= 0 or l2 <= 0: if options.display: winner = 0 if l1 < l2: winner = 2 else: winner = 1 game_interface.curses_declare_winner(winner) else: if l1 == l2: print 'Tie, remaining life: %d v. %d' % (l1, l2) elif l1 < l2: print 'Player 2 wins: %d v. %d' % (l1, l2) else: print 'Player 1 wins: %d v. %d' % (l1, l2) # Wait for input #sys.stdin.read(1) if options.display: game_interface.curses_close() break # distri_file = open('plant_distribution_startlife=100.txt', 'w') image_map_file = open('image_map_startlife=100.txt', 'w') #print img # print d # pickle.dump(d, distri_file) pickle.dump(img, image_map_file) # distri_file.close() image_map_file.close()
def run(options): game = game_interface.GameInterface(options.plant_bonus, options.plant_penalty, options.observation_cost, options.starting_life, options.life_per_turn) player1_view = game.GetPlayer1View() player2_view = game.GetPlayer2View() if options.display: if game_interface.curses_init() < 0: return game_interface.curses_draw_board(game) # Keep running until one player runs out of life. while True: (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1) if options.train != 0: (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2) else: (mv2, eat2) = get_move(player2_view, common.get_move, options, 2) game.ExecuteMoves(mv1, eat1, mv2, eat2) # No longer new game options.new_game = False if options.display == 1: game_interface.curses_draw_board(game) game_interface.curses_init_round(game) elif options.display == 2: print mv1, eat1, mv2, eat2 print player1_view.GetLife(), player2_view.GetLife() # Check whether someone's life is negative. l1 = player1_view.GetLife() l2 = player2_view.GetLife() if l1 <= 0 or l2 <= 0: # # Export Q # if options.q_out != None: # # print "Exporting Q to", options.q_out # f = open(options.q_out, "w") # cPickle.dump(player1.player.Q, f) # f.close() # f = open(options.t_out, "w") # cPickle.dump(player1.player.t_count, f) # f.close() # # Export neural network weights # if options.train == 1: # # print "Exporting weights to", options.out_file # player2.player.network.ExportWeights(options.out_file) # elif options.train == 2: # print player2.player.correct / player2.player.instances # elif options.train == 3: # # print "Exporting plants to", options.out_file # player2.player.ExportPlants(options.out_file) if options.display: winner = 0 if l1 < l2: winner = 2 else: winner = 1 game_interface.curses_declare_winner(winner) else: if l1 == l2: print 'Tie, remaining life: %d v. %d' % (l1, l2) elif l1 < l2: print 'Player 2 wins: %d v. %d' % (l1, l2) else: print 'Player 1 wins: %d v. %d' % (l1, l2) if options.display: # Wait for input sys.stdin.read(1) game_interface.curses_close() break