samedi 27 juin 2020

Why does this code result in an infinite loop? Also, how can I completely restart my number baseball game?(How can I re-generate random numbers?)

I'm new to C, and is trying to make a number baseball game. The functions I wish to include are, when I press x, the game ends, and when I press m, a series of instructions are printed. Finally, I've added a re-game function, so that when I press the number 1 after the game ends, one could play it again. The number baseball game itself works fine, but whenever I press x or m, the program goes into an infinite loop. I can't figure out what's wrong. Also, when I use the re-game function, my program works, but it doesn't generate a new set of numbers, and just uses the set it used before. Could you please help??

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

int main (void) {
    
    int computerBall[4];
    int X = 0;
    int i, j = 1;
    int userBall[4];
    int x, m;
    int o, q;
    int flag = 1;
    q = 1;
    x = 2;
    m = 3;
    srand (time (NULL));

    computerBall[0] = rand () % 31;

    while (computerBall[0] == 0)
        computerBall[0] = rand () % 31;

    for (i = 1; i < 4; i++) {
        computerBall[i] = rand () % 31;

        while (q == 1) {
            flag = 0;
            for (j = 0; j < i; j++) {
                if (computerBall[j] == computerBall[i]) {
                    computerBall[i] = rand () % 31;
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
                break;
        }
    }                           // Code to Generate Random Numbers
    while (q == 1) {
        printf ("Choose 4 numbers between 1 and 30\n");
        printf ("Press x to exit, and press m for help\n");
        while (q == 1) {
            printf ("Input numbers:");
            scanf_s ("%d %d %d %d\n", &userBall[0], &userBall[1],
                     &userBall[2], &userBall[3]);
            //To prevent numbers outside of range
            if (userBall[0] < 1 || userBall[0] > 30 || userBall[1] < 1 || 
                userBall[1] > 30 || userBall[2] < 1 || userBall[2] > 30 || 
                userBall[3] < 1 || userBall[3] > 30) {
                printf ("You cannot input numbers outside the range.\n");
                continue;
            } //To prevent repeating numbers
            else if (userBall[0] == userBall[1] || userBall[0] == userBall[2] ||
                     userBall[0] == userBall[3] || userBall[1] == userBall[2] ||
                     userBall[1] == userBall[3] || userBall[2] == userBall[3]) {
                printf ("You cannot input same numbers at once.\n");
                continue;
            }
            else if (userBall[0] == m) {        //Doesn't work
                printf ("Help\n\n");
                continue;
            }
            else if (userBall[0] == x) {        //Doesn't work
                printf ("\n Game Over\n");
                printf ("Answer : ");
                printf ("%d ", X);
                printf ("Press 1 to restart");
                scanf_s ("%d", &o);
                if (o == 1)
                    continue;
                else
                    q = 0;
            }
            break;
        }
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire