Hello.I'm working on a d20 based game system and I'm pretty new to python.What I've done so far is:
create 6 random rolls with values 3-18
rollOne
rollTwo
rollThree etc.
There are 6 stats that need theese rolls assigned
statOne
statTwo
etc.
I've done the part on how to assign rolls and it works,but:
What can I do to prevent using same roll for two stats?(eg.rollOne can be used for statOne and other stats,and it shouldn't).thank you.for complete source code and further explications contact me by e-mail muntean.horia@yahoo.com

Recommended Answers

All 6 Replies

The easiest way to do this is to use python's inbuilt random library, with the randint function. This function takes two integers, which define the range between which you want to get a random number.

You can use it like so:

from random import *

def setStats(numberOfStats):
    stats = []
    
    while numberOfStats > 0:
        stat = randint(3, 18)
        stats.append(stat)
        
        numberOfStats -= 1
        
    print stats
        
setStats(6)

Enjoy the gaming! This was how I got into python too.

thank you for the fast reply.I have used the random function,this works just fine.
I have #This will simulate 6 rolls of 3d6
rollOne = random.randint(3, 18)
rollTwo = random.randint(3, 18)
rollThree = random.randint(3,18)
rollFour = random.randint(3, 18)
rollFive = random.randint(3, 18)
rollSix = random.randint(3, 18)

and
#I've done the assignment system.can't write the whole code

strength = ' '
dexterity = ' '
constitution = ' '
intelligence = ' '
wisdom = ' '
charisma = ' '

The problem I was talking about is,that I CAN assign rollOne(for eg.) for more than one stat.and each of the six stats must have ONE DIFFERENT ROLL assigned...what can I do to prevent this?
If I use a lot of 'if' after each stat has been defined it won't work,because,look at this example:


#this will assign rollOne for strength stat
def chooseStrength():
strengthChosen = raw_input()
strengthChosen = int(strengthChosen)
if strengthChosen == 1:
rollOne = str(rollOne)
print 'You have chosen the first roll as your strength.Your strength is ' + rollOne + '.'
strength = rollOne

if I do something like:

if dexterityChosen == strengthChosen:
chooseStrenght()

it's not OK because "if dexterityChosen == strengthChosen:" means: if int(dexterityChosen) == int(strengthChosen) , and both rollOne and rollTwo can have the same value...

What can I do to prevent using same roll for two stats?

Append the random rolls to a list and use each value in the list once only. If I misunderstand and this is not what you want, then post back.

import random

## random can come up with the same number twice, so append
## integers until 6 unique numbers are in the list
def random_rolls():
   rolls_list = []
   ctr = 0
   while ctr < 6:
      x = random.randint(3,18)
      if x not in rolls_list:
         rolls_list.append(x)
         ctr += 1
   return rolls_list
         
#this will assign rollOne for strength stat
def chooseStrength(rolls_list):
   strengthChosen = raw_input("Choose a roll number between 1 and %d: " \
                               % (len(rolls_list)))

   ##  arrays are accessed via memory offset, so use 
   ##  zero through 5 instead of 1 --> 6 (one less)
   strengthChosen_int = int(strengthChosen) - 1
   strength = rolls_list[strengthChosen_int]
   print 'You have chosen the %s roll as your strength.Your strength is %d.' \
         % (strengthChosen, strength)

   ## now delete that number so it's not used again
   del rolls_list[strengthChosen_int]
   return strength, rolls_list

##-------------------------------------------------------------------
rolls_list = random_rolls()
print "You would use these numbers for the rolls:"
for x in rolls_list:
   print x
   
print "\nLet's do it again to test for different numbers"      
rolls_list = random_rolls()
print "You would use these numbers for the rolls:"
for x in rolls_list:
   print x

## copy the list if you want to keep the original
orig_rolls = rolls_list[:]

##  now chooseStrength as an example of how to use it
strength, rolls_list = chooseStrength(rolls_list)
print "Strength has been set to", strength
print "\nand rolls_list is now smaller"
for x in rolls_list:
   print x

And it might be easier to assign each roll to a dictionary key pointing to "strength", etc. or vice-versa. Also, within a class, you can define the list or dictionary as
self.rolls_list = []
and it will be visible throughout the class, i.e. you don't have to pass and return to/from functions.

woooee, I don't think he/she's concerned with having unique stat rolls (at least that's not the case in most d20 games).

Horia.Muntean, I think your problem is that you have too many functions. You shouldn't have one for each stat, but rather one function that can handle them all.

Here's how I would do it. Give it a try and see how you like it. It creates a list of stats based on the number the user enters (so six for D&D), then pops one stat out of the list each time the user asks for a specific stat, like Strength.

from random import *

def setStats(numberOfStats):
    stats = []
    
    while numberOfStats > 0:
        stat = randint(3, 18)
        stats.append(stat)
        
        numberOfStats -= 1
        
    return stats

def chooseStat(statsList):
    statName = raw_input("Choose the stat you're rolling for now: ")
    
    stat = statsList.pop()
    
    print "Your " + statName + " stat is " + str(stat) + "."

def rollStats():
    numberOfStats = raw_input("Choose how many stats you want to roll for: ")
    numberOfStats = int(numberOfStats)
    
    statsList = setStats(numberOfStats)
    
    while statsList:
        chooseStat(statsList)
        
        
rollStats()

Thanks very much guys,both solutions are good.I'm pretty new to programming in general, and I started with python.Good thing I learn fast and that there are nice people to help me out.

*two stat rolls can have same value in d&d,I must have forgotten to write that,but I just edited the code a bit. :)

another little question if you don't mind.I have several python text editors on my linux OS all can run the program and print the output,but what I would like is a program that could show the value change without having to write "print" for each variable.I have something like that for Java.for eg. if I run the program, it shows directly the variable progression like:
HP = 20(at the begining of the fight)
after successful hit was made on the subject it shows the value of the hit and the remaining HP.is there something like that for python on linux platform?or windows if necessary..the reason why I need this is because I'm trying only to make the game system for further use,not a full python game

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.