简体   繁体   English

c 中的掷骰子游戏

[英]Craps game in c

First of all, I'm really sorry for my bad English I tried to explain the problem as much as I can首先,我真的很抱歉我的英语不好我试图尽可能多地解释这个问题

Craps game in c c 中的掷骰子游戏

The computer rolls two dice and if the result is 7 or 11 you directly win or the result is 2,3 and 12 you directly lose.电脑掷两个骰子,如果结果是 7 或 11,您直接赢,或者结果是 2,3 和 12,您直接输。

if the sum is 4,5,6,8,9 or 10 it wants you to roll the dice again.如果总和是 4、5、6、8、9 或 10,它希望您再次掷骰子。 and this time you have to find the same sum 4,5,6,8,9 or 10 if you get 7 or 11 before finding the same number you lose.这一次,如果你得到 7 或 11,你必须找到相同的总和 4、5、6、8、9 或 10,然后才能找到你输掉的相同数字。 (This is where the problem is, even if the computer finds the same number twice. it doesn't print out as "you won." https://imgur.com/KL358Fi (这就是问题所在,即使计算机两次找到相同的数字。它不会打印出“你赢了。” https://imgur.com/KL358Fi

Examples: dices are rolling... 11 You won.示例:掷骰子... 11 你赢了。

do you want to play again (y/Y – n/N)?你想再玩一次吗(y/Y – n/N)? y是的

dices are rolling... 3 You Lose骰子正在滚动... 3 你输了

do you want to play again (y/Y – n/N)?你想再玩一次吗(y/Y – n/N)? y是的

dices are rolling... 12 Kaybettiniz骰子正在滚动... 12 Kaybettiniz

Yeni oyun oynansin mi (y/Y – n/N)? Yeni oyun oynansin mi (y/Y – n/N)? y是的

dices are rolling... 7 You Won.骰子正在滚动... 7 你赢了。

do you want to play again (y/Y – n/N)?你想再玩一次吗(y/Y – n/N)? y是的

dices are rolling... 7 you won骰子在滚动... 7 你赢了

do you want to play again(y/Y – n/N)?你想再玩一次吗(y/Y – n/N)? y是的

dices are rolling... 9 result is unclear, dice will be rolled again.骰子正在滚动... 9 结果不清楚,将再次掷骰子。 roll (r/R)?滚动(r/R)?

dices are rolling... 8 result is unclear, dice will be rolled again.骰子正在滚动... 8 结果不清楚,将再次掷骰子。 roll(r/R)?滚动(r/R)?

Dices are rolling... 11 result is unclear, dice will be rolled again.骰子正在滚动... 11结果不清楚,将再次滚动骰子。 roll (r/R)?滚动(r/R)? R R

dices are rolling... 9 You won.骰子正在滚动... 9 你赢了。

Yeni oyun oynansın mı (y/Y –(n/N)? e Yeni oyun oynansın mı (y/Y –(n/N)? e

Zarlar atiliyor... 5 result is unclear, dice will be rolled again. Zarlar atiliyor... 5结果不清楚,将再次掷骰子。 roll (r/R) r滚动 (r/R) r

Zarlar atiliyor... 10 result is unclear, dice will be rolled again. Zarlar atiliyor... 10 结果不明确,将再次掷骰子。 roll (r/R) r滚动 (r/R) r

Zarlar atiliyor... 7 You lose. Zarlar atiliyor... 7 你输了。

do you want to play again (y/Y – n/N)?你想再玩一次吗(y/Y – n/N)? N ñ

CRAPS has ended. CRAPS 已经结束。

#include<stdio.h>
#include <time.h>

int main() 
{
 char ynd;
 int kazan=0; 
 int sonuc=0;
 char yoyun;


 do{



    printf("dice is rolling  ");
    srand(time(NULL));
    kazan = rand()%11+1;
    kazan++;

    if( kazan==7 || kazan==11 )
    {
        printf(" %d you won.\n",kazan);
    }
        else if( kazan==2 || kazan==3 || kazan==12 )
        {
            printf(" %d you lose.\n",kazan);
        }


    sonuc=kazan;

    if( kazan==4 || kazan==5 || kazan==6 || kazan==8 || kazan==9 || kazan==10 )
    {


        do{
            printf("%d result is unclear, dice will be rolled again roll(r/R) ",kazan);
            scanf(" %s",&ynd);
            printf("dice is rolling  ");
            kazan = rand()%11+1;
            kazan++;
            if(kazan == sonuc)
            {
                printf("%d you won",kazan);
                break;
            }
            else if(kazan==7){
                printf("%d You lose",kazan);
                break;
            }

        }  

            while(ynd=='r' || ynd=='R');
    }

printf("want to play a new game ( y/Y-n/N )");
    scanf(" %s",&yoyun);
} while(yoyun=='y' || yoyun=='Y');

printf("craps has ended");


return 0;
}

The code you posted does appear to print out "you won" if it finds the original number before finding 7 .如果在找到7之前找到原始数字,您发布的代码似乎会打印出“您赢了”。 Perhaps you fixed your error in translation?也许您修正了翻译中的错误?

However, it does not result in a loss if it rolls 11 before finding the duplicate.但是,如果在找到副本之前掷出11 ,它不会导致损失。

I also see some potential improvements:我还看到了一些潜在的改进:

  • You should #include <stdlib.h> to use srand and rand (gcc automatically fixes this for you).您应该#include <stdlib.h>使用srandrand (gcc 会自动为您修复此问题)。 See here .这里 In general try to resolve warnings produced by the compiler (search for them online if you don't understand).通常尝试解决编译器产生的警告(如果您不理解,请在线搜索)。
  • I recommend only using srand(time(NULL)) at the beginning of the program because if the user plays the game rapidly they will end up with identical games until the second has elapsed.我建议只在程序开始时使用srand(time(NULL)) ,因为如果用户快速玩游戏,他们最终会得到相同的游戏,直到第二个游戏结束。

  • You could use rand()%6+rand()%6+2 to simulate the distribution of dice rolls (for example, 7 is more likely than any other number).您可以使用rand()%6+rand()%6+2来模拟掷骰子的分布(例如, 7比任何其他数字更有可能)。 Simply using rand()%11+1 results in a uniform distribution.简单地使用rand()%11+1会导致均匀分布。

  • You incremented the dice roll with ++ and 1 directly after you called rand .在调用rand后,您直接用++1增加了掷骰子。 It would be more straightforward to just add 2 after you call rand .在调用rand之后添加2会更直接。

  • No need to check if the dice roll is indeterminate.无需检查掷骰子是否不确定。 Simply use a final else and move sonuc=kazan into the block.只需使用final else并将sonuc=kazan移动到块中。

  • You may want to look into using a jump table rather than if statements because it could be more efficient in cases like this where the input (dice roll) is tightly packed integers.您可能希望考虑使用跳转表而不是if语句,因为在输入(掷骰子)是紧密压缩的整数的情况下,它可能会更有效。

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main() 
{
    char ynd;     
    int kazan=0;
    int sonuc=0;
    char yoyun;
    srand(time(NULL));
    do {
        printf("dice is rolling  ");
        kazan = rand()%6+rand()%6+2;
        if( kazan==7 || kazan==11 )
        {
            printf(" %d you won.\n",kazan);
        }
        else if( kazan==2 || kazan==3 || kazan==12 )
        {
            printf(" %d you lose.\n",kazan);
        }   
        else
        {                       
            sonuc=kazan;
            do {
                printf("%d result is unclear, dice will be rolled again roll(r/R) ",kazan);
                scanf(" %s",&ynd);
                printf("dice is rolling  ");
                kazan = rand()%6+rand()%6+2;
                if(kazan == sonuc)
                {
                    printf("%d you won.\n",kazan);
                    break;
                }
                else if(kazan==7 || kazan==11){    
                    printf("%d You lose.\n",kazan);
                    break;
                }
            } while (ynd=='r' || ynd=='R');
        }
        printf("want to play a new game ( y/Y-n/N )");
        scanf(" %s",&yoyun);            
    } while(yoyun=='y' || yoyun=='Y');  
    printf("craps has ended.\n");
    return 0;
}

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

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