简体   繁体   English

如何获得脚本和变量更改的重复功能

[英]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.我需要制作一款基于“饮酒”游戏 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.游戏包括滚动 5 个骰子,如果数字等于 6、5、4(按此顺序),则您获胜。 6 is the Ship, 5 is the Captain and 4 is the crew. 6 是船,5 是船长,4 是船员。 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.我有一个问题,当我执行脚本时,滚动永远不会停止,因为我遇到问题告诉“roll = random.randint(1,6)”只运行 5 次然后停止。 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.例如:骰子掷了 5 次后,骰子将 = 5。我需要帮助,以便脚本识别骰子必须掷 5 次,如果是,如果掷出的数字不是 6, 5和4,结束游戏。 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.由于您想运行它一定次数,带有范围的 for 循环会更好。

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:如果您调用DiceRound()直到返回 == 0,则此脚本中的 Output 看起来有点像这样:

[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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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