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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
void instructions();
int beginGame(int[]);
void displayBoard(int board[][3]);
bool testWinner(int, int, int[]);
int main()
{
int board_1[4][3] = { 38, 11, 83,
15, 6, 33,
10, 2, 20,
86, 0, 95};
int board_2[4][3] = { 28, 10, 55,
89, 17, 98,
22, 4, 31,
69, 0, 78};
int board_3[4][3] = { 90, 9, 45,
66, 12, 48,
34, 7, 70,
44, 0, 26};
int ansBoard[3] = { 14,15,8 };
int usedBoards[3] = {0,0,0};
int userInput, number_wins = 0, guesses = 0, boardNumber;
int board[4][3];
instructions();
do
{
beginGame(usedBoards);
boardNumber = beginGame(usedBoards);
switch(boardNumber){
case 0:
displayBoard(board_1);
break;
case 1:
displayBoard(board_2);
break;
case 2:
displayBoard(board_3);
break;
}
for (int i = 1; i < 4; i++)
{
cout << endl << "Enter your guess or zero to exit";
cin >> userInput;
int ans = userInput;
if (userInput == 0){
cout << endl << "Exiting..";
return 0;
}
else{
while (userInput < 0){
cout << endl << "Please enter a valid value";
cin >> userInput;
}
}
testWinner(ans, boardNumber, ansBoard);
if (testWinner(ans, boardNumber, ansBoard) == true)
{
switch (boardNumber)
{
case 0:
number_wins++;
usedBoards[0] = 1 ;
break;
case 1:
number_wins++;
usedBoards[1] = 1 ;
break;
case 2:
number_wins++;
usedBoards[2] = 1 ;
}
if ((usedBoards[0] == 0) || (usedBoards[1] == 0) || (usedBoards[2] == 0))
{
cout << endl << "Do you want to play again or exit (0)";
cin >> userInput;
if (userInput == 0){
cout << endl << "Exiting..";
return 0;
}
else
{
while (userInput < 0)
{
cout << endl << "Please enter a valid value";
cin >> userInput;
}
if (userInput != 0)
{
beginGame(usedBoards);
}
}
}
break;
}
else
{
guesses++;
cout << endl << "Please try again, that was incorrect";
cin >> userInput;
}
}
if (guesses > 3)
{
cout << endl << "Do you want to play again?. 1 to play again, 0 to exit";
cin >> userInput;
if (userInput == 0){
cout << endl << "Exiting..";
return 0;
}
else{
while (userInput < 0)
{
cout << endl << "Please enter a valid value";
cin >> userInput;
}
}
if (userInput != 0)
{
main();
}
}
}while (number_wins < 3);
cout << endl << "You are the number guessing Champion !!";
return 0;
}
void instructions()
{
cout << "**********************************" << endl;
cout << endl << "MISSING NUMBERS GAME" << endl;
cout << "A Fun Brain Game" << endl << endl << endl;
cout << "Please enter a whole number to guess the missing number...";
cout << endl << "Program Developed by: Rohan Jakhete" << endl;
cout << "**********************************" << endl;
}
int beginGame(int usedBoards[])
{
int randomNum;
srand(time(0));
do{
randomNum = rand() % 3;
}while (usedBoards[0] != 0 || usedBoards[1] != 0 || usedBoards[2] != 0);
return randomNum;
}
void displayBoard(int board[4][3])
{
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 3; y++)
{
cout << setw(4) << board[x][y] << " ";
}
cout << endl;
}
}
bool testWinner(int ans, int boardNum, int ansBoard[]){
if (ans == ansBoard[boardNum]) {
cout << endl << "Nice Job!!";
return true;
}
else
{
cout << endl << "Sorry, that is incorrect";
return false;
}
}
|