Okay, I've been programming for awhile in many different languages, but my current obsession is Python. Whenever I get into a new language, I usually write a small program, usually in the form of a dice roller.
My Python dice roller is probably the best I've done yet (rivaling my mIRC script one ^_~), and I'm thrilled that I've been able to use string-splitting functions rather than regular expressions to allow multiple dice combinations. The user specifies a number of dice and number of sides in the form XdY, with the lowercase 'd' separating the two. The user can also put multiple rolls in the same command, which is the part I'm really proud of. The format for this is XdY,MdN,PdQ, etc.
The part I'm looking to improve is the current version returns each die roll as its own string to the terminal. I want to pass the "roll" function a tuple (or something similar) and have the roll function output all the rolls as a single line to the terminal.
Here's the source code.
[code=Python]
#! /usr/bin/python
import random
import re
def roll(dice = 1, sides = 6):
array = [0] * dice # array stores the results
total = 0 # total stores the running count of the dice
output = "* On a roll of " # Contains the beginning of the output string
for pos in range(dice):
array[pos] = random.randint( 1, sides)
total += array[pos] # add the roll to the total
output += str(dice) + "d" + str(sides) + " you rolled "
output += str(array) + " Total: " + str(total)
print output
print "* Input number of dice to roll (XdY), type quit to exit."
rollString = ""
while rollString.lowe r() not in ("quit", "exit"):
rollString = raw_input("* How many dice to roll? Type \"quit\" to exit. ")
rollString = rollString.lowe r()
if rollString in ("quit", "exit"):
break
rollString = rollString.spli t(',')
for x in rollString:
splitString = x.split('d') #split the string at 'd'
if(splitString[1]) == "%":
splitString[1] = 100
try:
roll(int(splitS tring[0]), int(splitString[1]))
except ValueError:
roll(1, int(splitString[1]))
rollString = ""
[/code]
And here's a sample run:
My Python dice roller is probably the best I've done yet (rivaling my mIRC script one ^_~), and I'm thrilled that I've been able to use string-splitting functions rather than regular expressions to allow multiple dice combinations. The user specifies a number of dice and number of sides in the form XdY, with the lowercase 'd' separating the two. The user can also put multiple rolls in the same command, which is the part I'm really proud of. The format for this is XdY,MdN,PdQ, etc.
The part I'm looking to improve is the current version returns each die roll as its own string to the terminal. I want to pass the "roll" function a tuple (or something similar) and have the roll function output all the rolls as a single line to the terminal.
Here's the source code.
[code=Python]
#! /usr/bin/python
import random
import re
def roll(dice = 1, sides = 6):
array = [0] * dice # array stores the results
total = 0 # total stores the running count of the dice
output = "* On a roll of " # Contains the beginning of the output string
for pos in range(dice):
array[pos] = random.randint( 1, sides)
total += array[pos] # add the roll to the total
output += str(dice) + "d" + str(sides) + " you rolled "
output += str(array) + " Total: " + str(total)
print output
print "* Input number of dice to roll (XdY), type quit to exit."
rollString = ""
while rollString.lowe r() not in ("quit", "exit"):
rollString = raw_input("* How many dice to roll? Type \"quit\" to exit. ")
rollString = rollString.lowe r()
if rollString in ("quit", "exit"):
break
rollString = rollString.spli t(',')
for x in rollString:
splitString = x.split('d') #split the string at 'd'
if(splitString[1]) == "%":
splitString[1] = 100
try:
roll(int(splitS tring[0]), int(splitString[1]))
except ValueError:
roll(1, int(splitString[1]))
rollString = ""
[/code]
And here's a sample run:
Code:
* Input number of dice to roll (XdY), type quit to exit. * How many dice to roll? Type "quit" to exit. 3d6 * On a roll of 3d6 you rolled [3, 4, 6] Total: 13 * How many dice to roll? Type "quit" to exit. 2d4,2d6,1d8 * On a roll of 2d4 you rolled [2, 4] Total: 6 * On a roll of 2d6 you rolled [5, 5] Total: 10 * On a roll of 1d8 you rolled [5] Total: 5 * How many dice to roll? Type "quit" to exit. quit
Comment