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
|
//libraries
#include <cctype> //reference to tolower
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
//Programmer defined data types
//playing cards
struct GuessMade
{
int chosen;
}; //
//Special compiler dependent definitions
//NONE
//global constants/variables
const int MAX_TRIES = 100;
//Programmer defined functions
void introduction(string obj, string ins); //user introduction
bool checkValid (int target, int uG, GuessMade*, int n); //input validation
bool dupeCheck (int uG, GuessMade*, int n); // dupe check
bool getYesorNo();
//main program
int main()
{
//Data
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
string objective = "play the overUnder game."; //program objective
string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
int ngameLogs = 0;
GuessMade userPick[MAX_TRIES];
int userGuess; //users guess
int compGuess = 1 + rand() % 100; //random number
//user introduction
introduction(objective, instructions);
//game loop
do
{
//user input
cout << "What is your guess? " << endl;
cin >> userGuess;
cin.ignore(1000, 10);
//input validation
if (checkValid(compGuess, userGuess, &userPick[ngameLogs], ngameLogs))
{
//update list of non dupe guesses
userPick[ngameLogs++].chosen = userGuess;
//user feedback
if (userGuess == compGuess)
{
cout <<"Thats right -- it's " << compGuess << endl;
}//guess is correct
else if (userGuess > compGuess)
{
cout <<"Thats too high -- guess again " << endl;
}//guess is too high
else if (userGuess < compGuess)
{
cout <<"Thats too low -- guess again " << endl;
}//guess is too low
}//checkvalid
}while (getYesorNo()); // game loop
}//main
// user introduction
void introduction(string obj, string ins)
{
//data
//obj is the program objective
//ins is the user instructions
//output user introduction
cout << "Objective: This program will " << obj << endl;
cout << "Programmer: Prosper\n";
cout << "Editor(s) used: Notepad\n";
cout << "Compiler(s) used: TDM MinGW\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl;
cout << ins << endl << endl;
}//introduction
//input valid
bool checkValid(int target, int uG, GuessMade *a, int n)
{
//data
//target
//uG
//gameLog
//n
bool result = true; //false is not valid, true is valid
//input validation
if (uG < 1 || uG > 100)
{
result = false;
cout << "not a valid value--guess again [1-100]: ";
}// if valid
if (dupeCheck(uG, a, n))
{
result = false;
cout << "you already guessed " << uG << " -- guess again: ";
}//check duplicate
return result;
}//checkValid
//check if user guess is a duplicate guess
bool dupeCheck(int uG, GuessMade *a, int n)
{
//data
//GuessMade is the collection of non-duplicate guesses
//n is the size counter of the list
//uG is the user guess
bool result = false; //false is not a duplicate, true is a duplicate guess
int i; //loop index for traversing the list
//check if duplicate guess
for(i=0; i < n; i = i + 1)
{
if (uG == a[i].chosen)
{
result = true;
}//if
}//for i
return result;
}//checkDuplicatebool
bool getYesorNo()
{
//
char letsGo;
do
{
cout <<"Do you want to play again? [Y for yes/N for no] ";
cin >> letsGo;
cin.ignore(1000, 10);
cout << endl;
if (toupper(letsGo) == 'N') return false;
if (toupper(letsGo) == 'Y') return true;
}while (toupper(letsGo) != 'Y' || toupper(letsGo) != 'N');
}//playAg
| |