【问题标题】:Looping Until A Condition Takes Place循环直到条件发生
【发布时间】:2023-03-24 00:24:01
【问题描述】:

我如何继续让玩家掷骰子,直到他们掷出正确的数字或​​输掉?

我需要游戏或骰子来继续循环。需要修改什么来实现这一点?

这是我的程序:

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
int dice_num1, dice_num2 = 0; 
int roll_dice;
int dice_num3, dice_num4 = 0;
int roll_dice2;
char repeat = 'y'; 

while (repeat == 'y' || repeat == 'Y') 
{   
    srand(time(0));                                                
    dice_num1 = rand() % 6 + 1;
    dice_num2 = rand() % 6 + 1; 
    roll_dice = dice_num1 + dice_num2;

    cout <<"Player rolled: "<< dice_num1<<" + "<<dice_num2<<" = "<<roll_dice<<endl;
    cout <<"\nThe point is "<<roll_dice<<endl;

    dice_num3 = rand() % 6 + 1;
    dice_num4 = rand() % 6 + 1; 
    roll_dice2 = dice_num3 + dice_num4;

    cout <<"\nPlayer rolled: "<< dice_num3<<" + "<<dice_num4<<" = "<<roll_dice2<<endl;



    if (roll_dice2 == 7 || roll_dice2 == 11)
    {
        cout << "Congrats you are a Winner!" << endl ;
    } 

    else if (roll_dice2 == 2 || roll_dice2 == 3 || roll_dice2 == 12)
    {
        cout  << "Sorry, you rolled craps, You lose!" << endl; 
    }

    else if (roll_dice2 == 4 || roll_dice2 == 5 ||roll_dice2 == 6 ||roll_dice2 == 8 || roll_dice2 == 9 || roll_dice2 == 10)
      {     
        dice_num3 = rand() % 6 + 1;
        dice_num4 = rand() % 6 + 1; 
    int sum_num2 = dice_num3 + dice_num4;

     if( sum_num2 == roll_dice2 )
            {
                cout << "Congrats you are a Winner!" << endl;
                break;
            } 
    else if( sum_num2 == 7 )
            {
                cout << "Sorry, You Lose!" << endl;
                break;
            }
  }
cout <<"\nAnother game? Y(es) or N(o)"  << endl;
    cin >> repeat;

if (repeat == 'n' || repeat == 'N') 
{
cout <<"Thank you for playing!"<< endl;
return 0;
}
}
}

这是我的输出:

Player rolled: 4 + 5 = 9

The point is 9

Player rolled: 5 + 3 = 9

Another game? Y(es) or N(o)

这就是我需要的输出:

Player rolled: 2 + 2 = 4

The point is 4

Player rolled: 4 + 5 = 9
Player rolled: 5 + 1 = 6
Player rolled: 1 + 2 = 3
Player rolled: 2 + 3 = 5
Player rolled: 1 + 2 = 3
Player rolled: 3 + 5 = 8
Player rolled: 2 + 4 = 6
Player rolled: 6 + 3 = 9
Player rolled: 6 + 2 = 8
Player rolled: 3 + 4 = 7

You rolled 7 and lost!

我怎样才能让它继续在循环中掷骰子?

【问题讨论】:

  • 我从来没有玩过赌博。好吧,至少我现在知道掷骰子的规则了。

标签: c++ loops dice


【解决方案1】:

我试图使代码与您的原始结构相似。这应该可以满足您的需求。

#include <iostream>
#include <time.h>
#include <cstdlib>

using namespace std;

int main() {

    srand(clock());

    int die1, die2, total, point;

    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1;
    total = die1 + die2;

    cout << "Player rolled: " << die1 << " + " << die2 << " = " << total << endl;

    if (total == 2 || total == 3 || total == 12) {
        cout << "You rolled " << total << " and lost!" << endl;
        return 0;
    }
    else if (total == 7 || total == 11) {
        cout << "You rolled " << total << " and won!" << endl;
        return 0;
    }

    cout << "The point is " << total << endl;

    point = total;

    while (true) {
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1;
        total = die1 + die2;
        cout << "Player rolled: " << die1 << " + " << die2 << " = " << total << endl;

        if (total == point) {
            cout << "You rolled " << total << " and won!" << endl;
            break;
        }
        else if (total == 7) {
            cout << "You rolled " << total << " and lost!" << endl;
            break;
        }
    }

    return 0;

}

【讨论】:

  • 完成了吗?我无法让它运行,我试着把它放在上面。
  • 我在发布之前对其进行了测试。你有什么错误吗?
  • 那是在你的原始代码中......但我会用不同的方法更新我的帖子。
  • srandsystem 都在 cstdlib 中定义。您需要在开头添加#include &lt;cstdlib&gt;(如在OP 的示例代码中),然后此错误就会消失。但是,我仍然收到sh: pause: command not found 的错误。
  • 哇,我根本没有遇到这些问题。再次更新代码以删除系统命令并仅使用 return。
猜你喜欢
  • 2013-12-09
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多