[Tutor] Help with Dice game
Alan Gauld
alan.gauld at btinternet.com
Tue Nov 11 01:09:36 CET 2014
On 10/11/14 20:57, coryloghry at yahoo.com.dmarc.invalid wrote:
> I wrote the following code as a simulation for the table top game
> x-wing.
I don;t know it so can only give some general comments below...
> import random
> print("X-wing dice simulator")
> x = int(input("How many dice will the offensive player be rolling?\n"))
> y = int(input("How many dice will the defensive player be rolling?\n"))
Might be better to use more descriptive names like 'offense_turns'
and defense_turns'?
> hits = 0
> crits = 0
> dodges = 0
> offense = 0
> defense = 0
> while offense < x:
> odie = random.randint(1,8)
Is it really an 8 sided dice?
> if odie <= 4:
> hits = hits + 1
> offense = offense + 1
> if odie == 4:
> crits = crits + 1
> offense = offense + 1
> else:
> continue
You don't need the else: continue, the loop does that by itself.
Also by not incrementing the offense counter you effectively let the
player have unlimited misses - is that really what you want?
Finally, the Pythonic idiom for incrementing a counter, n, is
n += 1
It saves a few keystrokes...
> while defense < y:
> ddie = random.randint(1,8)
> if ddie <= 3:
> dodges = dodges + 1
> defense = defense + 1
> else:
> continue
Same comments as above
To help you debug this it might be worth adding a few print statements
inside the loop, like so:
while offense < x:
odie = random.randint(1,8)
print('odie is: ',odie)
if odie <= 4:
hits = hits + 1
offense = offense + 1
print('hits, offense = ',hits,offense)
> print("The offensive player lands", hits,"hits and", crits,"crits\n")
> print("The defensive player dodges", dodges, "hits\n")
> print("The offensive player deals", int((hits + crits) - dodges), "to
> the defensive player")
Doing all the printing after the loop finishes gives you a
very limited view of what's happening inside.
hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list