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
|
//This here is for the forum, "stdafx.h" is something I have to use because of the compiler i'm using, MS visual studio express 2012
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main (){
int gameMode;
int playerGuess, computerGuess, previousGuess;
int higherThan, lowerThan;
int tries;
int secretNumber;
char guessRight;
char playAgain;
cout
<< "Welcome to the number guessing game!\n\n";
gameLoop:
cout
<< "Would you like to:\n"
<< "0. Stop\n"
<< "1. Guess a number\n"
<< "2. Have the computer guess a number\n\n"
<< "I would like to: ";
cin >> gameMode;
cout << endl << endl;
playerGuess = 0;
computerGuess= 0;
tries = 0;
// Resetting the guess and amount of tries in case of replay
srand(static_cast<unsigned int>(time(0)));
// Seeding the number generator for both game modes
if (gameMode == 0)
{
return 0;
}
else if (gameMode == 1)
{
secretNumber = (rand() % 100) + 1;
cout << "The number can range between 1 and 100\n";
do
{
cout << "Guess my number: ";
cin >> playerGuess;
++tries;
if (playerGuess < secretNumber)
{
cout << "Nope, higher!";
}
else if (playerGuess > secretNumber)
{
cout << "Nope, lower!";
}
else
{
if (tries == 1)
{
cout << "You guessed it in 1 try, well done!!";
}
else
{
cout << "You guessed it in " << tries << " tries.";
}
cout << "\n\nWould you like to play again? (y/n): ";
cin >> playAgain;
if (playAgain == 'y')
{
cout << "\n\n\n";
goto gameLoop;
}
else
{
return 0;
}
}
cout << endl << endl;
} while (playerGuess != secretNumber);
}
else if (gameMode == 2)
{
computerGuess = (rand() % 100) + 1;
previousGuess = 0;
higherThan = 0;
lowerThan = 101;
// resetting all computer's guessing values in case of replay
//PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM
//My problem is below this text, What i'm trying to do is that when
//the player keeps saying 'h' or 'l' while they said it's higher than say, 74
//but also say it's lower than 74, the program says, "no you're screwing with me, it's 74".
//but I can't get it to work, the most important keywords are 'computerGuess' and 'previousGuess'
//
//
//
//
do
{
cout
<< "My guess is: " << computerGuess << endl
<< "is it higher, lower or just right? (h/l/r): ";
cin >> guessRight;
cout << endl << endl;
if (guessRight == 'h')
{
previousGuess = computerGuess;
higherThan = computerGuess;
computerGuess = rand() % (lowerThan - higherThan) + higherThan +1;
if (computerGuess == previousGuess)
{
cout << "you're just fucking with me, it is " << computerGuess;
guessRight = 'r';
}
else if (computerGuess == lowerThan)
{
--computerGuess;
}
else {}
}
else if (guessRight == 'l')
{
previousGuess = computerGuess;
lowerThan = computerGuess;
computerGuess = rand() % (lowerThan - higherThan) + higherThan +1;
if (computerGuess == previousGuess)
{
cout << "You're just fucking with me, it's " << computerGuess;
guessRight = 'r';
}
}
else
{
if (tries == 1)
{
cout << "Ha, that was easy!";
}
}
} while (guessRight != 'r');
cout << "\n\nWould you like to play again? (y/n): ";
cin >> playAgain;
if (playAgain == 'y')
{
cout << "\n\n\n";
goto gameLoop;
}
else
{
return 0;
}
}
else
{
cout << "Please enter a valid option.\n\n\n";
goto gameLoop;
}
return 0;
}
|