简体   繁体   中英

A Game of Chance,a simulator for a very simple dice game

Can anyone please tell what is wrong with my code?

player will roll two dice. In the first throw if the sum of the two dice is equal to 7 or 11, player wins. If the sum is equal to 2, 3, or 12 player loses. Any other sum, the game will continue, and the sum will become players “points”. The player will roll the two dice again and again to achieve the players “points”. If the player achieves this, the player wins. If the player rolls a 7 before achieving the “points” the player loses.

peace!

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

int rand_num()
{
    return ( 1 + (rand()%6) );
}

int main( void )
{
    int D1, D2, score, point;
    int D3, D4, score_n = 0;

    srand ( time( NULL ) );

    D1 = rand_num();
    D2 = rand_num();

    score = D1 + D2;
    printf( "You rolled %d + %d = %d", D1, D2, score );

    if ( score == 7 || score == 11 )
        printf( "\n\nYou win!");

    else if ( score == 2 || score == 3 || score == 12 )
        printf( "\n\nYou lose!");

    else
        {
            point = score;
            printf( "\n\nYou must get %d to win", point);

            do
            {
                D3 = rand_num();
                D4 = rand_num();

                score_n = D3 + D4;

                printf( "\n\nYou rolled %d + %d = %d", D3, D4, score_n );   

                if ( score_n == 7 ) 
                    printf( "\n\nYou lose!" );


                if ( score_n == point )
                    printf( "\n\nYou win!" );

            }while ( score_n == point || score_n == 7 );            


        }



    return 0;
}
 }while ( score_n == point || score_n == 7 ); 

仅当分数==点或7时,循环才会继续

 }while ( score_n != point && score_n != 7 ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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