-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeGame.cpp
More file actions
92 lines (66 loc) · 2.34 KB
/
codeGame.cpp
File metadata and controls
92 lines (66 loc) · 2.34 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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void PrintIntro(int level){
std::cout << "\n***** You have entered into restricted area level " << level << " *****\n";
std::cout << "***** So you need to enter correct code to escape ***** \n \n" ;
}
bool PlayGame(int difficulty) {
// Setting the difficulty level for this game
srand((unsigned) time(0));
int Player1Guess, Player2Guess, Player3Guess,Player4Guess, GuessTotal, GuessProduct;
const int FirstCode = (rand() % difficulty) + difficulty;
const int SecondCode = (rand() % difficulty) + difficulty;
const int ThirdCode = (rand() % difficulty) + difficulty;
const int FourthCode = (rand() % difficulty) + difficulty;
// Adding the four codes and multiplying the four codes.
const int CodeTotal = FirstCode + SecondCode + ThirdCode + FourthCode;
const int CodeProduct = FirstCode * SecondCode * ThirdCode * FourthCode;
std::cout << "\nEnter the four numbers " << '\n';
std::cout << "The four numbers sum upto : " << CodeTotal << '\n';
std::cout << "The multiple of four number is : " << CodeProduct << '\n';
// Guessing part of the game
std::cout << "Enter the codes " << '\n';
cin >> Player1Guess >> Player2Guess >> Player3Guess >> Player4Guess;
GuessTotal = Player1Guess + Player2Guess + Player3Guess + Player4Guess;
GuessProduct = Player1Guess * Player2Guess * Player3Guess * Player4Guess;
if ((GuessTotal == CodeTotal) && (GuessProduct == CodeProduct)) {
return true;
}
else{
return false;
}
}
void game(int difficulty, int chance, bool loop){
const int MAX_LEVEL = 5;
PrintIntro(difficulty);
while (loop) {
bool bLevelComplete = PlayGame(difficulty);
if (bLevelComplete == true) {
difficulty++;
if (difficulty == MAX_LEVEL) {
std::cout << "You won !" << '\n';
break;
}
else
{
PrintIntro(difficulty);
}
}else{
chance = chance - 1;
std::cout << "You have " << chance << " chances left \n";
if (chance == 0) {
std::cout << "You lose" << '\n';
loop = false;
}
}
}
}
int main() {
int difficulty = 1;
int chance = 5;
bool loop = true;
game(difficulty, chance, loop);
return 0;
}