RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 1927 | Replies: 17
Reply
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Problem with numbers

  #1  
Jul 31st, 2006
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.

#Risk dice simulator#

import random
from time import time

attack = True
defense = True

raw_input("Risk Rolling Simulator")

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
elif dR1 < dR2:
    defense = dR2, dR1
elif dR1 == dR2:
    defense = dR1, dR2
    

if aR1 > aR3 and aR2 > aR3 and aR1 > aR2:
    attack = aR1, aR2
        
elif aR1 > aR3 and aR2 > aR3 and aR2 > aR1:
    attack = aR2, aR1
    
elif aR1 > aR2 and aR3 > aR2 and aR1 > aR3:
    attack = aR1, aR3
    
elif aR1 > aR2 and aR3 > aR2 and aR3 > aR1:
    attack = aR3, aR1
    
elif aR2 > aR1 and aR3 > aR1 and aR2 > aR3:
    attack = aR2, aR3
    
elif aR2 > aR1 and aR3 > aR1 and aR3 > aR2:
    attack = aR3, aR2

elif aR1 > aR3 and aR2 == aR3:
    attack = aR1, aR2
elif aR2 > aR3 and aR1 == aR3:
    attack = aR2, aR1
elif aR3 > aR1 and aR2 == aR1:
    attack = aR3, aR2
elif aR1 < aR3 and aR2 == aR3:
    attack = aR3, aR2
elif aR2 < aR3 and aR1 == aR3:
    attack = aR3, aR1
elif aR3 < aR1 and aR2 == aR1:
    attack = aR1, aR2
elif aR1 == aR2 and aR2 == aR3:
    attack = aR1, aR2

    
print "\nResults are:"
print attack, "vs.", defense

raw_input("")
execfile("Risk.py")
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Problem with numbers

  #2  
Aug 1st, 2006
Hello there,

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

AttackerWins = 0
DefenderWins = 0

if HighestAttackerRoll > HighestDefenderRoll:
    AttackerWins += 1

else:
    DefenderWins += 1

if SecondHighestAttackerRoll > SecondHighestDefenderRoll:
    AttackerWins += 1

else:
    DefenderWins += 1

print "\nOutcome:"
print "Attacker Wins %i" % (AttackerWins)
print "Defender Wins %i" % (DefenderWins)

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

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.
Reply With Quote  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: Problem with numbers

  #3  
Aug 1st, 2006
How do I put this into the code? I'm sorry I'm a noob I know
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Problem with numbers

  #4  
Aug 2nd, 2006
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:
if attack[0] > defense[0]:
    attackerWins += 1
else:
    defenseWins += 1
The second if, else combo does the same thing except it compares the two lower numbers.
In Code:
if attack[1] > defense[1]:
    attackWins += 1
else:
    defenseWins += 1
then at the end of it all, i simply print the results:
print attackWins
print defenseWins

if your still stuck let me know, glad to help

happy coding
Reply With Quote  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: Problem with numbers

  #5  
Aug 2nd, 2006
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.
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Problem with numbers

  #6  
Aug 2nd, 2006
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:
>>> alist = [1,2,3,4]
>>> alist.sort(reverse=True) # if you don't have the reverse, you will end up with it lowest to highest.
>>> print alist
[4,3,2,1]
Last edited by a1eio : Aug 2nd, 2006 at 3:02 pm.
Reply With Quote  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: Problem with numbers

  #7  
Aug 3rd, 2006
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.
Reply With Quote  
Join Date: Jul 2005
Location: France
Posts: 1,051
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 46
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Veteran Poster

Re: Problem with numbers

  #8  
Aug 3rd, 2006
A more foolproof way would be to loop each data entry like this:
# let's say your attacking army can be from 1 tp 100

# keeps looping until entry is acceptable
aArmy = 0
while aArmy < 1 or aArmy > 100:
    try:
        aArmy = input("Enter the size of the attacking army (1 to 100): ")
    except NameError:  # a letter is entered
        aArmy = 0
print aArmy
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Problem with numbers

  #9  
Aug 3rd, 2006
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:
>>> alist = [random.randint(1,6) for num in range(3)]
>>> dlist = [random.randint(1,6) for num in range(2)]
>>> print alist
[4, 6, 1]
>>> print dlist
[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:
>>> alist = []
>>> for num in range(3):
            alist.append(random.randint(1,6))
>>> print alist
[6, 3, 4]

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

happy coding

p.s in your code,
loop = True
while loop:
    ...
you don't need to store True in a variable, just do this:
while True:
    ...
# OR This
while 1:
    ...
Last edited by a1eio : Aug 3rd, 2006 at 1:15 pm.
Reply With Quote  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: Problem with numbers

  #10  
Aug 3rd, 2006
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]
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:45 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC