Цель этого кода: имитировать 100 игр CRAPS и записать # потери первого раунда, победы в первом раунде, потери второго раунда ПЛЮС, а второй раунд - очки PLUS.C: Craps/Dice Game
Те из вас, кто не знаком с правилами CRAPS; вы в основном бросаете два кубика, если результат - любое, кроме 2, 3 или 12, вы снова можете катиться (число, которое вы свернули, будет сохранено и добавлено в ваши очки). Если вы откатываете 7 или 11, вы автоматически выигрываете.
Это где я нахожусь в данный момент:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times.\n");
for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win.\n");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose.\n");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;
if (sumd2==sumd){
printf("You rolled your points, you win.\n");
winp++;
break;}
if (sumd==7){
printf("You rolled a 7, you lose.\n");
lostp++;
break;}
}
}
}
printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. ", winf, lostf, winp, lostp);
}
Все, что я прошу вас, что вы даете мне варианты того, как я могу сохранить эти пункты должны быть напечатаны в конце ??
Кроме того, я чувствую, что мой код может быть написан лучше и менее избыточно, предложения?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times. Press any key to continue.\n");
//getchar();
for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
switch(sumd){
case 7:
case 11:
printf("You rolled %d, you win.\n", sumd);
winf++;
break;
case 2:
case 3:
case 12:
printf("You rolled %d, you lose.\n", sumd);
lostf++;
break;
default:
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;
if (sumd2==sumd){
printf("You rolled your points(%d), you win.\n",sumd);
winp++;
break;}
if (sumd2==7){
printf("You rolled a 7, you lose.\n");
lostp++;
break;}
}
}
}
printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. \n", winf, lostf, winp, lostp);
}
Совет: Вместо 'Е (" Вы катал 7 или 11, вы выигрываете \ п");' использовать 'Е (" Вы катал% d, вы выигрываете. \ n ", sumd);' – st0le
В 'while (1)' 'if (sumd == 7)' никогда не будет true. Это опечатка? Вы имели в виду 'if (sumd2 == 7)'? –
да, это должно быть 'if (sumd2 == 7)' –