-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (78 loc) · 2.26 KB
/
Copy pathmain.cpp
File metadata and controls
100 lines (78 loc) · 2.26 KB
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
//GuessMyNumber - The classic number guessing game
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
int totalTries=0;
int countIterations=0;
//The player tries to guess a random number generated by the computer
void humanFinder() {
//Seed the random number generator
srand(static_cast<unsigned int>(time(0)));
//get a random number between 1-100
int secretNumber = rand() % 100 + 1;
int tries = 0;
int guess;
cout << "\tWelcome to Guess My Number\n\n";
do {
cout << "Enter a guess (1-100): ";
cin >> guess;
++tries;
if(guess > secretNumber) {
cout << "Too high!\n\n";
} else if(guess < secretNumber) {
cout << "Too low!\n\n";
} else {
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
} while(guess != secretNumber);
}
//The computer tries to guess a number given by the player
void computerFinder() {
//Seed the random number generator
//srand(static_cast<unsigned int>(time(0)));
//get a random number between 1-100
int guess = rand() % 100 + 1;
int secretNumber;
int tries = 0;
//bool flag=true;
cout << "Enter a number (1-10000): ";
cin >> secretNumber;
do {
++tries;
++totalTries;
guess = rand() % 10000 + 1;
cout << guess << " - ";
if(guess > secretNumber) {
cout << "Too high!\n";
} else if(guess < secretNumber) {
cout << "Too low!\n";
} else {
cout << "\nThat's it! computer found your number in " << tries << " guesses!\n";
//Sleep(1000);
//tries = 0;
//countIterations++;
}
} while(guess != secretNumber); //while(countIterations < 20);
}
int main() {
int selection;
cout << "\tWelcome to Guess My Number\n\n";
cout << "Choose game mode \n--Press 1 if you want to guess the number \n--Press 2 for computer guessing your number\n";
cin >> selection;
switch (selection) {
case 1:
humanFinder();
break;
case 2:
computerFinder();
cout << "Total tries : " << totalTries << " - Total Iterations : " << countIterations << "\n\n";
cout << "Average tries before finding the number : " << totalTries/countIterations << "\n\n";
break;
default:
break;
}
cout << "\t\t!!GAME OVER!!\n\n";
return 0;
}