HOW TO CREATE TEXT GAMES USING C! : PART 2 (Getting started!)

Create text games using C:part 2


So, in the previous post I've described a simple program to generate a random number within a range ( 1 to 6 for a dice) . Now, any type or genre of a game requires actions of the player. So in this part, I bring in another basic element of a game into the code that is actions of the player

So how do we make a player act ?

The answer is to simply alert him with messages ( as there's no way to reach out to his throat and demand him ) to act using the console output functions such as printf() and take the actions he performed using the console input functions such as scanf() .

So, now using the printf() and scanf() I've constructed a simple guess game!

 //let's create a number guessing game when a dice is rolled  
   
  #include <stdio.h>  
  #include <stdlib.h>  
  #include <time.h>  
   
  int main(void)  
  {  
   int number,guess;  
   srand(time(NULL));  
   
   //randomly generate number  
   number = (rand()%6)+1;  
   
   // ALERT the user to act :)  
   printf("the dice has been rolled! guess the number shown on the top face : ");  
   //get the user's guess  
   scanf("%d",&guess);  
   
   //compare the randomly generated number and user's guess  
   if(guess == number)  
        printf("you win :) \n"); //if both match print you win :)  
   else  
        printf("you lose :( \n"); //else print you lose :(  
   
   return 0;  
  }  


  • First I declared two integers number and guess. number stores the randomly generated  number and guess stores the user input.
  • Then using simple if-else statement and a condition we could easily notify the user whether he guessed right or wrong
Before you move here are some interesting activities you can easily do with the help of  what has been explained till now :)

  1. Write a code to flip a coin and check whether it's a heads/tails 
    • (hint: use heads as 0 and tails as 1)
  2. Guess a number between 33 and 77 
    • (hint: change the expression involving rand())
 Hope, you get the above questions done :)

Now moving on, the above code would only make the player guess once if he wants to play again, he needs to restart the whole program. Now, to overcome this loops can be used! A do while loop is the best fit because it definitely implements once and continues if required condition is met.

 //let's create a number guessing game when a dice is rolled  
   
 #include <stdio.h>  
 #include <stdlib.h>  
 #include <time.h>  
   
 int main(void)  
 {  
      int number,guess;  
      char choice = 'y';  
      srand(time(NULL));  
   
      do  
      {  
           //randomly generate number  
           number = (rand()%6)+1;  
   
           // ALERT the user to act :)  
           printf("the dice has been rolled! guess the number shown on the top face : ");  
           //get the user's guess  
           scanf("%d",&guess);  
   
           //compare the randomly generated number and user's guess  
           if(guess == number)  
                printf("the rolled number is %d! you win :) \n",number); //if both match print you win :)  
           else  
                printf("the rolled number id %d! you lose :( \n",number); //else print you lose :(  
   
           printf("want to play again? y/n : ");  
           scanf(" %c",&choice); //note the space before format specifier %c  
   
      }while( choice=='y' || choice=='Y' );  
   
 }  


Hope you understood it! In the next part I'd be explaining how to construct a menu for a game. Click the link below to go to next part:
PART 3: constructing a menu

Comments

Popular posts from this blog

Beginner's guide to Solving the N-Queens problem using backtracking method

Developing Minesweeper using Java

PvP Chain reaction game using Python