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
|
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
using namespace System;
void PlayHotCold(int n, int g);
void PlayHighLow(int n, int g);
int getInt();
int prev_category, prev_difference;
int main()
{
int choice, n, g, guesscount;
bool quit;
srand((unsigned)time(0));
cout << "Welcome to the number guessing game." << endl;
cout << "How would you like to play? (1=Hot/Cold 2=High/Low 0=Quit):";
cin >> choice;
while (choice != 0)
{
n = (rand() % 100) + 1;
cout << "I am thinking of a number between 1 and 100..." <<endl;
quit = false;
guesscount = 0;
prev_category = 0;
prev_difference = 0;
while (!quit)
{
g = getInt();
guesscount++;
if (g == 0)
{
quit = true;
cout << "Sorry that you wish to quit. You tried "<<guesscount<<" times. The number was: " << n << endl; //sorry message with guess count here
}
else if (g == n)
{
cout << "You guessed it! (In " << guesscount << " tries)" << endl;
quit = true;
}
else
{
if (choice == 1) //play HotCold
{
PlayHotCold(n, g);
}
else
{
PlayHighLow(n, g);//PlayHighLow
}
}
}//end of while(!quit)
cout << "How would you like to play? (1=Hot/Cold 2=High/Low 0=Quit):";
cin >> choice;
} //end of while for choice
cout << "Thanks for playing" << endl;
system("Pause");
return 0;
}
void PlayHighLow(int n, int g)
{
if (g < n)
{
cout << "Sorry, your guess was too low" << endl;
}
else
{
cout << "Sorry, your guess was too high" << endl;
}
return;
}
void PlayHotCold(int n, int g)
{
//cout << "I can't play that game yet" << endl;
int category, difference;
difference = abs(n - g);
if (difference >= 75)
{
category = 1;
cout << " Very Cold";
}
else if (difference >= 50)
{
category = 2;
cout << " Cold";
}
else if (difference >= 25)
{
category = 3;
cout << " Warm";
}
else if (difference >= 11)
{
category = 4;
cout << " Very Warm";
}
else
{
category = 5;
cout << " Hot";
}
if (category == prev_category)
{
if (difference == prev_difference)
{
cout << " - same degree";
}
else if (difference > prev_difference)
{
cout << " Getting Colder";
}
else
{
cout << " Getting Warmer";
}
}
cout << endl << endl;
prev_category = category;
prev_difference = difference;
return;
}
int getInt()
{
int g;
bool baddata = false;
do
{
baddata = false;
cout << "What is your guess?: ";
cin >> g;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "I could not decifer your input as an integer value" << endl;
baddata = true;
}
else if (g < 0)
{
cout << "Please enter a non-negative number" << endl;
baddata = true;
}
} while (baddata);
return g;
}
|