[英]python risk game i can't get it to compare all the numbers only the last ones
i am making a game that will sort out the risk dice rolling but i can only get it to compare the last two numbers and tell me who has won but i want it to order the numbers then compare the top numbers from each set ( like in a normal risk game) here is my code 我正在制作一款可以解决风险骰子滚动的游戏,但是我只能让它比较最后两个数字并告诉我谁赢了,但是我希望它对数字进行排序,然后比较每组中的最高数字(例如一个普通的风险游戏)这是我的代码
import random
import time
from random import randrange
from random import randint
r = randint
min = 1
max = 6
def main():
print("Welcome to risk dice")
playing = input("Press Enter to Roll or Q to quit")
if playing == 'q':
print("Thanks for Playing")
time.sleep(2)
quit()
elif playing != 'q':
rolls = int(float(input('how many soliders are you attacking with:')))
for x in range(rolls):
print ('you got a...')
a = (r(min, max))
print(a)
rolls = int(float(input('how many soliders are you defending with:')))
for x in range(rolls):
print ('you got a...')
d = (r(min, max))
print(d)
if d >= a:
print('defence wins')
else:
print('attackers win')
main()
main()
any ideas are welcome im completely stuck (im using python 3.4.4) 欢迎任何想法,即时通讯完全卡住(使用python 3.4.4)
attacker_dice = []
defender_dice = []
Initialize empty arrays to put the dice in as we roll them. 初始化空数组以在掷出骰子时放入骰子。
rolls = int(float(input('how many soliders are you attacking with:')))
for x in range(rolls):
print ('you got a...')
attacker_dice.append(r(min, max))
print(attacker_dice[-1])
Here we append
the new values to the arrays, which adds them to the end. 在这里,我们
append
新值append
到数组,然后将它们添加到末尾。 Then, we print the number at index [-1]
which is the last item. 然后,我们在最后一个项目的索引
[-1]
处打印数字。
attacker_dice.sort(reverse=True)
defender_dice.sort(reverse=True)
Once we have the arrays, we sort (from high to low) 一旦有了数组,我们就进行排序(从高到低)
paired_dice = zip(attacker_dice, defender_dice)
Then, we pair the dice with zip. 然后,我们将骰子与拉链配对。 Zip by default drops any dice that can't be paired.
默认情况下,Zip丢弃所有无法配对的骰子。 This mirrors the Risk rules, that you drop the lowest unpaired dice.
这反映了风险规则,即您丢弃了最低的未配对骰子。
attacker_wins = 0
defender_wins = 0
for pair in paired_dice:
attacker, defender = pair
Here we unpack the dice: for pair in paired_dice
loops between each pair. 在这里,我们解开骰子的
for pair in paired_dice
: for pair in paired_dice
每对之间的for pair in paired_dice
循环中的对。 attacker, defender = pair
unpacks the pairs into their component dice. attacker, defender = pair
将对解包到其组件骰子中。
if attacker > defender:
attacker_wins += 1
else:
defender_wins += 1
It's important to note for Risk, attacker_wins
is how many armies the defender loses and vice versa. 重要的是要注意“风险”,
attacker_wins
是防御者失去多少军队,反之亦然。
This example works on repl.it . 本示例适用于repl.it。 Since the input in not constrained, I tested it to 50 on 50 and it works just fine.
由于输入不受限制,因此我在50上将其测试为50,并且效果很好。
i have edited galra's scripted to get exactly what i want (galra's scripted didn't work but i used some of the ideas) 我已经编辑了galra的脚本来获得我想要的(galra的脚本没有用,但是我使用了一些想法)
import random
import time
from random import randrange
from random import randint
r = randint
min = 1
max = 6
def main():
print("Welcome to risk dice")
playing = input("Press Enter to Roll or Q to quit")
if playing == 'q':
print("Thanks for Playing")
time.sleep(2)
quit()
elif playing != 'q':
rolls = int(float(input('how many soliders are you attacking with:')))
a = [ r(min, max) for x in range(rolls) ]
a.sort(reverse=True)
print('you got a...')
print(a)
rolls = int(float(input('how many soliders are you defending with:')))
d = [ r(min, max) for x in range(rolls) ]
d.sort(reverse=True)
print('you got a...')
print(d)
if a <= d:
print('defence wins the first battal')
elif a > d:
print('attackers win the first battal')
a2 = a[1]
d2 = d[1]
if a2 <= d2:
print('defence wins the second battal')
elif a2 > d2:
print('attackers win the second battal')
main()
main()
thanks mostly to TemporalWolf for your advice i haven't checked to see if your answer works but this one works for me 多亏了TemporalWolf的建议,我尚未检查您的答案是否有效,但这对我有用
I'm not familiar with the game, but I believe that's what you're asking for. 我对游戏不熟悉,但是我相信这就是您所要的。 What's different?
有什么不同? Now the code rolls all the attacker turns one after the other, saves them to a list and printing the results, following by the same procedure for the defender.
现在,代码将所有攻击者一个又一个的回合滚动,将它们保存到列表中并打印结果,然后按照与防御者相同的过程进行操作。 After both player's rolls have been evaluated, the short one is padded to fit the length of the long one (assigning a value of 0 to every missing roll).
在评估了两个玩家的掷骰之后,对短的掷骰进行填充以适合长的掷骰的长度(为每个丢失的掷骰分配0值)。 The two are sorted and compared.
将两者进行排序和比较。 As I mentioned, I don't know the game, but this should give you a start for comparing all the rolls one to another.
正如我提到的,我不了解游戏,但这应该为您提供一个开始,将所有掷骰进行比较。
import random
import time
from random import randrange
from random import randint
r = randint
min = 1
max = 6
def main():
print("Welcome to risk dice")
playing = input("Press Enter to Roll or Q to quit")
if playing == 'q':
print("Thanks for Playing")
time.sleep(2)
quit()
elif playing != 'q':
rolls = int(float(input('how many soliders are you attacking with:')))
# evaluating all the attacker rolls
rolls_attack_res = [ r(min, max) for i in range(rolls) ]
print('you got a...')
print('\n'.join(rolls_attack_res))
rolls = int(float(input('how many soliders are you defending with:')))
# evaluating all the defender rolls
rolls_defense_res = [ r(min, max) for i in range(rolls) ]
print('you got a...')
print('\n'.join(rolls_defense_res))
# filling the missing rolls for the player with less soldiers
max_len = max(len(rolls_attack_res), len(rolls_defense_res))
rolls_attack_res.extend([0] * (max_len - len(rolls_attack_res)))
rolls_defense_res.extend([0] * (max_len - len(rolls_defense_res)))
# sorting the rolls of both players
rolls_attack_res.sort()
rolls_defense_res.sort()
# comparing the rolls, highest to highest, and following.
rolls_res = zip(rolls_attack_res, rolls_defense_res)
num_of_attack_wins = len([ 1 for a,d in rolls_res if a > d ])
num_of_defense_wins = len(rolls_res) - num_of_attack_wins
if num_of_defense_wins >= num_of_attack_wins:
print('defence wins')
else:
print('attackers win')
main()
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.