0

I'm trying to make a craps game where there would be dice rolling to check if it hits the target. I can't be sure what's wrong with my while loop but it keeps running even when the while loop requirement is false. Here is my code:

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

int rollDice() {
    int sum, d1, d2;
    sum = 0;
    d1 = (rand() % 6) + 1;
    d2 = (rand() % 6) + 1; 
    sum = d1 + d2;
    printf("Player rolled %d + %d = %d\n", d1, d2, sum);
    return sum;
}

int main() {
    int player, target, tries;
    srand(time(NULL));
    tries = 0;
    player = rollDice();
    target = player;
    if (player == 7 || player == 11) {
        printf("Player wins.\n");
    }
    else if (player == 2 || player == 3 || player == 12) {
        printf("Player loses.\n");
    } else {
        printf("Point is %d. The game continues: \n", target);
        player = rollDice();
    }
    printf("Player is %d, Target is %d.\n", player, target);
    while (target != player || player != 7) {
        player = rollDice();
    }
    return 0;
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
James Bu
  • 19
  • 4
  • 2
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Apr 23 '22 at 21:52
  • 1
    James, best to roll back you code to as it was before answers arrived. – chux - Reinstate Monica Apr 24 '22 at 05:35
  • Huh, I edited and was about to make a clever comment about how the proper term is "craps game", and a crap game would be one that just isn't very good. But apparently the singular *crap* can also be correctly used for the dice game, see [noun (2)](https://www.merriam-webster.com/dictionary/crap). – Nate Eldredge Apr 24 '22 at 06:34
  • @NateEldredge: I ran the same investigation... A game that isn't very good is a *crappy* game :) – chqrlie Apr 24 '22 at 17:45

1 Answers1

3

The game ends when a 7 is thrown or when the player's score is thrown again. Hence the while test should be while (target != player && player != 7), not while (target != player || player != 7).

You must also move the while loop inside the last else block.

Here is a modified version:

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

int rollDice(void) {
    int sum, d1, d2;
    sum = 0;
    d1 = rand() % 6 + 1;
    d2 = rand() % 6 + 1; 
    sum = d1 + d2;
    printf("Player rolled %d + %d = %d\n", d1, d2, sum);
    return sum;
}

int main() {
    int player, target, tries;
    srand(time(NULL));
    tries = 0;
    player = rollDice();
    target = player;
    if (player == 7 || player == 11) {
        printf("Player wins.\n");
    } else
    if (player == 2 || player == 3 || player == 12) {
        printf("Player loses.\n");
    } else {
        printf("Point is %d. The game continues:\n", target);
        player = rollDice();

        printf("Player is %d, Target is %d.\n", player, target);
        for (;;) {
            if (player == 7) {
                printf("Pass loses\n");
                break;
            }
            if (player == target) {
                printf("Pass wins\n");
                break;
            }
            player = rollDice();
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I had made some changes to the code but there's something else which seems to trouble me. For the while loop, why does it keep printing "Player wins" even though the results haven't been meet yet. The code is updated on the top. – James Bu Apr 23 '22 at 22:41
  • @JamesBu: modifying the code in the question makes the comments and answers inconsistent. You can append extra text to the question with **EDIT**: paragraphs. Did my post fully address your question? – chqrlie Apr 24 '22 at 17:49