homework commit
This commit is contained in:
+182
@@ -0,0 +1,182 @@
|
||||
from core import Trainer
|
||||
|
||||
import random
|
||||
import copy
|
||||
import game_data as gd
|
||||
import pickle
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
def RandomBattle(trainer,cpu_trainer):
|
||||
#print(f"You are in battle against {cpu_trainer.name}!")
|
||||
battle_active = True
|
||||
|
||||
active_pokemon = trainer.get_active_pokemon()
|
||||
cpu_active_pokemon = cpu_trainer.get_active_pokemon()
|
||||
|
||||
did_player_won = False
|
||||
battle_turns_num = 0
|
||||
|
||||
while battle_active:
|
||||
player_move_name = random.choice(active_pokemon.get_moves())
|
||||
#print(f"{active_pokemon.name} selected {player_move_name} to use against {cpu_active_pokemon.name}:\n")
|
||||
try:
|
||||
|
||||
active_pokemon.useMove(player_move_name,cpu_active_pokemon)
|
||||
battle_turns_num += 1
|
||||
#check if the opponent has been defeated
|
||||
if not cpu_trainer.active_pokemon_can_battle():
|
||||
#print(f"{cpu_trainer.get_active_pokemon().name} has been defeated! You win!")
|
||||
did_player_won = True
|
||||
battle_active = False
|
||||
break
|
||||
|
||||
#if the opponent has not been defeated, then its the CPU turn to play
|
||||
cpu_active_pokemon_selected_move_name = random.choice(cpu_active_pokemon.get_moves())
|
||||
#print(f"CPU's {cpu_active_pokemon.name} is using {cpu_active_pokemon_selected_move_name} against your {active_pokemon.name}!")
|
||||
|
||||
cpu_active_pokemon.useMove(cpu_active_pokemon_selected_move_name, active_pokemon)
|
||||
battle_turns_num += 1
|
||||
#update the player pokemon list
|
||||
trainer.update_pokemon_list(active_pokemon)
|
||||
#check if the player has been defeated
|
||||
if not trainer.active_pokemon_can_battle():
|
||||
#print(f"Your {active_pokemon.name} has been defeated!")
|
||||
if not trainer.can_battle():
|
||||
#print("You have no more Pokemons that can battle! You lose!")
|
||||
did_player_won=False
|
||||
battle_active = False
|
||||
break
|
||||
else:
|
||||
#print("You have other Pokemons that can battle!")
|
||||
continue
|
||||
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
|
||||
player_residual_hp_percent = trainer.get_active_pokemon().get_current_hp_percent()
|
||||
|
||||
battle_results = {
|
||||
'encountered_pokemon':cpu_trainer.get_active_pokemon().name,
|
||||
'result':did_player_won,
|
||||
'battle_turns_num':battle_turns_num,
|
||||
'player_residual_hp_percent':player_residual_hp_percent
|
||||
}
|
||||
return battle_results
|
||||
|
||||
def Main(starter_choice,Nbattles):
|
||||
try:
|
||||
starter_name = starter_choice
|
||||
trainer = Trainer(name='edo', starter_name=starter_name)
|
||||
#print(f"{trainer.name} has chosen {trainer.starter} as their starter Pokemon!")
|
||||
#print("Adding potions and pokeballs to your inventory...")
|
||||
trainer.items = {"potions": 10, "pokeball": 10}
|
||||
#print("Your character is ready!")
|
||||
#print(f"Welcome {trainer.name}! Your adventure begins now!")
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
print("Something went wrong!")
|
||||
|
||||
game_active_flag = True
|
||||
while game_active_flag:
|
||||
all_battle_results = []
|
||||
for i in range(0,Nbattles):
|
||||
#print("You entered the Random Battle mode")
|
||||
#print("Battle #",i)
|
||||
cpu_trainer = Trainer(name='CPU',starter_name=gd.available_pokemon_names[random.randint(0,len(gd.available_pokemon_names)-1)],is_CPU=True)
|
||||
battle_results = RandomBattle(trainer,cpu_trainer)
|
||||
all_battle_results.append(battle_results)
|
||||
heal_flag = 'yes'
|
||||
if heal_flag == 'yes':
|
||||
for pokemon in trainer.pokemon_list:
|
||||
pokemon.curr_hp = pokemon.baseStats['hp']
|
||||
#print("All your Pokemon have been healed!")
|
||||
|
||||
game_active_flag = False
|
||||
#save all battle results
|
||||
return all_battle_results
|
||||
|
||||
def make_plots(Ngames,Nbattles):
|
||||
#SIMPLE PLOT
|
||||
cumulative_victories = np.zeros((Ngames,Nbattles))
|
||||
plt.figure(figsize=(10, 6))
|
||||
for starter_choice in gd.available_starters:
|
||||
for ng in range(Ngames):
|
||||
pickle_in = open(f"results/{starter_choice}_game_{ng}_{Nbattles}_battles.pickle",'rb')
|
||||
all_battle_results = pickle.load(pickle_in)
|
||||
pickle_in.close()
|
||||
this_game_victory_number = 0
|
||||
for j, battle_result in enumerate(all_battle_results):
|
||||
if battle_result['result'] == True:
|
||||
this_game_victory_number += 1
|
||||
cumulative_victories[ng,j] = this_game_victory_number
|
||||
|
||||
avg_cumulative_victories = np.mean(cumulative_victories,axis=0)
|
||||
|
||||
plt.plot(avg_cumulative_victories, label=f"{starter_choice}")
|
||||
plt.xlabel("Battle number")
|
||||
plt.ylabel("Average number of victories")
|
||||
plt.title(f"Cumulative number of victories over {Nbattles} battles average over {Ngames} games")
|
||||
plt.legend()
|
||||
plt.savefig(f'plots/battle_victories_plot.pdf')
|
||||
|
||||
#BOX PLOT - turns
|
||||
plt.figure(figsize=(10, 6))
|
||||
boxes_to_plot = []
|
||||
for m,starter_choice in enumerate(gd.available_starters):
|
||||
all_battle_turns = np.zeros((Ngames,Nbattles))
|
||||
for i in range(Ngames):
|
||||
pickle_in = open(f"results/{starter_choice}_game_{i}_{Nbattles}_battles.pickle",'rb')
|
||||
all_battle_results = pickle.load(pickle_in)
|
||||
pickle_in.close()
|
||||
for j in range(Nbattles):
|
||||
all_battle_turns[i,j] = all_battle_results[j]['battle_turns_num']
|
||||
|
||||
boxes_to_plot.append(all_battle_turns.flatten())
|
||||
plt.boxplot(boxes_to_plot)
|
||||
|
||||
plt.xticks(np.arange(1,len(gd.available_starters)+1), gd.available_starters, fontsize=15)
|
||||
plt.xlabel("Starter names", fontsize=15)
|
||||
plt.yticks(fontsize=15)
|
||||
plt.ylabel("Number of turns", fontsize=15)
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig(f'plots/battle_turns_boxplot.pdf')
|
||||
plt.close()
|
||||
|
||||
#BOX PLOT - health
|
||||
boxes_to_plot = []
|
||||
plt.figure(figsize=(10, 6))
|
||||
for m,starter_choice in enumerate(gd.available_starters):
|
||||
all_battle_health = np.zeros((Ngames,Nbattles))
|
||||
for i in range(Ngames):
|
||||
pickle_in = open(f"results/{starter_choice}_game_{i}_{Nbattles}_battles.pickle",'rb')
|
||||
all_battle_results = pickle.load(pickle_in)
|
||||
pickle_in.close()
|
||||
for j in range(Nbattles):
|
||||
all_battle_health[i,j] = all_battle_results[j]['player_residual_hp_percent']*100
|
||||
boxes_to_plot.append(all_battle_health.flatten())
|
||||
plt.boxplot(boxes_to_plot)
|
||||
|
||||
plt.xlabel("Starter names", fontsize=15)
|
||||
plt.xticks(np.arange(1,len(gd.available_starters)+1), gd.available_starters,fontsize=15)
|
||||
plt.yticks(fontsize=15)
|
||||
plt.ylabel("Player residual HP (%)", fontsize=15)
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig(f'plots/battle_health_boxplot.pdf')
|
||||
plt.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Ngames = 500
|
||||
Nbattles = 100
|
||||
for starter_choice in gd.available_starters:
|
||||
for i in range(Ngames):
|
||||
print(f"Generating data for {starter_choice}, game {i}...")
|
||||
results = Main(starter_choice = starter_choice, Nbattles=Nbattles)
|
||||
pickle_out = open(f"results/{starter_choice}_game_{i}_{Nbattles}_battles.pickle",'wb')
|
||||
pickle.dump(results,pickle_out)
|
||||
pickle_out.close()
|
||||
|
||||
make_plots(Ngames, Nbattles)
|
||||
Reference in New Issue
Block a user