I made a game called more or less. The computer chooses a random number, and the user has to guess that number with minimum shots possible. I also added a way to prompt the user if want to replay or not(I did it with integers so far) and the possibility to choose the difficulty. but I'm pretty sure I can refactor it and write a better code for the same game.
Thanks for your ideas in advance.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//JeuDuPlusOuDuMoinsWithRetry.C WITH ALMOST WHOLE PROGRAM IN FUNCTION WITH RETRY WORKING AND DIFFICULTY TO CHOOSE WORKING TOO
int continuerPartie;//this variable has to be outside functions to avoid bugs
int WhatIsTheNumberWithRetryInIt()//function with almost all program in it
{
int MAX ;
int MIN ;
int enteredNumber;
int nombreMystere;
int NombreDeCoups = 1;
int difficultyChosen;
// prompting user to choose difficulty
printf("Bienvenue dans le jeu du plus ou moins.\nLe but du jeu est de deviner le nombre mystere \nChoisissez le niveau de difficulte:\n1 = entre 1 et 100;\n2 = entre 1 et 1000 ;\n3 = entre 1 et 10000.\n");
scanf("%d", &difficultyChosen);
if (difficultyChosen == 1)// if else to increase or deacrease MAX value depending on user difficulty chosen
{
MAX = 100;
}
else if (difficultyChosen == 2)
{
MAX = 1000;
}
else if (difficultyChosen == 3)
{
MAX = 10000;
}
printf("quel est le nombre mystere? \n");//ask user for input (integer)
scanf("%d", &enteredNumber);
//generation of random number
srand(time(NULL));//srand() must be used just once in any program
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;//rand() is a function to provide random number
//game main loop. repeats until user finds number
while (enteredNumber != nombreMystere)
{
if (enteredNumber < nombreMystere)//if loop, that shows if it,s more or less to provide
{
printf("c'est plus! \n");
}
else if (enteredNumber > nombreMystere)
{
printf("c'est moins! \n");
}
printf("quel est le nombre mystere? \n"); //reprompts user until find the number
scanf("%d", &enteredNumber);
NombreDeCoups++;//increments number of shots to give the final score
}
printf("Bravo! Vous avez trouve le bon nombre en %d coups \n", NombreDeCoups); //gives final score
}
int main (int argc, char *argv[])
{
do//do to start the game
{
WhatIsTheNumberWithRetryInIt();//call game function
printf("Voulez vous rejouer?(0/1) \n");//ask user if they want to replay
scanf("%d", &continuerPartie);
}
while (continuerPartie == 1);//do while that makes the game continue or not
}
Aucun commentaire:
Enregistrer un commentaire