2 Replies - 1876 Views - Last Post: 24 October 2011 - 01:36 PM Rate Topic: -----

#1 emery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-October 11

dice game, removing dice each loop and looping.

Posted 23 October 2011 - 09:21 AM

in this program, i have a single trial set up to roll 6 dice, add the scores except for the 2's and 5's and then remove the dice than landed on 2's and 5's and repeat until there are no dice left, and the final score has been tabulated. I have 2 'main' variables score and remove. score adds the score from the roll as an int, and holds that value, do i need to set these as global variables and then call for the global versions in each def function? as well for die1-die5? i tired that and didn't seem to get anywhere.



import random



def rolldice():
    
    
    
    
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    die3 = random.randint(1,6)
    die4 = random.randint(1,6)
    die5 = random.randint(1,6)
    

    
def singletrial():
    
    rolldice()
    score = score + int(die1 + die2 + die3 + die4 + die5)

    
    if die1 == 2:
        score = score-2
        remove = remove+1
    elif die2 == 2:
        score = score-2
        remove = remove+1
    elif die3 == 2:
        score = score-2
        remove = remove+1
    elif die4 == 2:
        score = score-2
        remove = remove+1
    elif die5 == 2:
        score = score-2
        remove = remove+1
    elif die1 == 5:
        score = score-5
        remove = remove+1
    elif die2 == 5:
        score = score-5
        remove = remove+1
    elif die3 == 5:
        score = score-5
        remove = remove+1
    elif die4 == 5:
        score = score-5
        remove = remove+1
    elif die5 == 5:
        score = score-5
        remove = remove+1
    else:
        pass
    
    if remove == 0:
        singletrial()
    if remove == 1:
        remove1()
    if remove == 2:
        remove2()
    if remove == 3:
        remove3()
    if remove == 4:
        remove4()
    if remove == 5:
        remove5()

def remove1():
   
    rolldice()
    
    score = score + int(die1 + die2 + die3 + die4)

    
    if die1 == 2:
        score = score-2
        remove = remove+1
    elif die2 == 2:
        score = score-2
        remove = remove+1
    elif die3 == 2:
        score = score-2
        remove = remove+1
    elif die4 == 2:
        score = score-2
        remove = remove+1
    elif die1 == 5:
        score = score-5
        remove = remove+1
    elif die2 == 5:
        score = score-5
        remove = remove+1
    elif die3 == 5:
        score = score-5
        remove = remove+1
    elif die4 == 5:
        score = score-5
        remove = remove+1
    else:
        pass
    
    
    if remove == 0:
        remove1()
    if remove == 1:
        remove2()
    if remove == 2:
        remove3()
    if remove == 3:
        remove4()
    if remove == 4:
        remove5()
    
    return score, remove




def remove2():
    
    rolldice()
    
    score = score + int(die1 + die2 + die3)

    
    if die1 == 2:
        score = score-2
        remove = remove+1
    elif die2 == 2:
        score = score-2
        remove = remove+1
    elif die3 == 2:
        score = score-2
        remove = remove+1
    elif die1 == 5:
        score = score-5
        remove = remove+1
    elif die2 == 5:
        score = score-5
        remove = remove+1
    elif die3 == 5:
        score = score-5
        remove = remove+1
    else:
        pass
    
    if remove == 0:
        remove2()
    if remove == 1:
        remove3()
    if remove == 2:
        remove4()
    if remove == 3:
        remove5()
   
        
    return score, remove
    
def remove3():
    
    rolldice()
    
    score = score + int(die1 + die2)

    
    if die1 == 2:
        score = score-2
        remove = remove+1
    elif die2 == 2:
        score = score-2
        remove = remove+1
    elif die1 == 5:
        score = score-5
        remove = remove+1
    elif die2 == 5:
        score = score-5
        remove = remove+1
    else:
        pass
    
    if remove == 0:
        remove3()
    if remove == 1:
        remove4()
    if remove == 2:
        remove5()
  
    
    return score, remove
    
def remove4():
    
    rolldice()
    
    score = score + int(die1)

    
    if die1 == 2:
        score = score-2
        remove = remove+1
    elif die1 == 5:
        score = score-5
        remove = remove+1
    else:
        pass
    
    if remove == 0:
        remove4()
    if remove == 1:
        remove5()
    
    
    return score, remove
    
    
def remove5():
    
   
    print score
    
    
    
    
def main():
   
   
    numtrials = int(raw_input("Number of trials: "))
    
    singletrial()
    
   
        




main()



Is This A Good Question/Topic? 0
  • +

Replies To: dice game, removing dice each loop and looping.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: dice game, removing dice each loop and looping.

Posted 23 October 2011 - 09:59 AM

Ok between this and your other program I see you are having trouble understanding the concept of scope. So let me explain this as simple as I can...

You see where you define die1, die2... die5 in rolldice()? These are local variables. Meaning they only exist inside that function. When the function ends, those variables go what we call "out of scope". Meaning they can not be accessed by another function (like singletrial()).

There are three main ways to get the values out of a function so they can be used by others.

1) You define them up top as global. When you do this, if you wish to modify the values from within a function (like rolldice()) you must precede the variable with the keyword "global". This says "I want to use the global version previously defined, don't create a local version for me."

2) You return the value from the function using the return keyword. If you want to return a single value (or a single structure/class that contains multiple values like returning a list) you can return it using the return keyword and then where you call rolldice() (in singletrial) you would store the result... returnedValue = rolldice().

3) You can call another method, pass it a mutable object (an object that can be modified like a list, but not like a tuple or string) and the function can alter it. When the function ends, the list is then modified.

So now you have to decide which method works best for your design. Because obviously die1 and the others aren't going to be seen by the other functions. The easiest way (but often considered the most dangerous way) is by making die1 through die5 global and then in each function that wants to alter these variables must define them using the "global" keyword.

If you are not sure what global does, you can do a search on the net for "Python global keyword". One site that simply explains it is http://zetcode.com/t...rial/functions/
just scroll down to the part that is titled "global and local variables".

Hope this clears things up a bit. :)

This post has been edited by Martyr2: 23 October 2011 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: dice game, removing dice each loop and looping.

Posted 24 October 2011 - 01:36 PM

You may also want to consider just hold the number of dice to a value rather than 6 dice. Here comes some psuedo code ^.^ Please note this probably wont run in python as my syntax is a little rusty and hell this algorithm may not work. I am just giving my insight.

D=6
score=0
while(D>0):
  roll = random.randint(1,6)
  if(roll == 2 or roll == 5):
    D -= 1
  else:
    score+=roll
    print "OMG YOU JUST GAINED ", roll," points making your total ", score
    pause(1000)
print "your score is ",score  


Then you can go beyond and make it so that the amount of dice starting from user input. Or better yet change it up to choose what values cause removal, how many sided dice you want, make specials, cause factors like wind speed that will favor number over others.....AI, ECT!!! PROGRAMMING IS INFINITE MWAHAHAHA *Cough* *Cough* but please be sure to understand the basics. Algorithm design is key. While it may work one way, you may save yourself hundreds of lines of code a different way. ^.^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1