How to write the 7 Boom game. If a number is divided by 7 or includes the digit 7, the user writes Boom in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h> // tolower
#include <stdlib.h> // strtol

// 7 Boom game. The user enters a number he wants to start the game, 
// (-1 to end the game, or lost), then the user enters numbers, from the number he entered
// If the current number is divisible by 7, or one of its digits is 7, 
// And the user did not write the word 'Boom', the user loses. 

int contains_seven(int num) {
    char buffer[16] = "";
    sprintf(buffer, "%d", num);
    
    return strchr(buffer, '7') != NULL;
}

int is_boom(int num) {
    return (num % 7 == 0) || contains_seven(num);
}

void get_user_input(char *buffer, int size) {
    printf("Enter a number (or Boom): ");
    fgets(buffer, size, stdin);

    // Remove newline
    buffer[strcspn(buffer, "\n")] = '\0';
}

int process_turn(int expected, const char *input) {
    int should_boom = is_boom(expected);

    // Convert input to lowercase
    char lower[32] = "";
    int i;
    for (i = 0; input[i] && i < 31; i++)
        lower[i] = tolower((unsigned char)input[i]);
    lower[i] = '\0';

    if (should_boom) {
        if (strcmp(lower, "boom") != 0) {
            printf("%s (should be Boom)\n", input);
            printf("YOU LOSE!\n");
            return 0;
        }
    } else {
        // If user typed "boom" incorrectly
        if (strcmp(lower, "boom") == 0) {
            printf("Wrong move! It was %d\n", expected);
            printf("YOU LOSE!\n");
            return 0;
        }

        // Must be a number
        char *endptr;
        int value = strtol(input, &endptr, 10);

        if (*endptr != '\0' || value != expected) {
            printf("Wrong move! It was %d\n", expected);
            printf("YOU LOSE!\n");
            return 0;
        }
    }

    return 1;
}

void run_game() {
    char input[32] = "";

    printf("Enter a number from which you want to start the game (-1 to exit): ");
    fgets(input, sizeof(input), stdin);
    input[strcspn(input, "\n")] = '\0';

    int current = atoi(input);
    if (current == -1)
        return;

    while (1) {
        get_user_input(input, sizeof(input));

        if (strcmp(input, "-1") == 0) {
            printf("Game Over!\n");
            break;
        }

        if (!process_turn(current, input))
            break;

        current++;
    }
}

int main() {
    run_game();
    
    return 0;
}



/*
run:

Enter a number from which you want to start the game (-1 to exit): 6
Enter a number (or Boom): 6
Enter a number (or Boom): Boom
Enter a number (or Boom): 8
Enter a number (or Boom): 9
Enter a number (or Boom): 10
Enter a number (or Boom): 11
Enter a number (or Boom): 12
Enter a number (or Boom): 13
Enter a number (or Boom): Boom
Enter a number (or Boom): 15
Enter a number (or Boom): 16
Enter a number (or Boom): Boom
Enter a number (or Boom): 18
Enter a number (or Boom): 19
Enter a number (or Boom): 20
Enter a number (or Boom): 21
21 (should be Boom)
YOU LOSE!

*/

 



answered Mar 15 by avibootz
edited Mar 16 by avibootz
...