Saturday 14 February 2015

C Programming: Hot/Cold Guessing Game

3 weeks or so ago at work, I was introduced to the programming language, C. We were given a brief overview ('Hello World', scanf for an integer, printf, compare functions and if statements), and then something came up and interrupted our training (this has been a bit of a theme), so we were left to ourselves.

The final task on the training we'd been given was to get the user to input a number, to compare that number to one set in the programme and then congratulate the user if they guessed the right number, tell them if their guess was too big or commiserate them.

That seemed a bit boring, and once the number had been worked out by the player there was no point playing again. So, with a bit of help from the internet, I found a way of creating a random number and then played with loops and breaks to give the player multiple guesses and an escape clause in case they got bored, and the absolute function to provide hot/cold clues.

#include <stdio.h>
#include <stdlib.h> /*For the random function, srand */
#include <time.h> /*By using time to seed the random function, the output changes each time*/

int main() {
                int guess, answer, difference, x;
               
                srand(time(NULL));
                answer = rand() % 10 + 1; /*produces a random number between 1 & 10*/
               
                printf("What number am I thinking of?\n");
               
                for (x = 1; x <= 3; x++) {
                                scanf("%d", &guess);
                                if(guess == answer) {
                                                break;
                                }
                                else {
                                                difference = abs(guess-answer);
                                                if(difference == 2 || difference == 3) {
                                                                printf("getting warm\nTry again\n");
                                                }
                                                else if(difference == 1) {
                                                                printf("Scorching hot! \nTry again\n");
                                                }
                                                else {
                                                                printf("Freezing! \nTry again\n");
                                                }
                                }
                }
                if(guess != answer){
                                printf("(Fed up? You can type '0' to lose)\n");
                                scanf("%d", &guess);
                }
                while(guess != answer) {
                                difference = abs(guess - answer);
                                if(guess == 0) {
                                                printf("You lose\n");
                                                break;
                                }
                                else if(difference == 3) {
                                                printf("getting warm\nTry again\n");
                                }
                                else if(difference == 2) {
                                                printf("getting warmer\nTry again\n");
                                }
                                else if(difference == 1) {
                                                printf("Scorching hot!\nTry again\n");
                                }
                                else if(guess < answer) {
                                                printf("Freezing! My number's bigger.\nTry again\n");
                                }
                                else if(guess > answer) {
                                                printf("Freezing! My number's smaller.\nTry again\n");
                                }
                                scanf("%d", &guess);
                }
                if (guess == answer) {
                                printf("You win!\n");
                }
return 0;
}

Our trainer was suitably impressed so included to build a treasure hunt game as part of our next module, on arrays. He was going to give us battleships but decided that was too complicated, so when our training was next interrupted, I worked on that. So I have a few more bits of code to show you in the future, and also have a few issues with initiliasing the arrays that I can't work out why it doesn't work. But I'll show you that another time.

2 comments:

  1. Looks good, I really need to pick up programming again!

    ReplyDelete
    Replies
    1. You really should!

      I really like C - I think I'll like Java more, but it's a while before I'll be using Java. I forgot to email home any of my other code, so not going to update on C tonight.

      Delete