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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
struct Monster
{
std::string Name ;
int CombatPower = 0 ;
static const int NUM_MONSTERS = 25 ;
};
Monster makeMonster()
{
static const std::string names[ Monster::NUM_MONSTERS ] = {
"Charmander", "Bulbasaur", "Squrtile", "Pidgey", "Pikachu", "Sandshrew", "Zubat",
"Mankey", "Abra","Magikarp", "Eevee", "Rattata", "Vulpix", "Scyther", "Jigglypuff",
"Geodude", "Onix", "Staryu","Snorlax", "Mewtwo", "Oddish", "Caterpie","Spearow" ,
"Charizard", "Zapdos"
};
return Monster{ names[ std::rand()%Monster::NUM_MONSTERS ],
int( std::rand()%Monster::NUM_MONSTERS + 1 ) } ;
}
bool caught( const Monster& m )
{
// r == 0, 1, 2 or 3 depending on the Combat Power
int r = 0 ;
if( m.CombatPower > 20 ) r = 3 ;
else if( m.CombatPower > 12 ) r = 2 ;
else if( m.CombatPower > 6 ) r = 1 ;
return ( std::rand() % 4 ) > r ;
}
int main()
{
std::srand( std::time(nullptr) ) ; // call once at the start of main
const Monster m = makeMonster() ;
std::cout << "A monster appeared. name: " << m.Name << " power: " << m.CombatPower << '\n' ;
int pokeballs = 5;
while( pokeballs > 0 )
{
std::cout << "Press enter to try to catch monster: " ;
std::cin.get() ;
if( caught(m) )
{
std::cout<<"\nGotcha! You caught the monster!\n" ;
return 0 ;
}
if( --pokeballs > 0 )
{
std::cout << "\nYou have remaining " << pokeballs <<" chances to catch. "
<< "Try again.\n" ;
}
}
}
| |