Last active
March 3, 2020 05:38
-
-
Save gftabor/d56b15cecb7bc580c71bafbb0d1e0386 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Spyder Editor | |
This is a temporary script file. | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
#some of this code is left over from when I was going to implement different strategies for calling for a new army name | |
class Army: | |
def __init__(self,name,soldiers): | |
self.name = name | |
self.numSoldiers = soldiers | |
def getCalledArmyName(self): | |
return self.name | |
def killGetPointChange(killerArmy,dyingArmy): | |
points = np.array([0,0,0]) | |
dyingArmy.numSoldiers -= 1 | |
killerArmyName = killerArmy.getCalledArmyName() | |
if(killerArmyName == dyingArmy.name): | |
points[armyIndex.index(killerArmyName)] -=1 | |
else: | |
points[armyIndex.index(killerArmyName)] +=2 | |
return points | |
def fight(army1,army2): | |
points = np.array([0,0,0]) | |
while(army1.numSoldiers >0 and army2.numSoldiers >0): | |
totalSoldiers = army1.numSoldiers + army2.numSoldiers | |
chosenSoldier = np.random.randint(1,totalSoldiers+1) | |
if(chosenSoldier>army1.numSoldiers): | |
points += killGetPointChange(army2,army1) | |
else: | |
points += killGetPointChange(army1,army2) | |
if(army1.numSoldiers >0): | |
return points,army1 | |
return points,army2 | |
#simulates a number of fights given some probability that at the beginning the fight it is decided if 2 dragon+chaos will team until hermione is defeated | |
#fights are always between 2 armies and there is a bit of hacking to make the 1v1v1 case work out reasonably | |
#also assume all armies split themselves evenly before fighting, which is not necessarily optimal | |
def simulateBattles(betrayalChance): | |
scores = np.zeros((numSimulation,3)) | |
for i in range(numSimulation): | |
if(np.random.random()<=betrayalChance): | |
sunHalf1 = Army(armyIndex[0],12) | |
sunHalf2 = Army(armyIndex[0],12) | |
chaosHalf1 = Army(armyIndex[1],12) | |
chaosHalf2 = Army(armyIndex[1],12) | |
dragonHalf1 = Army(armyIndex[2],12) | |
dragonHalf2 = Army(armyIndex[2],12) | |
outcome1 = fight(sunHalf1,chaosHalf1) | |
outcome2 = fight(sunHalf2,dragonHalf1) | |
outcome3 = fight(dragonHalf2,chaosHalf2) | |
if(outcome1[1].name==outcome2[1].name): | |
outcome1[1].numSoldiers += outcome2[1].numSoldiers | |
Final = fight(outcome1[1],outcome3[1]) | |
scores[i,:] = outcome1[0] + outcome2[0] + outcome3[0] + Final[0] | |
elif(outcome3[1].name==outcome2[1].name): | |
outcome3[1].numSoldiers += outcome2[1].numSoldiers | |
Final = fight(outcome1[1],outcome3[1]) | |
scores[i,:] = outcome1[0] + outcome2[0] + outcome3[0] + Final[0] | |
elif(outcome3[1].name==outcome1[1].name): | |
outcome3[1].numSoldiers += outcome1[1].numSoldiers | |
Final = fight(outcome1[1],outcome2[1]) | |
scores[i,:] = outcome1[0] + outcome2[0] + outcome3[0] + Final[0] | |
else: | |
outcome4 = fight(outcome1[1],outcome2[1]) | |
Final = fight(outcome3[1],outcome4[1]) | |
scores[i,:] = outcome1[0] + outcome2[0] + outcome3[0]+ outcome4[0] + Final[0] | |
else: | |
sunHalf1 = Army(armyIndex[0],12) | |
sunHalf2 = Army(armyIndex[0],12) | |
chaos = Army(armyIndex[1],24) | |
dragon = Army(armyIndex[2],24) | |
outcome1 = fight(sunHalf1,chaos) | |
outcome2 = fight(sunHalf2,dragon) | |
change = outcome1[0]+outcome2[0] | |
if(outcome1[1].name != outcome2[1].name): | |
change += fight(outcome1[1],outcome2[1])[0] | |
scores[i,:] = change | |
return scores | |
armyIndex=['sunshine','chaos','dragon'] | |
numSimulation = 10000 | |
for j in np.linspace(0,.50,11): | |
perc = j | |
scores = simulateBattles(perc) | |
avChange = np.average(scores,0) | |
print('Hermiones lead with ' +str(perc) + ' chance of betrayal is ' + str(24+avChange[0]-avChange[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment