Writing a Guessing Game

Writing a Guessing Game

Linux Distribution
C Compiler
a shell interface
My Setup:
Debian GNU/Linux

So i wanted to write a couple tutorials on simple command line games in
languages like C and C++. In this tutorial, i will write a simple guessing game
with a couple of different levels of difficulties. To start off I will setup my
project folder as such:

clim@debian:~/Desktop/Website/C Programming$ ls
clim@debian:~/Desktop/Website/C Programming$ mkdir programs
clim@debian:~/Desktop/Website/C Programming$ cd programs/
clim@debian:~/Desktop/Website/C Programming/programs$ mkdir guess
clim@debian:~/Desktop/Website/C Programming/programs$ cd guess
clim@debian:~/Desktop/Website/C Programming/programs/guess$ pluma main.c

The three headers i will be using are : stdio.h, stdlib.h, and time.h

So my first question is how many difficulties should i make it? I suppose i
could opt-out for something like 3 , an easy, medium , and hard difficulty, but
i wanna make it a little more complex then that. I will go 5. What i decide to
do is create an enum for all the maximum values of difficulties:

enum difficulty{ very_easy = 10,
                 easy = 100,
                 medium = 1000,
                 hard = 10000,
                 very_hard = 100000

Next i decided to abstract out the whole picking of the generated number based
on difficulty. This probably could be inlined for faster results:

int outPutViaDifficulty(enum difficulty d){
        return rand() % d;

Also i want to generate the amount of tries the user gets based on the pick:

  int triesViaDifficulty(enum difficulty d){
            case very_easy:
            return 3;
            case easy:
            return 10;
            case medium:
            return 15;
            case hard:
            return 20;
            case very_hard:
            return 25;
          return 1;

Next im gonna make some console tools for myself, to make it a little more

void clearScreen(){

This sequence of escape codes says: go to the first row, first column,
(“[1;1H”) and erase the screen (“[2J”). So now its time to write the main
function. This is going to include : an enum for the local difficulty , the
random number that will generate ,and the amount of tries the user gets.So first
I define them:

int main(int argc, char * argv[]){

int choice; //choice of difficulty
enum difficulty lDifficulty; //difficulty picked
int gameNumber = 0; //random picked number
int tries; //amount of tries the player gets

Now onto my first interface. Picking your difficulty:

  printf("Welcome to the Guessing Game! Pick Your Difficulty:\n");
  printf("1.Very Easy\n2.Easy\n3.Medium\n4.Hard\n5.Very Hard\n");
  if(choice <1 || choice > 5){
      printf("Error, user choice is not an option");
      return -1;


After I cleared the screen, i figured i would display the games info at the
top of the console:

switch (choice) {
  case 1:
  printf("User Picked: Very Easy\n");
  lDifficulty = very_easy;
  case 2:
  printf("User Picked: Easy\n");
  lDifficulty = easy;
  case 3:
  printf("User Picked: Medium\n");
  lDifficulty = medium;
  case 4:
  printf("User Picked: Very Hard\n");
  lDifficulty = hard;
  case 5:
  printf("User Picked: Very Hard\n");
  lDifficulty = very_hard;

  tries = triesViaDifficulty(lDifficulty);
  gameNumber = outPutViaDifficulty(lDifficulty);

  printf("Tries: %d\nNumber Between 1-%d\n",tries,lDifficulty);

First it picks the correct difficulty, and puts it in lDifficulty. After , I
use lDifficulty, to generate the amount of tries the user gets, as well as the
games number. So after i display that at the top, I begin the loop of tries:

while(tries >0){
  printf("Tries left: %d\nPick A Number 1-%d\n",tries,lDifficulty);
  if(choice < gameNumber){
    printf("Nope! That is less than the number!\n");
  }else if(choice > gameNumber){
    printf("Nope! That is greater than the number!\n");
  }else if(choice == gameNumber){
      printf("Correct! You Win!\n");
      tries = -1;
  if(tries == 0)
    printf("Sorry, You are out of tries.\n");

return 0;

And now im off to compile! Try it out, change the amount of tries, the max
number you can pick based on different difficulties!


Leave a Reply