You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Craps游戏投注模块输入处理异常及功能完善技术咨询

Craps骰子游戏钱包与输入处理改进方案

问题描述

开发带虚拟钱包的Craps骰子游戏时,投注环节存在输入校验逻辑混乱、钱包余额未正确更新、缺少游戏核心流程(骰子投掷、输赢结算)及结束条件判断的问题,需实现以下功能:

  • 校验用户投注输入是否为数字,非法输入需重新输入
  • 验证投注金额是否在钱包余额范围内,超出则要求重新输入
  • 合法投注后提供"roll dice"选项启动游戏
  • 获胜时获得双倍投注金额,失败扣除投注金额,实时更新钱包余额
  • 当余额≥1000或<0时,游戏自动结束

现有问题代码

import random     
import sys
money = "500"
 
# start the game
a = input("Hello travler, to start the game, please type 'yes'. If you are not ready to begin your journey, type 'no' ")
if a.lower() == "no":
    sys.exit()
else:
    print("Welcome")
    print("Hold on... I'm missing something,")
name = input("What is your name, traveler?: ")
print("Welcome", (name)) 

# those who need instructions can ask for it,
# others can start the game directly.
a = input("Welcome to a game that'll determine the rest of your life. Do you need instructions? (yes) or (no)? \n")
 
if a.lower() == "yes":
    print('''1. the player will roll two six sided dice. the sum of the two dice is the player's number for the round.
2. rolling a 7 or an 11 as your first roll win you the game, but be weary, a 2, 3, or 12 automatically causes the player to lose. no more game for you. If a 4, 5, 6, 8, 9, or 10 are rolled on this first roll, that number becomes the 'point.'
3. the fated player continues to roll the two dice again until one of two things occur: either they roll the 'point' again, causing them to win the game; or they roll a 7, causing them to be doomed to the eternals.''')
 
elif a.lower() == "no":
    print("may luck be with you on this fateful day,", name)

print("You will start off with 500 pieces of luck, if you leave this game with over 1000 pieces of luck, you will be fated for sucsess. On the other hand, should you drop below 0 peices of luck, you will find yourself in debt to the universe, a misfortune that few recover from.")
print("You currently have", money, "luck to spare")

# betting time
while True:
  bet = input("How much luck do you wish to bet? ")
  if bet.isdigit() is True:
    if money > bet:
      print("Bet accepted")
  if bet.isdigit() is True:
      if bet > money:
        print("How unfortunate, you do not have the luck to make this bet.")
  elif bet.isdigit() is False:
    print ("Sorry, luck can only be quantitated in number values.")
  # if you bet higher than your money, it wont allow you to bet within after that. 

改进后的完整代码

import random     
import sys

# 初始化钱包为整数类型,避免字符串比较错误
money = 500
 
# 游戏启动确认
start_input = input("Hello traveler, to start the game, please type 'yes'. If you are not ready to begin your journey, type 'no' ")
if start_input.lower() == "no":
    sys.exit()
else:
    print("Welcome")
    print("Hold on... I'm missing something,")
name = input("What is your name, traveler?: ")
print(f"Welcome, {name}") 

# 游戏说明询问
instructions_input = input("Welcome to a game that'll determine the rest of your life. Do you need instructions? (yes) or (no)? \n")
 
if instructions_input.lower() == "yes":
    print('''1. The player will roll two six-sided dice. The sum of the two dice is the player's number for the round.
2. Rolling a 7 or an 11 as your first roll wins you the game, but be wary, a 2, 3, or 12 automatically causes the player to lose. If a 4, 5, 6, 8, 9, or 10 are rolled on this first roll, that number becomes the 'point.'
3. The fated player continues to roll the two dice again until one of two things occur: either they roll the 'point' again, causing them to win the game; or they roll a 7, causing them to be doomed to the eternals.''')
 
elif instructions_input.lower() == "no":
    print(f"May luck be with you on this fateful day, {name}")

print("You will start off with 500 pieces of luck, if you leave this game with over 1000 pieces of luck, you will be fated for success. On the other hand, should you drop below 0 pieces of luck, you will find yourself in debt to the universe, a misfortune that few recover from.")

# 主游戏循环,直到余额触发结束条件
while 0 <= money < 1000:
    print(f"\nYou currently have {money} luck to spare")
    
    # 投注输入校验循环,直到输入合法
    while True:
        bet_input = input("How much luck do you wish to bet? ")
        # 校验是否为正整数
        if not bet_input.isdigit():
            print("Sorry, luck can only be quantitated in number values.")
            continue
        bet = int(bet_input)
        # 校验投注金额是否在余额范围内
        if bet <= 0:
            print("You must bet at least 1 piece of luck.")
        elif bet > money:
            print("How unfortunate, you do not have the luck to make this bet.")
        else:
            print("Bet accepted")
            break
    
    # 等待用户触发骰子投掷
    while True:
        roll_input = input("Type 'roll dice' to start the round: ").strip().lower()
        if roll_input == "roll dice":
            break
        else:
            print("Please type 'roll dice' to proceed.")
    
    # 骰子投掷逻辑
    def roll_dice():
        die1 = random.randint(1,6)
        die2 = random.randint(1,6)
        total = die1 + die2
        print(f"You rolled {die1} and {die2}, total: {total}")
        return total
    
    # 第一轮投掷
    first_roll = roll_dice()
    win = False
    lose = False
    
    # 判断第一轮结果
    if first_roll in [7, 11]:
        print("You win the round!")
        win = True
    elif first_roll in [2, 3, 12]:
        print("You lose the round!")
        lose = True
    else:
        point = first_roll
        print(f"Your point is {point}. Keep rolling until you hit {point} again or a 7.")
        # 后续投掷循环,直到赢或输
        while True:
            current_roll = roll_dice()
            if current_roll == point:
                print("You hit your point! You win the round!")
                win = True
                break
            elif current_roll == 7:
                print("You rolled a 7! You lose the round!")
                lose = True
                break
    
    # 结算投注金额
    if win:
        money += bet
        print(f"You gained {bet} luck! Current balance: {money}")
    elif lose:
        money -= bet
        print(f"You lost {bet} luck! Current balance: {money}")

# 游戏结束判断
if money >= 1000:
    print(f"\nCongratulations {name}! You have {money} luck - you're fated for success!")
else:
    print(f"\nOh no {name}! You have {money} luck - you're in debt to the universe. Game over.")

关键改进说明

  • 钱包类型修正:将money从字符串改为整数,解决数值比较错误问题
  • 投注输入校验:嵌套循环确保用户输入正整数且不超过余额,非法输入时持续提示重新输入
  • 骰子投掷触发:新增"roll dice"输入校验,确保用户明确触发游戏环节
  • 核心游戏逻辑实现:完成Craps规则的骰子投掷、输赢判断(第一轮直接胜负/点数循环)
  • 余额结算与结束条件:实时更新钱包余额,当余额≥1000或<0时自动终止游戏并显示对应结局

内容的提问来源于stack exchange,提问作者babyeepers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 23:40:27