Question: 1. Take the following code in the rtf file, paste it into a Jupyter Notebook: 2. Run the code once to make sure there are

1. Take the following code in the rtf file, paste it into a Jupyter Notebook:

2. Run the code once to make sure there are no bugs

3. Turn it into a function (the def command)

4. In another cell, create a loop that calls the function 1000 times

5. Before calling the simulation function, set army pieces to something interesting; Not the 1,1,1,1,1,1 that I used to test my python code

6. You can save your python code as a .py, .rtf. .txt. or a .ipynb file

# Python routine for simulation on Neil Thomas' Ancient Warfare Rules

#

import random

# This routines compares two armies using the tactics table from Imperator game

# The first army could be the player's army

# while the second army is the enemy

# First Army Units

A1Eleph = 1

A1Char = 1

A1HvInf = 1

A1Wrbnd = 1

A1Aux = 1

A1HvCav = 1

A1HvChar = 1

A1Archr = 1

A1LtCav = 1

A1LtInf = 1

A1Art = 1

# Second Army Units

A2Eleph = 1

A2Char = 1

A2HvInf = 1

A2Wrbnd = 1

A2Aux = 1

A2HvCav = 1

A2HvChar = 1

A2Archr = 1

A2LtCav = 1

A2LtInf = 1

A2Art = 1

# First Army Unit Classes

A1HCLC = A1HvCav + A1LtCav

A1Infantry = A1HvInf + A1Wrbnd + A1Aux + A1Archr + A1LtInf

A1AllMntd = A1Eleph + A1Char + A1HvCav + A1HvChar + A1LtCav

A1AllUnits = A1Eleph + A1Char + A1HvInf + A1Wrbnd + A1Aux + A1HvCav + A1Archr + A1LtCav + A1LtInf + A1Art

# Second Army Unit Classes

A2HCLC = A2HvCav + A2LtCav

A2Infantry = A2HvInf + A2Wrbnd + A2Aux + A2Archr + A2LtInf

A2AllMntd = A2Eleph + A2Char + A2HvCav + A2HvChar + A2LtCav

A2AllUnits = A2Eleph + A2Char + A2HvInf + A2Wrbnd + A2Aux + A2HvCav + A2Archr + A2LtCav + A2LtInf + A2Art

# First Army's initial probabilities per tactic

# Note that these really aren't probabiltites ranging from 0 to 1

# It's more like odds, the higher the number, the more likely the outcome

A1ChargeProb = 1

A1RefuseProb = 1

A1EnvelopeProb = 1

A1StandProb = 1

# Second Army's initial probabilities per tactic

A2ChargeProb = 1

A2RefuseProb = 1

A2EnvelopeProb = 1

A2StandProb = 1

# Next Modify the probabilities by comparing army classes

# Modify probabilities for the first army

if A1HCLC > A1HCLC: A1EnvelopeProb = A1EnvelopeProb + 2

if A1AllMntd > A2AllMntd: A1ChargeProb = A1ChargeProb + 1

if A1Infantry > A2Infantry:

A1RefuseProb = A1RefuseProb + 2

A1StandProb = A1StandProb + 1

if A1Infantry < A2Infantry:

A1RefuseProb = A1RefuseProb - 1

A1EnvelopeProb = A1EnvelopeProb - 1

A1TotalOptions = A1ChargeProb + A1RefuseProb + A1EnvelopeProb + A1StandProb

# Modify probabilities for the second army

if A2HCLC > A1HCLC: A2EnvelopeProb = A2EnvelopeProb + 2

if A2AllMntd > A1AllMntd: A2ChargeProb = A2ChargeProb + 1

if A2Infantry > A1Infantry:

A2RefuseProb = A2RefuseProb + 2

A2StandProb = A2StandProb + 1

if A2Infantry < A1Infantry:

A2RefuseProb = A2RefuseProb - 1

A2EnvelopeProb = A2EnvelopeProb - 1

A2TotalOptions = A2ChargeProb + A2RefuseProb + A2EnvelopeProb + A2StandProb

# Next choose the tactics for both armies

# first we initialize some variables to 0, indicating no tactic has been chosen yet

A1Charge, A1Refuse, A1Envelope, A1Stand = 0, 0, 0, 0

A2Charge, A2Refuse, A2Envelope, A2Stand = 0, 0, 0, 0

# set our seed for the random numbers

# random.seed(162892) # can set the seed if you want

# next generate a random number and compare that to some ratios

A1TacticsRG = random.randint(1, A1TotalOptions)

A2TacticsRG = random.randint(1, A2TotalOptions)

# next determine the tactic for each army

A1Tactic = 0

if A1TacticsRG <= A1ChargeProb:

A1Tactic = 1 # charging

print("Army 1 is charging.")

elif A1TacticsRG <= (A1ChargeProb + A1RefuseProb):

A1Tactic = 2 # refuse

print("Army 1 is refusing.")

elif A1TacticsRG <= (A1ChargeProb + A1RefuseProb + A1EnvelopeProb):

A1Tactic = 3 # envelope

print("Army 1 is enveloping.")

else:

A1Tactic = 4 # stand

print("Army 1 is standing.")

A2Tactic = 0

if A2TacticsRG <= A2ChargeProb:

A2Tactic = 1 # charging

print("Army 2 is charging.")

elif A2TacticsRG <= (A2ChargeProb + A2RefuseProb):

A2Tactic = 2 # refuse

print("Army 2 is refusing.")

elif A2TacticsRG <= (A2ChargeProb + A2RefuseProb + A2EnvelopeProb):

A2Tactic = 3 # envelope

print("Army 2 is enveloping.")

else:

A2Tactic = 4 # stand

print("Army 2 is standing.")

# What is the outcome of comparing the two tactics?

# in other words, which troops of which army is doubled or tripled?

# first army charges

if (A1Tactic == 1 and A2Tactic == 1):

A1Fight = A1AllUnits + A1AllMntd

A2Fight = A2AllUnits + A2AllMntd

if (A1Tactic == 1 and A2Tactic == 2):

A1Fight = A1AllUnits

A2Fight = A2AllUnits + A2AllUnits

if (A1Tactic == 1 and A2Tactic == 3):

A1Fight = A1AllUnits + A1HCLC + A1HCLC

A2Fight = A2AllUnits + A2HCLC + A2HCLC

if (A1Tactic == 1 and A2Tactic == 4):

A1Fight = A1AllUnits + A1AllUnits

A2Fight = A2AllUnits

# first army refuses

if (A1Tactic == 2 and A2Tactic == 1):

A1Fight = A1AllUnits + A1AllUnits

A2Fight = A2AllUnits

if (A1Tactic == 2 and A2Tactic == 2):

A1Fight = A1AllUnits

A2Fight = A2AllUnits

if (A1Tactic == 2 and A2Tactic == 3):

A1Fight = A1AllUnits + A1AllUnits

A2Fight = A2AllUnits

if (A1Tactic == 2 and A2Tactic == 4):

A1Fight = A1AllUnits + A1Infantry

A2Fight = A2AllUnits + A2Infantry

# first army envelopes

if (A1Tactic == 3 and A2Tactic == 1):

A1Fight = A1AllUnits + A1HCLC + A1HCLC

A2Fight = A2AllUnits + A2HCLC + A2HCLC

if (A1Tactic == 3 and A2Tactic == 2):

A1Fight = A1AllUnits

A2Fight = A2AllUnits + A2AllUnits

if (A1Tactic == 3 and A2Tactic == 3):

A1Fight = A1AllUnits + A1HCLC + A1HCLC

A2Fight = A2AllUnits + A2HCLC + A2HCLC

if (A1Tactic == 3 and A2Tactic == 4):

A1Fight = A1AllUnits + A1Infantry

A2Fight = A2AllUnits + A2Infantry

# first army stands

if (A1Tactic == 4 and A2Tactic == 1):

A1Fight = A1AllUnits

A2Fight = A2AllUnits + A2AllMntd

if (A1Tactic == 4 and A2Tactic == 2):

A1Fight = A1AllUnits + A1Infantry

A2Fight = A2AllUnits + A2Infantry

if (A1Tactic == 4 and A2Tactic == 3):

A1Fight = A1AllUnits + A1Infantry

A2Fight = A2AllUnits + A2Infantry

if (A1Tactic == 4 and A2Tactic == 4):

A1Fight = 0

A2Fight = 0

# Next calculate the odds of winning

if ((A1Fight + A2Fight) == 0):

print("uh oh...No Battle Today!")

else:

WinningOdds = (A1Fight + 0.0) / (A1Fight + A2Fight)

BattleOutcome = random.random()

if BattleOutcome <= WinningOdds:

print("You won!")

else: print("You lost!")

print("WinningOdds is ",WinningOdds)

print("BattleOutcome is ",BattleOutcome)

#print("A1Fight is ",A1Fight)

#print("A1Tactic is ",A1Tactic)

#print("A2Fight is ",A2Fight)

#print("A2Tactic is ",A2Tactic)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!