-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.cpp
More file actions
77 lines (71 loc) · 1.68 KB
/
NumberGame.cpp
File metadata and controls
77 lines (71 loc) · 1.68 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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int argc, char ** argv)
{
srand(time(0)); //synchronize the time to zero
const bool token = (rand() % 2);
// if no arguments, the human tries to guess
bool human_guess = token;
unsigned int m; //human entry
int i = 1; //no of chance
if(human_guess)
{
cout << "Human Play against Computer" << endl;
const unsigned int n = (rand() % 100);
//cout << "Random number: " << n << '\n';
while(true)
{
// Ask human to enter number
cout << "Please enter a number (1-100): "; cin >> m;
//check the guess with the number
if(m == n)
{
cout << "Great!!!, You have found it in " << i << " chance " << endl;
break;
}
else if(m < n){
cout << "you have entered a small number" << endl;
i++;
}
else{
cout << "you have entered a large number" << endl;
i++;
}
}
}
else{
cout << "Computer Play against Human" << endl;
unsigned int lower = 1, upper = 100;
unsigned int guess, response;
while(true)
{
guess = 0.5*(lower + upper);
cout << " 0 if correct, 1 if small, 2 if large " << endl;
cout << " Is it " << guess << " ? ";
cin >> response;
if(response == 0)
{
cout << "Yes I Got you in " << i << " chance!!! "<< endl;
break;
}
else if(lower >= upper)
{
cout << "you are cheating" << endl;
break;
}
else if(response == 1)
{
lower = guess + 1;
i++;
}
else
{
upper = guess - 1;
i++;
}
}
}
}