Skip to content

Instantly share code, notes, and snippets.

@rohanp
Created November 10, 2015 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohanp/d18ee6353476bd1f8124 to your computer and use it in GitHub Desktop.
Save rohanp/d18ee6353476bd1f8124 to your computer and use it in GitHub Desktop.
import random
import sys
from time import sleep
from getpass import getpass
def main():
n_games = int(sys.argv[1])
print("You have 100 tanks do distrubute between 5 territories.")
print("Enter the number of tanks to put in each territory.")
print("You may -1 for the territory to auto-select number needed to add to 100.")
print()
max_tanks = [[], []]
if len(sys.argv) == 2:
for i in range(2):
print("Player ", i+1)
print("---------")
for j in range(1, 6):
num = int(getpass("Territory {j}: ".format(**locals())))
if 0 <= num or j == 5:
max_tanks[i].append(num)
if max_tanks[i][4] == -1:
max_tanks[i][4] = 100 - sum(max_tanks[0][:4])
print()
else:
max_tanks = [[20, 20, 20 ,20, 20],
[21, 21, 21, 19, 18]]
#print("Player 1 distrubution: ", max_tanks[0])
#print("Player 2 distrubution: ", max_tanks[1])
print()
score = [0, 0, 0]
for game in range(n_games):
score[ play_game(max_tanks) ] += 1
if score[0] > score[1]:
print("Player 1 wins! {0} wins {1} losses {2} draws".format(*score))
elif score[0] < score[1]:
print("Player 2 wins! {1} wins {0} losses {2} draws".format(*score))
else:
print("Its a tie!")
print()
def play_game(tank_distrs):
capacities = [int(random.randrange(1, 100))] * 2
tanks = [[0] * 5 , [0] * 5]
points = 0
for i, (ammo, tank_distr) in enumerate(zip(capacities, tank_distrs)):
for j, n_tanks in enumerate(tank_distr):
if n_tanks <= ammo:
tanks[i][j] = n_tanks
ammo -= n_tanks
else:
tanks[i][j] = ammo
ammo = 0
for n_tanks_1, n_tanks_2 in zip(tanks[0], tanks[1]):
if n_tanks_2 < n_tanks_1:
points += 1
elif n_tanks_2 > n_tanks_1:
points -= 1
if 0 < points: return 0
elif 0 > points: return 1
else: return 2
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment