my code

A simple simon game written completely in C. There's a little problem with the delayNErase function. check in to it later.

Snippet options

Download: Download snippet as simple-simon-game.c.
Copy snippet: For this you need a free my code account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

 * Simple Simon game

#include<stdlib.h> //to use rand function to generate randoms :rand(), srand(), fflush()
#include<time.h> //used for seed and timings functions  :time(), clock()
#include<ctype.h> //used to process user input :toupper()
#include<stdbool.h> //for using 'bool' type for vars instead of _Bool

/* number of correct anwers after which to increase sequence length */
#define COUNT_MAX 3
/* number of seconds delay the erase the sequence after its display */
#define DELAY 2

void gameInstructions(void);
void startGamePlay(void);
void printSequence(unsigned int seed, int sequenceLength);
bool inputSeqCheck(unsigned int seed, int sequenceLength);
void delayNErase(int sequenceLength);
void printScore(int counter,time_t gameStart);

void main(int argc, char * argv[]){
	gameInstructions(); // print game instructions
}//end of main

void gameInstructions(void){
	printf(" print instructions \n\n\n");
	 * show auto concatenate feature of printf 
}//end of gameInstructions

void startGamePlay(void){
	char anotherGame = 'Y'; // player wants to play another game ?
	bool gameWon = true; // has the player lost the game ?
	int sequenceLength = 2; //used to limit the range of random number
	int counter = 0; //number of correct answers
		time_t gameStart = clock(); //total time taken to play the game
			unsigned int seed = time(NULL);
			/* increase the sequence length if counter has reached COUNT_MAX:
			 * check the way the increment is done with out using if construct 
			 *also note that counter is incremented in postfix */
			sequenceLength += ((counter++ % COUNT_MAX) == 0);
			/*print sequence to screen*/
			printSequence(seed, sequenceLength);
			/*Erase digits on the screen*/
			if (counter==1)
				printf("\nNow enter the sequence including the spaces \n");
			/*take user input and check if correct return true else false*/
			gameWon=inputSeqCheck(seed, sequenceLength);
			printf("%s\n",gameWon?"Correct !":"You lost the Game");
		}// end of while loop
		/* print the score */
		printScore(counter, gameStart);
		printf("\nWanna play again (y/n)? ");	
}//end of startGamePlay

void printSequence(unsigned int seed, int sequenceLength){
	/* Print the digits to the screen:
	 * We use this method instead of limiting the rand range because when
	 * you limit a rand range its output can be any number from 0-limit but
	 * we need the output to be of particular sequence length always + we 
	 * need seperate digits to make a sequence not a single number */
	int i=0;
	for  (i=0; i<= sequenceLength; i++){
		printf("%d ",rand()%10);
	/*clear buffers so that you actually print characters before u erase them*/
}//end of printSequence

bool inputSeqCheck(unsigned int seed, int sequenceLength){
	/*1. Take input sequence from the user:
	 * get the previous random sequence using same seed
	 * verify the u input against the random sequence and if
	 * right continue else exit the loop setting gameLost*/
	int i=0;
	for (i=0; i<=sequenceLength; i++){
		int number=0;
		if (number != rand()%10){
			return false;
	return true;
}//end of inputSeqCheck

void delayNErase(int sequenceLength){
	/* 1. Provides a delay of DELAY seconds and erases the 
	 * line which gives the effect of hiding the input.
	 * 2. Uses CLOCKS_PER_SEC and clock() to check the number
	 * of seconds elapsed. */
	/*time_t is a type used by time.h functions such as clock and time*/
	time_t now = clock();
	for( ;clock() - now < DELAY*CLOCKS_PER_SEC; );
	/*Erase the current line*/
	int i=0;
	for(i=0; i <sequenceLength; i++){
		printf("    ");
}//end of delayNErase

void printScore(int counter,time_t gameStart){
	int timeTaken = (clock() - gameStart)/CLOCKS_PER_SEC;
	printf("Your score: %d \n", (--counter*100)/ timeTaken);
}//end of printScore

Create a free my code account now.

my code is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.