-3

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

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.

if the sum is 4,5,6,8,9 or 10 it wants you to roll the dice again. 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. (This is where the problem is, even if the computer finds the same number twice. it doesn't print out as "you won." https://i.stack.imgur.com/KFoct.jpg

Examples: dices are rolling... 11 You won.

do you want to play again (y/Y – n/N)? y

dices are rolling... 3 You Lose

do you want to play again (y/Y – n/N)? y

dices are rolling... 12 Kaybettiniz

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

dices are rolling... 7 You Won.

do you want to play again (y/Y – n/N)? y

dices are rolling... 7 you won

do you want to play again(y/Y – n/N)? y

dices are rolling... 9 result is unclear, dice will be rolled again. roll (r/R)?

dices are rolling... 8 result is unclear, dice will be rolled again. roll(r/R)?

Dices are rolling... 11 result is unclear, dice will be rolled again. roll (r/R)? R

dices are rolling... 9 You won.

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

Zarlar atiliyor... 5 result is unclear, dice will be rolled again. roll (r/R) r

Zarlar atiliyor... 10 result is unclear, dice will be rolled again. roll (r/R) r

Zarlar atiliyor... 7 You lose.

do you want to play again (y/Y – n/N)? N

CRAPS has ended.

#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;
}
cl-creator
  • 25
  • 6
  • 2
    What is your question? You posted the code, is something wrong with it? – HolyBlackCat Apr 19 '20 at 23:06
  • Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 19 '20 at 23:09
  • if the dice is 4,5,6,8,9 or 10 I want computer to find the same number and print out as "you won." – cl-creator Apr 19 '20 at 23:15
  • You've not explained any problem you're having with the (far too much) code you posted, and you've not asked any sort of question. Dumping a whole lot of code and expecting us to both figure out what you're asking and then answer it is not appropriate. Please spend some time reading [ask] and [mcve] and then come back to [edit] your post and ask a more complete question so we can try to help. – Ken White Apr 19 '20 at 23:22

1 Answers1

1

The code you posted does appear to print out "you won" if it finds the original number before finding 7. Perhaps you fixed your error in translation?

However, it does not result in a loss if it rolls 11 before finding the duplicate.

I also see some potential improvements:

  • You should #include <stdlib.h> to use srand and rand (gcc automatically fixes this for you). 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.

  • 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). Simply using rand()%11+1 results in a uniform distribution.

  • You incremented the dice roll with ++ and 1 directly after you called rand. It would be more straightforward to just add 2 after you call rand.

  • No need to check if the dice roll is indeterminate. Simply use a final else and move sonuc=kazan into the block.

  • 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.

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