samedi 20 juin 2015

Dice Game Numbers not Random

Basically I am supposed to simulate a program that rolls three dice, add it up. Then I will have the user guess if the next roll is higher, lower, same or if they just want to quit. I am having two problems.

  1. The numbers are not random, obviously this is a big mistake on my part but I can't seem to figure it out. I am thinking I do not need the second inclusion of 3 dice pairs? They don't help at all anyways.

  2. Regardless, my program goes through all of the if/else if statements all at once. Obviously I do not want this to happen.


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

int main ()
{
    int diceOne, diceTwo, diceThree, diceSum=0, timesCorrect=0, choice; 
    int newDiceOne, newDiceTwo, newDiceThree, newDiceSum;

    srand( time(NULL) );

    diceOne      = rand() % 6 + 1;
    diceTwo      = rand() % 6 + 1;
    diceThree    = rand() % 6 + 1;
    newDiceOne   = rand() % 6 + 1;
    newDiceTwo   = rand() % 6 + 1;
    newDiceThree = rand() % 6 + 1;

    printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
    diceSum = diceOne + diceTwo + diceThree;
    printf("\nTotal sum of dice is %d\n", diceSum);

    do {
        printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
        printf(" You have been correct %d times\n", timesCorrect);
        scanf("%d", &choice);

        printf("The three dice rolls: %d, %d, %d", newDiceOne, newDiceTwo, newDiceThree);
        newDiceSum= newDiceOne + newDiceTwo + newDiceThree;
        printf("\nTotal sum of dice is %d\n", newDiceSum);

        if (choice == 1)
        {
            if (newDiceSum > diceSum);
                timesCorrect++;
            printf("You are correct!\n");
        }
        else if (newDiceSum < diceSum); 
        {
            printf("You are incorrect, sorry!\n");
        }

        if (choice == 2)
        {
            if (newDiceSum < diceSum);
                timesCorrect++;
            printf("You are correct!\n");
        }
        else if (newDiceSum > diceSum); 
        {
            printf("You are incorrect, sorry!\n");
        }

        if (choice == 3)
        {
            if (newDiceSum == diceSum);
                timesCorrect ++;
            printf("You are correct!\n");
        }
        else if (newDiceSum != diceSum); 
        {
            printf("You are incorrect, sorry!\n");
        }

        if (choice == 4)
        {
            printf("Thanks for playing!!!!!!\n");
            system("pause");

            return 0;
        }
    } while (choice!= 4 );
}




Aucun commentaire:

Enregistrer un commentaire