943,811 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 476
  • Python RSS
Jun 1st, 2009
0

game creation issue

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Horia.Muntean is offline Offline
5 posts
since Jun 2009
Jun 1st, 2009
0

Re: game creation issue

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:

Python Syntax (Toggle Plain Text)
  1. from random import *
  2.  
  3. def setStats(numberOfStats):
  4. stats = []
  5.  
  6. while numberOfStats > 0:
  7. stat = randint(3, 18)
  8. stats.append(stat)
  9.  
  10. numberOfStats -= 1
  11.  
  12. print stats
  13.  
  14. setStats(6)

Enjoy the gaming! This was how I got into python too.
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Jun 2nd, 2009
0

Re: game creation issue

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Horia.Muntean is offline Offline
5 posts
since Jun 2009
Jun 2nd, 2009
0

Re: game creation issue

Quote ...
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.
Python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. ## random can come up with the same number twice, so append
  4. ## integers until 6 unique numbers are in the list
  5. def random_rolls():
  6. rolls_list = []
  7. ctr = 0
  8. while ctr < 6:
  9. x = random.randint(3,18)
  10. if x not in rolls_list:
  11. rolls_list.append(x)
  12. ctr += 1
  13. return rolls_list
  14.  
  15. #this will assign rollOne for strength stat
  16. def chooseStrength(rolls_list):
  17. strengthChosen = raw_input("Choose a roll number between 1 and %d: " \
  18. % (len(rolls_list)))
  19.  
  20. ## arrays are accessed via memory offset, so use
  21. ## zero through 5 instead of 1 --> 6 (one less)
  22. strengthChosen_int = int(strengthChosen) - 1
  23. strength = rolls_list[strengthChosen_int]
  24. print 'You have chosen the %s roll as your strength.Your strength is %d.' \
  25. % (strengthChosen, strength)
  26.  
  27. ## now delete that number so it's not used again
  28. del rolls_list[strengthChosen_int]
  29. return strength, rolls_list
  30.  
  31. ##-------------------------------------------------------------------
  32. rolls_list = random_rolls()
  33. print "You would use these numbers for the rolls:"
  34. for x in rolls_list:
  35. print x
  36.  
  37. print "\nLet's do it again to test for different numbers"
  38. rolls_list = random_rolls()
  39. print "You would use these numbers for the rolls:"
  40. for x in rolls_list:
  41. print x
  42.  
  43. ## copy the list if you want to keep the original
  44. orig_rolls = rolls_list[:]
  45.  
  46. ## now chooseStrength as an example of how to use it
  47. strength, rolls_list = chooseStrength(rolls_list)
  48. print "Strength has been set to", strength
  49. print "\nand rolls_list is now smaller"
  50. for x in rolls_list:
  51. 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.
Last edited by woooee; Jun 2nd, 2009 at 9:31 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Jun 3rd, 2009
0

Re: game creation issue

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.

Python Syntax (Toggle Plain Text)
  1. from random import *
  2.  
  3. def setStats(numberOfStats):
  4. stats = []
  5.  
  6. while numberOfStats > 0:
  7. stat = randint(3, 18)
  8. stats.append(stat)
  9.  
  10. numberOfStats -= 1
  11.  
  12. return stats
  13.  
  14. def chooseStat(statsList):
  15. statName = raw_input("Choose the stat you're rolling for now: ")
  16.  
  17. stat = statsList.pop()
  18.  
  19. print "Your " + statName + " stat is " + str(stat) + "."
  20.  
  21. def rollStats():
  22. numberOfStats = raw_input("Choose how many stats you want to roll for: ")
  23. numberOfStats = int(numberOfStats)
  24.  
  25. statsList = setStats(numberOfStats)
  26.  
  27. while statsList:
  28. chooseStat(statsList)
  29.  
  30.  
  31. rollStats()
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Jun 3rd, 2009
0

Re: game creation issue

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Horia.Muntean is offline Offline
5 posts
since Jun 2009
Jun 3rd, 2009
0

Re: game creation issue

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Horia.Muntean is offline Offline
5 posts
since Jun 2009

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: unique thread
Next Thread in Python Forum Timeline: How can I fetch web pages in Python using sockets (not urllib)?





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


Follow us on Twitter


© 2011 DaniWeb® LLC