Completed Unnecessary hard game *Updated Again*

So here is the thing I sent this code to CSNorwood who helped me make the code shorter. So This game has now been re-written, re-worded, and re-structured with a lot and I mean a lot of help. I have asked and have been given permission to release this game again as I did not work on this alone and an agreement was made that he really does not care lol so the update is here and the download and been updated also. A massive thank you to CSNorwood for all his help.

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

struct game_level {
    char intro[64];
    char outro[64];
    long int range_max; // for PSYCHO level 10 range
};

struct game_level game_levels[10] = {
    // Level 1
    {
        "Welcome to the easiest level, yeah, LEVEL 1!!!\n",
        "Well done, but it was way too easy anyway\n",
        10
    },
    // Level 2
    {
        "Welcome to the NEXT easiest level, okay, LEVEL 2!!!\n",
        "Well done, but it was easy anyway\n",
        100
    },
    // Level 3
    {
        "Okay, now we're testing you, LEVEL 3!!!\n",
        "Well done, is all I can say\n",
        1000
    },
    // Level 4
    {
        "Welcome to a moderately easy but hard, LEVEL 4!!!\n",
        "Well done, bit harder, so alright\n",
        10000
    },
    // Level 5
    {
        "Welcome to playing a stupid level, LEVEL 5!!!\n",
        "Well done, you actually lasted\n",
        100000
    },
    // Level 6
    {
        "Welcome to 'should be a piece of cake.', LEVEL 6!!!\n",
        "Well done, but it was easy anyway\n",
        1000000
    },
    // Level 7
    {
        "Welcome to Psycho easy, LEVEL 7!!!\n",
        "Well done, but it was easy anyway\n",
        10000000
    },
    // Level 8
    {
        "Welcome to Psycho medium, LEVEL 8!!!\n",
        "Well done, but it was easy anyway\n",
        100000000
    },
    // Level 9
    {
        "Welcome to Psycho hard, LEVEL 9!!!\n",
        "Well done, but it was easy anyway\n",
        1000000000
    },
    // Level 10
    {
        "Welcome to just PSYCHO, LEVEL 10!!!\n",
        "Well done, but it was easy anyway\n",
        10000000000
    }
};

void show_intro() {
    puts("Welcome to the 'Guess The Number game'");
    puts("Brought to you by Stephen Wilde (with a bit of help from CSNorwood)\n");
}

int get_level_select() {
    int level;

    for (int pos = 0; pos < 10; pos++) {
        printf("Level %d is between 1 and %ld\n",
                pos + 1,
                game_levels[pos].range_max);
    }

    do {
        printf("\nEnter a level to play 1 to 10 (0 to quit) : ");
        scanf("%d", &level);
    } while (level < 0 || level > 10);

    return level;
}

int play_level(int level) {
    long int number = rand() % (game_levels[level].range_max) + 1;

    printf("\n%s", game_levels[level].intro);

    int completed = 0;
    int try_count = 0;
    long int try_value;

    char buffer[64];
    char *temp_ptr; // used in strtol function

    do {
        printf("Enter value between 1 and %ld (try %d) (Q - Quit): ",
                game_levels[level].range_max,
                try_count + 1);

        scanf("%s", &buffer);

        if (buffer[0] == 'q' || buffer[0] == 'Q') { // option to quit
            puts("So sorry you have given up already...");
            return -1;
        } else if (strcmp("CHEAT", buffer) == 0) { // cheat
            puts("CHEATER HA HA");
            printf("The number is %ld\n", number);
            printf("Adding %d tries\n", (level + 1) * 10);
            try_count += (level + 1) * 10;
        } else {
            try_count++;

            try_value = strtol(buffer, &temp_ptr, 10);

            if (try_value == number) {
                printf("%ld is CORRECT\n", try_value);
                printf("It took you %d tries\n", try_count);
                puts(game_levels[level].outro);
                completed = 1;
            } else if (try_value < number) {
                puts("Too LOW");
            } else if (try_value > number) {
                puts("Too HIGH");
            }
        }
    } while (!completed);

    return 0;
}

int main(int argc, char** argv) {
    srand(time(NULL));
    int result;

    do {
        show_intro();

        int level = get_level_select();

        if (level > 0) result = play_level(level - 1);
        else result = -1;

    } while (result == 0);

    return (EXIT_SUCCESS);
}

You may also like...

1 Response

  1. April 28, 2020

    […] is that many posts over the last few days about the updates and the progress of Unnecessary hard game but…. I feel now that the program is 100% complete and the source code and even the downloads […]

Leave a Reply

Your email address will not be published. Required fields are marked *