The Wayback Machine - https://web.archive.org/web/20240906050946/https://www.geeksforgeeks.org/random-number-memory-game-in-c/
Open In App

Random Number Memory Game in C

Last Updated : 28 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Easy
44.85%
1.1K

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




// C program to implements the above
// memory game
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
  
// Function to generate the random
// number at each new level
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 the number
    return num;
}
  
// Driver Code
void main()
{
    clrscr();
    long num;
    long guessnum;
  
    long level = 1;
  
    long inputnum;
  
    // Start the game
    printf("Press 1 to start Game! ");
    scanf("%ld", &inputnum;);
  
    // Game Starts
    if (inputnum == 1) {
  
        // Iterate until game ends
        do {
  
            // Generate a random number
            num = randomnum(level);
  
            // Get the guessed number
            scanf("%ld", &guessnum;);
            level++;
  
            // Condition for the Game
            // Over State
            if (guessnum != num) {
                printf("You Failed! ");
            }
        } while (num == guessnum);
    }
  
    getch();
}


Output:



Similar Reads

Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
Game Theory (Normal – form game) | Set 1 (Introduction) Please go through the above article before proceeding. Given a payoff matrix. The task is to find the optimum strategies of the players. Solution: Player A is having 3 strategies - 1, 2 and 3, and player B is also having 3 strategies - 1, 2 and 3. Step 1: Find row minimum values for each row a
2 min read
Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
Consider the following payoff matrix with respect to player A and solve it optimally. Solution: If a game has no saddle point then the game is said to have mixed strategy. Step 1: Find out the row minimum and column maximum. Step 2: Find out the minimax and maximin values. Since minimax and maximin value of this game are not equal, this game has no
4 min read
Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
The payoff matrix of an M * 2 game consists of M rows and two columns. This article will discuss how to solve an M * 2 game by graphical method. Also, this article will discuss if more than two lines intersect the same point in the graph then how can a 2 * 2 payoff matrix be formed. Consider the below problem: Solution: First check whether the prob
3 min read
Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
The payoff matrix of a 2 * N game consists of 2 rows and N columns . This article will discuss how to solve a 2 * N game by graphical method. Consider the below 2 * 5 game: Solution: First check the saddle point of the game. This game has no saddle point. Step 1: Reduce the size of the payoff matrix by applying dominance property, if it exists. Thi
3 min read
Why Linked List is implemented on Heap memory rather than Stack memory?
Pre-requisite: Linked List Data StructureStack vsHeap Memory Allocation The Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. It is implemented on the heap memory rather than the stack memory. This article discusses the reason behind
14 min read
Implement random-0-6-Generator using the given random-0-1-Generator
Given a function random01Generator() that gives you randomly either 0 or 1, implement a function that utilizes this function and generate numbers between 0 and 6(both inclusive). All numbers should have same probabilities of occurrence. Examples: on multiple runs, it gives 3 2 3 6 0 Approach : The idea here is to find the range of the smallest numb
5 min read
Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
Set 1 (Random Numbers, Arrays and Matrices) Generating Random Characters C/C++ Code // A C++ Program to generate test cases for // random characters #include&lt;bits/stdc++.h&gt; using namespace std; // Define the number of runs for the test data // generated #define RUN 5 // Define the range of the test data generated // Here it is 'a' to 'z' #def
11 min read
Memory Game in Java
The Memory Game is a game where the player has to flip over pairs of cards with the same symbol. We create two arrays to represent the game board: board and flipped. The board is an array of strings that represents the state of the game board at any given time.When a player flips a card, we replace the corresponding element of the board array with
3 min read
Memory Game from scratch using React
In this article, we will create a Memory game using ReactJS. A memory game is a famous game in which a pair of cards are placed in front of a player with their face down. The two pairs are always going to have the same content. The player has the choice to select any two cards at a time and check if the faces of the card match or not. If the face c
6 min read
Create a Memory Pair Game using React-Native
In this article, we will build the interactive Memory Pair Game using the React Native language. We are displaying the 12 boxes to the user, in the user has to click on each box, and when the user clicks on the box the random icon will be shown. Users have to guess or find its match by clicking on the other box. If the user correctly finds that, th
5 min read
Game Theory (Normal - form game) | Set 1 (Introduction)
Game theory is a mathematical model used for decision making. It has applications in all fields of social science, as well as in logic and computer science. Game theory has come to play an increasingly important role in logic and in computer science. To be fully defined, a game must specify the following elements: the players of the game, the infor
4 min read
Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
This article discusses how to solve a game by the dominance property with mixed strategy. Consider the below game: Solution: Find out the row minimum and column maximum values. Here Minimax value is not equal to Maximin so this game has no saddle point. Now proceed with dominance property to reduce the rows and the columns. Reducing the row and the
2 min read
Combinatorial Game Theory | Set 2 (Game of Nim)
We strongly recommend to refer below article as a prerequisite of this. Combinatorial Game Theory | Set 1 (Introduction) In this post, Game of Nim is discussed. The Game of Nim is described by the following rules- “ Given a number of piles in which each pile contains some numbers of stones/coins. In each turn, a player can choose only one pile and
15+ min read
Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
In some of the games, it is possible to reduce the size of the payoff matrix by eliminating rows (or columns) that are dominated by other rows (or columns) respectively. Dominance property for rows: X ≤ Y i.e. if all the elements of a particular row X are less than or equal to the corresponding elements of another row Y then delete row X (row X is
4 min read
Winning Game by replacing numbers with factors (Brain Game)
Given 2 players A and B take turns alternatively to play a game in which they have N numbers on a paper. In one turn, a player can replace one of the numbers by any of its factors (except for 1 &amp; the number itself). The player who is unable to make a move loses the game. Find the winner of the game if A starts the game and both play optimally.
15+ min read
Game of Chocolates | Wythoff's Game
Bunty and Dolly are playing a game, described as follows:There are two boxes having A and B number of chocolates respectively. Both can eat L (L &gt;= 1) chocolates from any one box or L chocolates from both boxes in one move. They play the game alternatively and the last one to eat the chocolate will be the winner. You have to help Bunty in decidi
13 min read
Which sorting algorithm makes minimum number of memory writes?
Minimizing the number of writes is useful when making writes to some huge data set is very expensive, such as with EEPROMs or Flash memory, where each write reduces the lifespan of the memory. Among the sorting algorithms that we generally study in our data structure and algorithm courses,  Selection Sort makes least number of writes (it makes O(n)
1 min read
Select a random number from stream, with O(1) space
Given a stream of numbers, generate a random number from the stream. You are allowed to use only O(1) space and the input is in the form of a stream, so can't store the previously seen numbers. So how do we generate a random number from the whole stream such that the probability of picking any number is 1/n. with O(1) extra space? This problem is a
11 min read
Probability of getting a perfect square when a random number is chosen in a given range
Given two integers L and R that denote a range, the task is to find the probability of getting a perfect square number when a random number is chosen in the range L to R. Examples: Input: L = 6, R = 20 Output: 0.133333 Explanation: Perfect squares in range [6, 20] = {9, 16} =&gt; 2 perfect squares Total numbers in range [6, 20] = 15 Probability = 2
4 min read
Random number generator in arbitrary probability distribution fashion
Given n numbers, each with some frequency of occurrence. Return a random number with probability proportional to its frequency of occurrence. Example: Let following be the given numbers. arr[] = {10, 30, 20, 40} Let following be the frequencies of given numbers. freq[] = {1, 6, 2, 1} The output should be 10 with probability 1/10 30 with probability
11 min read
Project Idea | ( True Random Number Generator)
Introduction Random numbers in computer science is used for cryptography, simulation, sampling, design and games. In the past the need for more and more randomness has increased. Developers seek for more and more randomness. This project is based on generating random numbers using simple programming on a local system. Features The source of randomn
2 min read
Pseudo Random Number Generator (PRNG)
Pseudo Random Number Generator(PRNG) refers to an algorithm that uses mathematical formulas to produce sequences of random numbers. PRNGs generate a sequence of numbers approximating the properties of random numbers. A PRNG starts from an arbitrary starting state using a seed state. Many numbers are generated in a short time and can also be reprodu
4 min read
Number of wins for each player in a series of Rock-Paper-Scissor game
Two players are playing a series of games of Rock–paper–scissors. There are a total of K games played. Player 1 has a sequence of moves denoted by string A and similarly player 2 has string B. If any player reaches the end of their string, they move back to the start of the string. The task is to count the number of games won by each of the player
11 min read
Number of ways for playing first move optimally in a NIM game
Two players A and B are playing NIM Game with each other. Both are playing optimally. Player A starts the game. The task is to find the number of ways of playing 1st move for A to ensure a winning strategy for A if possible, otherwise, print -1. Examples: Input: arr[] = {1, 2, 3} Output: -1 There is no winning strategy for A no matter how optimally
8 min read
Find the winner of the game based on greater number of divisors
Given two arrays arr1[] and arr2[], player A picks an element from arr1[] and player B picks an element from arr2[], the player which has the element with more number of divisors wins the round. If both have elements with the same number of divisors then player A wins that round. The task is to find whether player A has more probability of winning
10 min read
Two player game in which a player can remove all occurrences of a number
Two players, player1 and player2, are playing a game on a given number sequence S where player1 starts first and both of them play optimally. The task is to find whether player1 wins or loses. If he wins, print "Yes", otherwise print "No". The rules of the game are as follows: The player's alternate turns.In each turn, the current player must choos
7 min read
Guessing the Number Game using Android Studio
A Simple Guess the number application in which application generates the random number between 1 to 100 and the user has to guess that Number. It is a simple and basic application that uses basic widgets and libraries. Approach: Step1: Creating a new project Click on File option at topmost corner in left. Then click on new and open a new project an
3 min read
Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
Given an array arr[] consisting of N integers, each representing size of a pile of stones. The task is to determine the winner of the game when two players, A and B, play a game optimally as per the following conditions: Player A always starts the game.In one move, a player may remove any number of stones (at least 1), from the first non-empty pile
9 min read
Number of coins to be removed for the first player to win the Game
Two friends, A, and B are playing a game. In this game, a group of N coins is placed. The players can pick any number of coins between 1 and 4 (both inclusive) in their chance. The player who takes the last coin wins the game. If A starts first, find out how many coins should he pick such that he is guaranteed to win the game or determine if it's i
5 min read
Count number of ways to reach a given score in a game
Consider a game where players can score 3, 5, or 10 points in a move. Given a total score of N, The task is to find the number of ways to reach the given score. Examples: Input: n = 20Output: 4Explanation: There are following 4 ways to reach 20: (10, 10), (5, 5, 10), (5, 5, 5, 5), (3, 3, 3, 3, 3, 5) Input: n = 13Output: 2Explanation: There are foll
14 min read
Practice Tags :