我为棋盘游戏Risk编写了一个模拟器,用来模拟防御与进攻部队之间的战斗。 然后,我添加了几行代码来创建一个文本文件,该文件以标准的“ Battlerreport”命名,后跟日期/时间
我用while循环和if语句编写了模拟代码,因此该代码在每次“滚动”后输出经过修改的攻击部队数量
我的问题是:我如何获取python创建文件,然后运行模拟并在每次“滚动”到文本文件后写入str(攻击者数量)+ str(防御者数量),所以显示整个战斗进展情况?
基本上,我需要定义一个起点,在该起点上首先编辑文件,然后循环编写,以便Python在一行中写入两个数字,然后切换到下一行,依此类推...主要问题是, python在执行代码之前不知道要进行多少次滚动,因此以这种方式脚本应该编辑的行是未知的。 PLS HELP:/对于想要检查我的代码和/或有改进建议的人:
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()
这是文本文件当前收到的内容:
2019年3月4日10:08:04起的战斗报告
('19','0')
恭喜!进攻部队征服了土地!
我不仅希望在战斗结束时算入部队,我还希望看到档案中的发展情况,从42个进攻和23个防御部队一直到最后的剩余部队。战斗!
我非常感谢您提供的任何帮助!
答案 0 :(得分:0)
我对您的代码进行了一些更改,以使攻击者和防御者的数量在每次掷骰后都存储在列表中。然后,当您调用createFile()函数时,会将此列表写入文件。
我在更改的每一行上方添加了注释,以使其更加清晰。
from time import strftime
from os import path
def createFile():
'''
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(name)):
saveFile = open(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")
#write the troops list to the file
for i in troops:
saveFile.write(str(i) + "\n")
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: "))
#create a list that contains the number of troops
troops = [(A, D)]
while A >= 5 and D >= 3:
s = (str(A), str(D))
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))
#add the changed troops number to the list
troops.append((A, 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))
#add the changed troops number to the list
troops.append((A, 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))
#add the changed troops number to the list
troops.append((A, 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!")
#add the changed troops number to the list
troops.append((A, 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!")
#add the changed troops number to the list
troops.append((A, D))
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!")
#add the changed troops number to the list
troops.append((A, D))
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))
#add the changed troops number to the list
troops.append((A, 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!")
#add the changed troops number to the list
troops.append((A, D))
if __name__ == "__main__":
destination = 'C:\\Users\\vannoyi\\Desktop\\Battlereport\\'
createFile(destination)
input('done')
saveFile.close()