1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
/*
This program is designed to play guess the number with a single user, using a random number generator.
Providing feed back to the user as they input guesses between 1-20, directing the user to guess higher or lower.
Each round the user will be given 6 chances to guess the number, if the user inputs the correct number with in 6
tries he/she wins. The program will store and output the # of games played, # of wins, and # of losses when the user
quits the game.
1. Create a function genNumber() that generates a random number, using a different seed each time it generates a number.
2. Create a main function that:
a. Asks players name and greets the player
b. Calls function genNumber()
c. asks user to guess a number and allows user to input number.
d. determines whether user guessed correctly or not.
e. if user guessed correctly user wins
f. if user guessed incorrectly program tells user higher or lower and asks user to guess again.
g. continues through 6th guess.
h. if user does not guess within 6 guesses the user loses.
i. create loop that asks user if user would like to play again. //FIXME //fixed
j. create function getStats() to store stats of games played, wins and losses //fix me research
k. if user quits call getStats() and outputs them to the user. //FIXME still dont know how to return
wins losses games played
3. create test function
a. create assertions to ensure guess number program works. //FIXME
*/
#include <iostream>
#include <ctime> //for time, to create a new seed each time a random number is generated to increase illusion of randomness
#include <cstdlib> // for srand and rand
#include <string>
using namespace std;
void genNumber() {
srand (time(NULL)); //initializes sequenced random number generator thats seed changes based on time.
}
int main()
{
//int again; // trying char/
string name;
int guess, num, turn;
genNumber();
turn = 0;
char again;
num = (rand() % 20) + 1; //generates a random number between 1-20
//cout << num << endl; checking to see if calling function and rng works.
cout << "Hi, what is your name?" << endl;
cin >> name;
cout << "Hi " << name << ", Let's play a game!!" << endl;
while (true) { //fix me loop not working. //fixed
while (true) {
cout << "Enter a number between 1 and 20 (" << 5 - turn << " tries left): ";
cin >> guess;
cin.ignore();
// Check is tries are taken up.
if (turn >= 5) {
break;
}
// Check number.
if (guess > num) {
cout << "you guessed to high!" << endl;
}
else if (guess < num) {
cout << "you guessed to low" << endl;
}
else {
break;
}
// If not number, increment tries.
turn++;
}
// Check for tries.
if (turn >= 5) {
cout << "Sorry, I win this time!" << endl;
}
else {
// Or, user won.
cout << "You're a great guesser, you win! " << endl;
}
while (true) { // play again loop
cout << "Want to keep playing? (Y/N)? ";
cin >> again;
cin.ignore();
if (again == 'n' || again == 'N' || again == 'y' || again == 'Y') {
system("cls"); // trying to figure out how to reset my turns.
break;
}
else {
//cout << "Please enter \'Y\' or \'N\'...\n";
}
}
// Check user's input and run again or exit;
if (again == 'n' || again == 'N') {
cout << "Thank you for playing!";
break;
}
else {
}
}
cout << "Any Key to exit" << endl;
//} while loop not worrking
cin.ignore(1000, '\n');
cin.get();
return num;
}
|