I programmed a Simulator for the board game Risk that simulates the battles between defending and attacking troops. I then added lines of code to create a text file named after a standard 'Battlerreport', followed by the date/time
I wrote the simulating code with while loops and if statements so the code outputs the modified number of attacking troops after every 'roll'
My question is: How do I get python to create the file and THEN run the simulation AND write str(Number of attackers) + str(Number of defenders) after each 'roll' into the text file, so the entire battle development is displayed?
Basically I need to define a starting point where the file first gets edited and then loop the writing so Python writes the two numbers in one line, then switches to the next line and so on... The main problem is, that python doesn't know how many rolls there will be before the code gets executed, so in that way the lines the script is supposed to edit is unknown..? PLS HELP :/ For anyone that wants to check my code and/or has suggestions for improvement:
from time import strftime from os import path
def createFile(dest):
''' The script creates a text file at the passed in location, names file based on date '''
#FileName= Battlereport_Year_Month_Day_Hour_Minute_Second.txt
name = 'Battlereport_' + strftime("%Y_%m_%d_%H_%M_%S.txt")
if not (path.isfile(dest + name)):
saveFile = open(dest + name, 'w')
o = ["1", "21", "31"]
t = ["2", "22"]
e = ["3", "23"]
date_day = strftime("%d")
for date_day in o:
date = strftime("%B %dst, %Y at %H:%M:%S")
for date_day in t:
date = strftime("%B %dnd, %Y at %H:%M:%S")
for date_day in e:
date = strftime("%B %drd, %Y at %H:%M:%S")
else:
date = strftime("%B %dth, %Y at %H:%M:%S")
if A > D:
s = (str(A), str(D))
text = 'Battle report from {}\n\n\n\n{}\n\nCongratulations! The attacking force has conquered the lands!'.format(date, s)
else:
s = (str(A), str(D))
text = 'Battle report from {}\n\n\n\n{}\n\nCongratulations! The defense has held strong!'.format(date, s)
saveFile.write(str(text))
import random
A = int(input("Number of attacking troops: ")) D = int(input("Number of defending troops: "))
while A >= 5 and D >= 3:
V = random.randint(1, 6)
W = random.randint(1, 6)
X = random.randint(1, 6)
Y = random.randint(1, 6)
Z = random.randint(1, 6)
Attacker = [V, W, X]
Attacker2 = [V, W]
Defender = [Y, Z]
a1 = max([Attacker])
a2 = max(n for n in Attacker if n != a1)
d1 = max([Defender])
d2 = max(n for n in Defender if n != d1)
a1_2 = max([Attacker2])
a2_2 = max(n for n in Attacker2 if n != a1_2)
if a1 > d1:
D -= 1
else:
A -= 1
if a2 > d2:
D -= 1
else:
A -= 1
print(str(A), str(D))
if A >= 4 and D > 2:
if a1 > d1:
D -= 1
else:
A -= 1
if a2 > d2:
D -= 1
else:
A -= 1
print(str(A), str(D))
if A >= 4 and D == 2:
if a1 > d1:
D -= 1
else:
A -= 1
if a2 > d2:
D -= 1
if D == 0:
print(str(A), str(D))
print("Congratulations! The attacking force has won!")
else:
A -= 1
print(str(A), str(D))
if A == 3 and D == 2:
if a1_2 > d1:
D -= 1
else:
A -= 1
if a2_2 > d2:
D -= 1
else:
A -= 1
if A == 1:
print(str(A), str(D))
print("Congratulations! The defense held strong!")
if A == 3 and D >= 2:
if a1_2 > d1:
D -= 1
else:
A -= 1
if a2_2 > d2:
D -= 1
else:
A -= 1
if A == 1:
print(str(A), str(D))
print("Congratulations! The defense held strong!")
if A == 2 and D >= 2:
if [V] > d1:
D -= 1
else:
A -= 1
if A == 1:
print(str(A), str(D))
print("Congratulations! The defense held strong!")
while A >=2 and D == 1:
V = random.randint(1, 6)
W = random.randint(1, 6)
X = random.randint(1, 6)
Y = random.randint(1, 6)
Z = random.randint(1, 6)
Attacker = [V, W, X]
Attacker2 = [V, W]
Defender = [Y, Z]
a1 = max([Attacker])
a2 = max(n for n in Attacker if n != a1)
d1 = max([Defender])
d2 = max(n for n in Defender if n != d1)
a1_2 = max([Attacker2])
a2_2 = max(n for n in Attacker2 if n != a1_2)
s = (str(A), str(D))
if A >= 4 and D == 1:
if a1 > [Y]:
D -= 1
if D == 0:
print(str(A), str(D))
print("Congratulations! The attacking force has won!")
else:
A -= 1
print(str(A), str(D))
if A == 3 and D == 1:
if a1_2 > [Y]:
D -= 1
if D == 0:
print(str(A), str(D))
print("Congratulations! The attacking force has won!")
else:
A -= 1
print(str(A), str(D))
if A == 2 and D == 1:
if V > Y:
D -= 1
if D == 0:
print(str(A), str(D))
print("Congratulations! The attacking force has won!")
else:
A -= 1
print(str(A), str(D))
print("Congratulations! The defense held strong!")
if name == "main": destination = 'C:\Users\vannoyi\Desktop\Battlereport\' createFile(destination) input('done')
saveFile.close()
I greatly appreciate any kind of help!
Aucun commentaire:
Enregistrer un commentaire