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 201 202
|
/*
Using user input, calculates a "random" target distance. Then using user inputs
for the angle and speed of their projectile, calulcates if the target was hit, if not, the input is
gathered again until the target is hit.
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <math.h>
using namespace std;
#define M_PI (Radians = Degrees*M_PI / 180);
bool GetPlayerInput( string sPrompt, string sError, float& fResult, float& fMin, float& fMax);
int generateRandomNumber( int& iRandom );
int main()
{
//float fPlayerAngle; // ask the player for an initial angle
float fProjectile; //ask the player for an initial speed
float fDistanceFormula = 0; // X value for calculating the distance traveled
int iTries = 0; // tries the player has made
int iRandom; // varying the Target by getting user input to generate a random number
int iNumbersAdded; // used in calculating the target distance
float fRandNumber = 0; // the distance of the target
float fResult;
float fMin;
float fMax;
cout << "Welcome to the Text-Based Artillery Game!";
cout << "\n\nPut '0' in response to Angle or Speed to exit the program.";
/* Calculating a "Random" Number for the Target's distance */
generateRandomNumber(iRandom);
do //Game Loop Beginning
{
/******* Get Angle *********/
GetPlayerInput( "\nEnter an angle: ", "\nThat is not a valid angle, try again: ", fResult, fMin, fMax);
float fPlayerAngle;
fPlayerAngle = fResult;
/*cout << "\n\nChoose an angle of 15, 30, 45, 60, or 75: ";
do
{
cin >> iPlayerAngle;
if( iPlayerAngle > 0, iPlayerAngle < 90, (iPlayerAngle % 15) == 0 )
if( iPlayerAngle == 0)
return 0;
else
break;
else
cout << "\nThat's not a valid angle. It must be one of the five given values: ";
} while( iPlayerAngle <= 0, iPlayerAngle > 90, (iPlayerAngle % 15) != 0 );
*/
/******** Get Speed **********/
GetPlayerInput ("Enter a speed from 1-10: ", "That is not a valid speed. It must be from 1.0 and 10.0: ", fResult, fMin, fMax);
float fPlayerSpeed;
fPlayerSpeed = fResult;
/* cout << "\nNow choose an initial speed from 1 - 10: ";
do
{
cin >> fPlayerSpeed;
if( fPlayerSpeed >= 1.0, fPlayerSpeed <= 10.0 )
if( fPlayerSpeed == 0 )
return 0;
else
break;
else
cout << "\nThat is not a valid speed. It must be between 1.0 and 10.0.";
} while( fPlayerSpeed = 0, fPlayerSpeed < 1.0, fPlayerSpeed > 10.0 );
*/
/******** get X value for Distance Formula ********
switch( iPlayerAngle )
{
case 15:
fDistanceFormula = 0.5;
break;
case 30:
fDistanceFormula = 0.866;
break;
case 45:
fDistanceFormula = 1.0;
break;
case 60:
fDistanceFormula = 0.866;
break;
case 75:
fDistanceFormula = 0.5;
break;
}
*/
/******* Calculate the distance the projectile flew ***********/
fProjectile = fPlayerSpeed * fPlayerSpeed * sin (2 * fPlayerAngle) / 10;
cout << "\n\nYour projectile flew a total distance of: \n\n" << fProjectile;
++iTries;
} while( (fProjectile <= (iRandom - 0.5)) || (fProjectile >= (iRandom + 0.5)) ); // Game Loop End
iTries == 1 ? cout << "\n\nYou got the target in 1 try!" : cout << "\n\nIt took you " << iTries << " tries to get the Target!";
std::cin.sync();
std::cin.get();
return 0;
}
int generateRandomNumber( int& iRandom)
{
srand( time( NULL ));
iRandom = ( rand() % 9) + 1;
cout << "\n\nThe target is a distance of " << iRandom << " away.";
cout << "\n\nTry to get a distance between " << ( iRandom - 0.5 );
cout << " and " << (iRandom + 0.5);
return iRandom;
}
bool GetPlayerInput( string sPrompt, string sError, float& fResult, float& fMin, float& fMax)
{
do
{
fMin = 0;
fMax = 90;
cout << sPrompt;
cin >> fResult;
if( fResult == 0 )
return false;
if( fResult < fMin || fResult > fMax )
cout << sError;
} while( fResult < fMin || fResult > fMax);
return true;
do
{
fMin = 1.0;
fMax = 10.0;
cout << sPrompt;
cin >> fResult;
if( fResult == 0 )
return false;
if( fResult < fMin || fResult > fMax )
cout << sError;
} while( fResult >= fMin || fResult <= fMax );
return true;
}
|