简体   繁体   中英

How can I get repeating functions of a script and variable changes

I need to make a game based on the 'drinking' game Ship, Captain, Crew. The game consists of rolling 5 dies, and if the numbers are equal to 6,5,4 (in that order), you win. 6 is the Ship, 5 is the Captain and 4 is the crew. I wrote basic script but I'm still having issues. I have an issue where when I execute the script, the rolls never stop as I'm having issues telling the "roll = random.randint(1,6)" to only run 5 times then stop. Also, 'Dice' Is trying to define the number of rolls. So for example: after the dice has been rolled 5 times, Dice would = 5. I need help making it so that the script recognises that the dice must be rolled 5 times, and when it is, if the numbers rolled were not 6,5 and 4, end the game. Can anyone help?

Code:

import random

Dice = 0
SHIP_CAPTAIN_CREW = 6, 5, 4
SHIP = 6
CAPTAIN = 5
CREW = 4

WINNING_SCORE = 6, 5, 4

while Dice  !=  WINNING_SCORE:
    roll = random.randint(1,6)
   
    
    print ("You Rolled A", roll)

    
        
    if roll == SHIP:
          print("You found the ship!")
     
           
              
          
    if roll == CAPTAIN:
          print("You found the Captain")
               
            
    if roll == CREW:
          print("You found the Crew!")
           
  
        
    if roll == SHIP:
        
        score = +1
        roll
    else:
        Dice    = roll
        
    if roll ==  CAPTAIN :
      
        score = +1
        roll
    else:
        Dice += roll

    
    if Dice == 5:
        break    
print ("Dice:", roll)

Since you want to run it a set number of times, a for loop with a range would be better.

Additionally, the code you have now doesn't check to make sure the numbers are rolled in the correct order.

import random

needed_value = 6; #the next roll you need

for x in range(5) :
    roll = random.randint(1,6)


    print ("You Rolled A", roll)

    
    if roll == 6:
          print("You found the ship!")
          if(needed_value == 6):
                needed_value = 5 #update the next needed number
      
    if roll == 5:
          print("You found the Captain")
          if(needed_value == 5):
               needed_value = 4 #update the next needed number
        
    if roll == 4:
          print("You found the Crew!")
          if(needed_value == 4):
                print ("Win!") #all three were found in order
                break #the game has been won so no need to continue with loop 

If I understood your game and your expectation well, this might be the answer.

import random
def drinking():
    names = ['crew', 'captain', 'ship']
    winning_order = '654'
    score = ''
    for _ in range(5):
        dice = random.randint(1, 6)
        print(f"You Rolled A {dice}")
        if 4 <= dice <= 6:
            print(f'You found the {names[dice - 4]}')
            score += str(dice)
        elif score == winning_order:
            return f'Dice:, {dice}'  # You won the game would be ideal
    return 'You lose the game'

print(drinking())

You Rolled A 6
You found the ship
You Rolled A 6
You found the ship
You Rolled A 5
You found the captain
You Rolled A 1
You Rolled A 3
You lose the game

I made some heavy modifications to your code. I was hoping to keep it easy to understand, while still doing everything you're expecting, and also making it easy to modify.

import random

# I put the logic inside a function so you can call it
# DiceRound() will return 0 on wins, and -1 on losses
def DiceRound():
    SHIP = 6
    CAPTAIN = 5
    CREW = 4
    Counter = 0 # keeps track of how many rolls we've made
    WINNING_SCORE = 0 # modified to keep track of win-condition
    dierolls = "" # used for output

    while WINNING_SCORE  !=  3 and Counter < 5:
        v = 0 # verbate
        roll = random.randint(1,6)
        Counter = Counter +1
        dierolls = dierolls + "[%d] "%roll

        # Any time we roll 6, we're 1-roll towards victory
        if roll == SHIP:
            WINNING_SCORE = 1

        # If we roll a 5 when we've already rolled a 6
        if roll == CAPTAIN and WINNING_SCORE == 1:
            WINNING_SCORE = 2
        # Elsewise if we just rolled a 5
        elif roll == CAPTAIN and WINNING_SCORE != 1:
            WINNING_SCORE = 0

        # If we roll a 4 when we've already rolled a 6 and a 5
        if roll == CREW  and WINNING_SCORE == 2:
            WINNING_SCORE = 3
            print(dierolls)
            print("... Round won!", end="")
            return(0) # return; exits loop and returns 0 for Win-condition
        # Elsewise if we just rolled a 4
        elif roll == CREW and WINNING_SCORE != 2:
            WINNING_SCORE = 0

        # If we rolled below a 4, anytime
        if roll < 4:
            WINNING_SCORE = 0

    # If we rolled all five times without winning
    print(dierolls)
    print("... Round lost.", end="")
    return(-1) # return; exits loop and returns -1 for Lose-condition

Output from this script will look somewhat like this if you call DiceRound() until the return == 0:

[1] [6] [1] [5] [5] ... Round lost.
[3] [6] [4] [5] [3] ... Round lost.
[4] [2] [5] [4] [4] ... Round lost.
[2] [2] [6] [5] [4] ... Round won!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM