944,008 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2513
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 31st, 2006
0

Problem with numbers

Expand Post »
I have made a program that does the rolling for you for the board game "Risk" I even made it tell only the attackers top two rolled and it says the attacks and defenders rolled in order of highest to lowest.

But I need it to say who has to give up which pieces.

The rules in risk, in case the person who can help me doesn't know, is that you compare your highest role to their highest role and your second with their second. If both your roles are higher, they lose two guys, if one of yours is higher and one of theirs is higher you each lose one, ties are given to the defender.

Python Syntax (Toggle Plain Text)
  1. #Risk dice simulator#
  2.  
  3. import random
  4. from time import time
  5.  
  6. attack = True
  7. defense = True
  8.  
  9. raw_input("Risk Rolling Simulator")
  10.  
  11. aR1 = random.randint(1,6)
  12. aR2 = random.randint(1,6)
  13. aR3 = random.randint(1,6)
  14.  
  15. dR1 = random.randint(1,6)
  16. dR2 = random.randint(1,6)
  17.  
  18. print "Rolled:\n"
  19. print "Attack:"
  20. print aR1, aR2, aR3
  21. print "\nDefense:"
  22. print dR1, dR2
  23.  
  24. if dR1 > dR2:
  25. defense = dR1, dR2
  26. elif dR1 < dR2:
  27. defense = dR2, dR1
  28. elif dR1 == dR2:
  29. defense = dR1, dR2
  30.  
  31.  
  32. if aR1 > aR3 and aR2 > aR3 and aR1 > aR2:
  33. attack = aR1, aR2
  34.  
  35. elif aR1 > aR3 and aR2 > aR3 and aR2 > aR1:
  36. attack = aR2, aR1
  37.  
  38. elif aR1 > aR2 and aR3 > aR2 and aR1 > aR3:
  39. attack = aR1, aR3
  40.  
  41. elif aR1 > aR2 and aR3 > aR2 and aR3 > aR1:
  42. attack = aR3, aR1
  43.  
  44. elif aR2 > aR1 and aR3 > aR1 and aR2 > aR3:
  45. attack = aR2, aR3
  46.  
  47. elif aR2 > aR1 and aR3 > aR1 and aR3 > aR2:
  48. attack = aR3, aR2
  49.  
  50. elif aR1 > aR3 and aR2 == aR3:
  51. attack = aR1, aR2
  52. elif aR2 > aR3 and aR1 == aR3:
  53. attack = aR2, aR1
  54. elif aR3 > aR1 and aR2 == aR1:
  55. attack = aR3, aR2
  56. elif aR1 < aR3 and aR2 == aR3:
  57. attack = aR3, aR2
  58. elif aR2 < aR3 and aR1 == aR3:
  59. attack = aR3, aR1
  60. elif aR3 < aR1 and aR2 == aR1:
  61. attack = aR1, aR2
  62. elif aR1 == aR2 and aR2 == aR3:
  63. attack = aR1, aR2
  64.  
  65.  
  66. print "\nResults are:"
  67. print attack, "vs.", defense
  68.  
  69. raw_input("")
  70. execfile("Risk.py")
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Aug 1st, 2006
0

Re: Problem with numbers

Hello there,

i've been playing around and this seems to have the right result for me:

Python Syntax (Toggle Plain Text)
  1. AttackerWins = 0
  2. DefenderWins = 0
  3.  
  4. if HighestAttackerRoll > HighestDefenderRoll:
  5. AttackerWins += 1
  6.  
  7. else:
  8. DefenderWins += 1
  9.  
  10. if SecondHighestAttackerRoll > SecondHighestDefenderRoll:
  11. AttackerWins += 1
  12.  
  13. else:
  14. DefenderWins += 1
  15.  
  16. print "\nOutcome:"
  17. print "Attacker Wins %i" % (AttackerWins)
  18. print "Defender Wins %i" % (DefenderWins)

the result with attack rolls 3, 2 vs defenders rolls 6 and 5:

Quote ...
Outcome:
Attacker Wins 0
Defender Wins 2
i've made some other changes if you want to me to show you feel free to ask, and i'll explain, but thats an answer to the problem

happy coding
Last edited by a1eio; Aug 1st, 2006 at 5:32 am.
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 1st, 2006
0

Re: Problem with numbers

How do I put this into the code? I'm sorry I'm a noob I know
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Aug 2nd, 2006
0

Re: Problem with numbers

hey no probs i always find it hard understanding other peoples code.

basically, you want to work out who wins right?

well if i'm right, then all you need to do is put that if, else, if, else section of my code into yours, however you will need to change some of the stuff so it works with your code.

to simplify it:

you have a tuple called attack and a tuple called defense right? each contains two numbers, the first being the higher dice roll and the second being the lower dice roll.
To work out the results you need to compare the first number of the attacker against the first number of the defender. If the attackers number is higher than the defenders number then the attacker gains a point. (or in the case of risk, the loser loses a unit) however if not, then the defender gains the point (again in the case of risk; loser loses a unit)
In Code:
Python Syntax (Toggle Plain Text)
  1. if attack[0] > defense[0]:
  2. attackerWins += 1
  3. else:
  4. defenseWins += 1
The second if, else combo does the same thing except it compares the two lower numbers.
In Code:
Python Syntax (Toggle Plain Text)
  1. if attack[1] > defense[1]:
  2. attackWins += 1
  3. else:
  4. defenseWins += 1
then at the end of it all, i simply print the results:
Python Syntax (Toggle Plain Text)
  1. print attackWins
  2. print defenseWins

if your still stuck let me know, glad to help

happy coding
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 2nd, 2006
0

Re: Problem with numbers

I actually found my own way around it while I was waiting for a reply. I know many will look at my code and laugh at how pathetic it is. Because I did it by running through every possible scenario, so my code is not as neat as it could be and is way bigger than it has to be, but meh. I did manage to make the program much more elaborate once I got pased my first problem, but now I have a much harder problem.

[php]#Risk dice simulator#

print "Risk Rolling Simulator"
attackForce = input("\nEnter the number of attacking units. ")
defenseForce = input("Enter the number of defending units. ")

import random
from time import time
loop = True

while loop:

attack = 0
defense = 0
attackW = 0
defenseW = 0
men = "men."
men2 = "men."

print "Attacker has", attackForce,". While defender has", defenseForce,"."
aR1 = random.randint(1,6)
aR2 = random.randint(1,6)
aR3 = random.randint(1,6)

dR1 = random.randint(1,6)
dR2 = random.randint(1,6)

print "Rolled:\n"
print "Attack:"
print aR1, aR2, aR3
print "\nDefense:"
print dR1, dR2

if dR1 > dR2:
defense = dR1, dR2
dH = dR1
dL = dR2
elif dR1 < dR2:
defense = dR2, dR1
dH = dR2
dL = dR1
elif dR1 == dR2:
defense = dR1, dR2
dH = dR1
dL = dR2


if aR1 > aR3 and aR2 > aR3 and aR1 > aR2:
attack = aR1, aR2
aH = aR1
aL = aR2
elif aR1 > aR3 and aR2 > aR3 and aR2 > aR1:
attack = aR2, aR1
aH = aR2
aL = aR1
elif aR1 > aR2 and aR3 > aR2 and aR1 > aR3:
attack = aR1, aR3
aH = aR1
aL = aR3
elif aR1 > aR2 and aR3 > aR2 and aR3 > aR1:
attack = aR3, aR1
aH = aR3
aL = aR1
elif aR2 > aR1 and aR3 > aR1 and aR2 > aR3:
attack = aR2, aR3
aH = aR2
aL = aR3
elif aR2 > aR1 and aR3 > aR1 and aR3 > aR2:
attack = aR3, aR2
aH = aR3
aL = aR2
elif aR1 > aR3 and aR2 == aR3:
attack = aR1, aR2
aH = aR1
aL = aR2
elif aR2 > aR3 and aR1 == aR3:
attack = aR2, aR1
aH = aR2
aL = aR1
elif aR3 > aR1 and aR2 == aR1:
attack = aR3, aR2
aH = aR3
aL = aR2
elif aR1 < aR3 and aR2 == aR3:
attack = aR3, aR2
aH = aR3
aL = aR2
elif aR2 < aR3 and aR1 == aR3:
attack = aR3, aR1
aH = aR3
aL = aR1
elif aR3 < aR1 and aR2 == aR1:
attack = aR1, aR2
aH = aR1
aL = aR2
elif aR1 == aR2 and aR2 == aR3:
attack = aR1, aR2
aH = aR1
aL = aR2

if aH > dH and aL > dL:
attackW = 2
elif aH > dH and aL < dL:
attackW = 1
defenseW = 1
elif aH > dH and aL == dL:
attackW = 1
defenseW = 1
elif aH < dH and aL > dL:
attackW = 1
defenseW = 1
elif aH == dH and aL > dL:
attackW = 1
defenseW = 1
elif aH < dH and aL < dL:
defenseW = 2
elif aH == dH and aL < dL:
defenseW = 2
elif aH == dH and aL == dL:
defenseW = 2
elif aH < dH and aL == dL:
defenseW = 2

if attackW == 1:
men = "man."
if defenseW == 1:
men2 = "man."

if attackW == 2:
defenseForce -= 2
elif defenseW == 2:
attackForce -= 2
else:
defenseForce -= 1
attackForce -= 1


print "\nResults are:"
print attack, "vs.", defense
print "\nAttacker looses", defenseW, men2
print "Defender looses", attackW, men
print "Attacker now has", attackForce,". While defender has", defenseForce,"."

if attackForce == 1:
Fmen2 = "man"
else:
Fmen2 = "men"
if defenseForce == 1:
Fmen = "man"
else:
Fmen = "men"

if attackForce == 0 or attackForce < 0:
print "The defender wins with", defenseForce, Fmen, "remaining."
break
elif defenseForce == 0 or defenseForce < 0:
print "The attacker wins with", attackForce, Fmen2, "remaining."
break

raw_input("\nPress [Enter] for a new assault\n")
execfile("Risk.py")[/php]
My new problem is that in a real game of risk, an attacker and a defender will not always role 3 dice or 2 dice, sometimes they will roll a smaller number of dice. ex. an attacker may want to use 3 dice, but a defender only has 1 man left so he has to use 1 die. I need to know how to do that.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Aug 2nd, 2006
0

Re: Problem with numbers

hehe i read about that little scenario when i looked up the rules of risk myself, seems like a complicated little game although i didn't read it that indepth. i'll mess about with mine, see what i can get working or not working.

p.s by the way that bit you do to sort the numbers from highest to lowest. python has a sort function for lists:
Python Syntax (Toggle Plain Text)
  1. >>> alist = [1,2,3,4]
  2. >>> alist.sort(reverse=True) # if you don't have the reverse, you will end up with it lowest to highest.
  3. >>> print alist
  4. [4,3,2,1]
Last edited by a1eio; Aug 2nd, 2006 at 3:02 pm.
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 3rd, 2006
0

Re: Problem with numbers

I thank you VERY much for telling me about lists. I tinkered with them and have been able to pretty much make the same program run on alot less code.

[php]import random

print "---Risk attack simulator---"
aArmy = input("Enter the size of the attacking army: ")
dArmy = input("Enter the size of the defending army: ")

loop = True
while loop:

a1 = random.randint(1,6)
a2 = random.randint(1,6)
a3 = random.randint(1,6)
d1 = random.randint(1,6)
d2 = random.randint(1,6)

alist = [a1,a2,a3]
dlist = [d1,d2]

alist.sort(reverse = True)
dlist.sort(reverse = True)

print "\nAttacker rolls:", alist[0], alist[1], alist[2]
print "Defender rolls:", dlist[0], dlist[1]
print "\nResults:"
print alist[0],alist[1], "vs.", dlist[0],dlist[1]

roll1 = alist[0] - 1
roll2 = alist[1] - 1

aCount = 0
dCount = 0

if alist[0] > dlist[0]:
dArmy -= 1
dCount += 1
if alist [1] > dlist[1]:
dArmy -= 1
dCount += 1
if dlist[0] > roll1:
aArmy -= 1
aCount += 1
if dlist[1] > roll2:
aArmy -= 1
aCount += 1

print "\nAttacker looses:",aCount
print "Defender looses:",dCount
print "\nAttacker now has:",aArmy
print "Defender now has:",dArmy

if dArmy < 1 or aArmy < 1:
break

raw_input("")
execfile("RiskV2.py")
[/php]

One quick question: How do I make the program refuse to take "0" or no answer for when it asks about the armies? If nothing is entered, then it errors and if 0 is entered... then that just doesn't make sense.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Aug 3rd, 2006
0

Re: Problem with numbers

A more foolproof way would be to loop each data entry like this:
Python Syntax (Toggle Plain Text)
  1. # let's say your attacking army can be from 1 tp 100
  2.  
  3. # keeps looping until entry is acceptable
  4. aArmy = 0
  5. while aArmy < 1 or aArmy > 100:
  6. try:
  7. aArmy = input("Enter the size of the attacking army (1 to 100): ")
  8. except NameError: # a letter is entered
  9. aArmy = 0
  10. print aArmy
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Aug 3rd, 2006
0

Re: Problem with numbers

glad you liked em, lists are wonderful things, lots of things can be done with them. Another great thing i love about lists is something called list comprehension.
List comprehension works in the following way:
[expression for item in sequence (if condition not necesary)] # Thats the rough outline
# Here's a simple example
>>> [1 for num in range(5)]
[1, 1, 1, 1, 1]
First i'm telling python to put '1' in the list, then i tell python it's going to keep putting '1' inside it untill the 'for num in range(5)' stops looping.

Here's an example that fits your code:
Python Syntax (Toggle Plain Text)
  1. >>> alist = [random.randint(1,6) for num in range(3)]
  2. >>> dlist = [random.randint(1,6) for num in range(2)]
  3. >>> print alist
  4. [4, 6, 1]
  5. >>> print dlist
  6. [3, 5]
In the example above, python understands that random.randint(1,6) is going to be placed in the list 3 times (3 times, because the for loop is looping 3 times; range(3))
list comprehension, simply 'compreses' loops eg if you wanted to do the above without using list comprehension, you would simply use a for loop:
Python Syntax (Toggle Plain Text)
  1. >>> alist = []
  2. >>> for num in range(3):
  3. alist.append(random.randint(1,6))
  4. >>> print alist
  5. [6, 3, 4]

if you need any more info on list comprehension, just ask.

happy coding

p.s in your code,
Python Syntax (Toggle Plain Text)
  1. loop = True
  2. while loop:
  3. ...
you don't need to store True in a variable, just do this:
Python Syntax (Toggle Plain Text)
  1. while True:
  2. ...
  3. # OR This
  4. while 1:
  5. ...
Last edited by a1eio; Aug 3rd, 2006 at 1:15 pm.
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 3rd, 2006
0

Re: Problem with numbers

alright I was able to finish the code, and now it changes the number of dice rolled according to the size of the armies. Again, like my orginal code, I'm sure there is way more code than needed for this. But it was the only way I could come up with a solution.

Code:
[php]import random

dashes = "======================================"
clear = "\n"*100

print "---Risk attack simulator---\n"

aArmy = 0
while aArmy < 2:
try:
aArmy = input("Enter the size of the attacking army (2 or more): ")
except NameError:
aArmy = 0

dArmy = 0
while dArmy < 1:
try:
dArmy = input("Enter the size of the defending army (1 or more): ")
except NameError:
dArmy = 0

while True:

if aArmy > 3:
aNum = 3
elif aArmy == 3:
aNum = 2
elif aArmy == 2:
aNum = 1
elif aArmy == 1:
break

if dArmy > 1:
dNum = 2
elif dArmy == 1:
dNum = 1

alist = [random.randint(1,6) for num in range(aNum)]
dlist = [random.randint(1,6) for num in range(dNum)]

alist.sort(reverse = True)
dlist.sort(reverse = True)

if aNum == 3 and dNum == 2:
print dashes
print "Attacker rolls:", alist[0], alist[1], alist[2]
print "Defender rolls:", dlist[0], dlist[1]
print "\nResults:"
print alist[0],alist[1], "vs.", dlist[0],dlist[1]

roll1 = alist[0] - 1
roll2 = alist[1] - 1

aCount = 0
dCount = 0

if alist[0] > dlist[0]:
dArmy -= 1
dCount += 1
if alist [1] > dlist[1]:
dArmy -= 1
dCount += 1
if dlist[0] > roll1:
aArmy -= 1
aCount += 1
if dlist[1] > roll2:
aArmy -= 1
aCount += 1

print "\nAttacker looses:",aCount
print "Defender looses:",dCount
print "\nAttacker now has:",aArmy
print "Defender now has:",dArmy
print dashes

if aNum == 3 and dNum == 1:
print dashes
print "Attacker rolls:", alist[0], alist[1], alist[2]
print "Defender rolls:", dlist[0]
print "\nResults:"
print alist[0], "vs.", dlist[0]

roll1 = alist[0] - 1

aCount = 0
dCount = 0

if alist[0] > dlist[0]:
dArmy -= 1
dCount += 1
if dlist[0] > roll1:
aArmy -= 1
aCount += 1

print "\nAttacker looses:",aCount
print "Defender looses:",dCount
print "\nAttacker now has:",aArmy
print "Defender now has:",dArmy
print dashes

if aNum == 2 and dNum == 2:
print dashes
print "Attacker rolls:", alist[0], alist[1]
print "Defender rolls:", dlist[0], dlist[1]
print "\nResults:"
print alist[0],alist[1], "vs.", dlist[0],dlist[1]

roll1 = alist[0] - 1
roll2 = alist[1] - 1

aCount = 0
dCount = 0

if alist[0] > dlist[0]:
dArmy -= 1
dCount += 1
if alist [1] > dlist[1]:
dArmy -= 1
dCount += 1
if dlist[0] > roll1:
aArmy -= 1
aCount += 1
if dlist[1] > roll2:
aArmy -= 1
aCount += 1

print "\nAttacker looses:",aCount
print "Defender looses:",dCount
print "\nAttacker now has:",aArmy
print "Defender now has:",dArmy
print dashes

if aNum == 2 and dNum == 1:
print dashes
print "Attacker rolls:", alist[0], alist[1]
print "Defender rolls:", dlist[0]
print "\nResults:"
print alist[0], "vs.", dlist[0]

roll1 = alist[0] - 1

aCount = 0
dCount = 0

if alist[0] > dlist[0]:
dArmy -= 1
dCount += 1
if dlist[0] > roll1:
aArmy -= 1
aCount += 1

print "\nAttacker looses:",aCount
print "Defender looses:",dCount
print "\nAttacker now has:",aArmy
print "Defender now has:",dArmy
print dashes

if aNum == 1 and dNum == 2:
print dashes
print "Attacker rolls:", alist[0]
print "Defender rolls:", dlist[0], dlist[1]
print "\nResults:"
print alist[0], "vs.", dlist[0]

roll1 = alist[0] - 1

aCount = 0
dCount = 0

if alist[0] > dlist[0]:
dArmy -= 1
dCount += 1
if dlist[0] > roll1:
aArmy -= 1
aCount += 1

print "\nAttacker looses:",aCount
print "Defender looses:",dCount
print "\nAttacker now has:",aArmy
print "Defender now has:",dArmy
print dashes

if aNum == 1 and dNum == 1:
print dashes
print "Attacker rolls:", alist[0]
print "Defender rolls:", dlist[0]
print "\nResults:"
print alist[0], "vs.", dlist[0]

roll1 = alist[0] - 1

aCount = 0
dCount = 0

if alist[0] > dlist[0]:
dArmy -= 1
dCount += 1
if dlist[0] > roll1:
aArmy -= 1
aCount += 1

print "\nAttacker looses:",aCount
print "Defender looses:",dCount
print "\nAttacker now has:",aArmy
print "Defender now has:",dArmy
print dashes

if dArmy < 1 or aArmy < 2 :
break

if aArmy == 1:
print "The defending army held off the assault with", dArmy, "left."
if dArmy == 0:
print "The attacking army won with", aArmy, "left."

raw_input("\n==Press [enter] for a new assault==")
print clear
execfile("RiskV2.py")
[/php]
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python for web design
Next Thread in Python Forum Timeline: range().remove()?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC