Random Number Memory Game in C
In this article, will design a simple number memory game in the C programming language. It is a simple memory number game where a random number is displayed and is hidden after some time. The task is to guess that displayed number to continue the game.
How to Play This Game:
- Press 1 on your keyboard to start the game.
- A random positive number is displayed in the console which the player has to remember.
- After few seconds the number displayed will disappear.
- At the next line in the console, the player has to input the number shown previously.
- If the input number is the same as the previous number then the player’s score increases by 1 and the game continue.
- If the input number is incorrect, the game ends and the player’s score is displayed.
Below is the implementation of the above approach:
C
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
int randomnum( long level)
{
clrscr();
printf ( "Level %ld \n" , level);
long num;
num = ( rand () % 100 * level)
+ 1 + level * 5.2f;
printf ( "Number : %ld \n" , num);
delay(2000 - (10 * level));
clrscr();
return num;
}
void main()
{
clrscr();
long num;
long guessnum;
long level = 1;
long inputnum;
printf ( "Press 1 to start Game! " );
scanf ( "%ld" , &inputnum);
if (inputnum == 1) {
do {
num = randomnum(level);
scanf ( "%ld" , &guessnum);
level++;
if (guessnum != num) {
printf ( "You Failed! " );
}
} while (num == guessnum);
}
getch();
}
|
Output:
Last Updated :
28 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...