2016-09-24 3 views
-4

정확한 숫자를 굴릴 때까지 또는 계속 잃어 버릴 때까지 플레이어가 계속 주사위를 굴립니다.조건이 발생할 때까지 루핑

루프를 계속 진행하려면 게임이나 주사위가 필요합니다. 이 문제를 해결하기 위해 수정해야 할 사항은 무엇입니까?

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! 

가 어떻게이받을 수 있나요 :

Player rolled: 4 + 5 = 9 

The point is 9 

Player rolled: 5 + 3 = 9 

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

이 내 출력 할 필요가 무엇 : 이것은 내 출력

#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; 
} 
} 
} 

: 여기

내 프로그램입니다 루프에서 주사위를 굴리는가?

+1

나는 결코 도박을 한 적이 없습니다. 음, 적어도 나는 지금은 쓰레기 규칙을 알고있다. –

+0

입력 해 주셔서 감사합니다. –

답변

-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; 

} 
+0

이것이 완료 되었습니까? 나는 달릴 수 없었고, 나는 그것에 대한 수익을 올리려고했다. –

+0

게시하기 전에 테스트를 거쳤습니다. 오류가 있습니까? –

+0

"srand (time (0));"에 오류가 발생했습니다. scope –

관련 문제