Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hokemon - Static Variables Write a simple game called \"Hokemon\" that loops til

ID: 3750639 • Letter: H

Question

Hokemon - Static Variables

Write a simple game called "Hokemon" that loops till the user quits:

You should create a class called Hokemon that creates these magical creatures.

Write methods to make them sing and dance.

They can be alive or dead.These creatures die at a temperature >101.

Only one can die at a time. But if it is the last Hokemon, it says"I am the last living hokemon, I cannot die"!

In your driver program, instantiate 4 hokemon and put them inside a vector.

Generate random temperatures such that if temp>101, then one of them dies.

So far i have this:

hokemon.h

#ifndef hokemon_h

#define hokemon_h

Class Hokemon {

private:

static int count;

public:

void getCount();

bool killHokemon();

};

#endif

hokemon.cpp

#include "hokemon.h"

#include <iostream>

void Hokemon::getCount(){

return count;

}

bool Hokemon::killHokemon(){

count --;

}

main.cpp

#include <iostream>

#include <vector>

#include <cstdlib>

#include "hokemon.h"

using namespace std;

int main() {

vector<Hokemon> v4;

bool gameover=false;

for (int i=0; i<0;i++){

Hokemon h(i);

v.push_back(h);

}

while(!gameover){

int temp = rand()%10+99;

cout << "temperatrue is " << temp << endl;

if(temp>101){

}

}

}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//hokemon.h

#ifndef hokemon_h

#define hokemon_h

class Hokemon {

private:

              static int count;

              bool alive; //denotes if the hokemon is alive or not

              int id; //a unique id for each hokemon

public:

              //static method to reset count to 0, for playing again

              static void reset();

              //constructor

              Hokemon();

              //returns the current count

              int getCount();

              //returns true if a hokemon is successfully killed

              bool killHokemon();

              //other method prototypes to make hokemon sing and dance

              void dance();

              void sing();

};

#endif

//hokemon.cpp

#include "hokemon.h"

#include <iostream>

using namespace std;

//initializing static variable

int Hokemon::count=0;

void Hokemon::reset(){

              //resetting count to 0

              count=0;

}

Hokemon::Hokemon(){

              //creating a hokemon

              alive=true;

              ++count; //incrementing count

              id=count; //assigning current count as unique id

}

int Hokemon::getCount(){

              return count;

}

bool Hokemon::killHokemon(){

              if(count==1){

                             //last one standing

                             cout<<"Hokemon "<<id<<": I am the last living hokemon, I cannot die!"<<endl;

                             return false;

              }else if(alive){

                             //killing

                             cout<<"Hokemon "<<id<<" is dead!"<<endl;

                             count--;

                             alive=false;

                             return true;

              }else{

                             return true; //already dead

              }

}

void Hokemon::dance(){

              //making hokemon dance if alive

              if(alive){

                             cout<<"Hokemon "<<id<<" is dancing!"<<endl;

              }

}

void Hokemon::sing(){

              //making hokemon sing if alive

              if(alive){

                             cout<<"Hokemon "<<id<<" is singing!"<<endl;

              }

}

//main.cpp

#include <iostream>

#include <vector>

#include <cstdlib>

#include "hokemon.h"

using namespace std;

//method to play the game for once

void play(){

              //defining a vector

              vector<Hokemon> v;

              //loop controller

              bool gameover=false;

              //resetting count to 0 (for multiple runs)

              Hokemon::reset();

              //creating and adding 4 Hokemons to a vector

              for (int i=0; i<4;i++){

                             Hokemon h;

                             v.push_back(h);

              }

              //looping until game over

              while(!gameover){

                             //generating a random temperature

                             int temp = rand()%10+99;

                             cout << "temperature is " << temp << endl;

                             if(temp>101){

                                           //trying to kill a random hokemon

                                           int randomIndex=rand()%v.size();

                                           if(v[randomIndex].killHokemon()){

                                                          //killed successfully, removing from vector

                                                          v.erase(v.begin()+randomIndex);

                                           }else{

                                                          //killing failed, last living hokemon. end of loops

                                                          gameover=true;

                                           }

                             }else{

                                           //temperature under 101

                                           //looping through all alive hokemons

                                           for(int i=0;i<v.size();i++){

                                                          //making them sing or dance in random order

                                                          if(rand()%2 ==0){

                                                                        v[i].dance();      

                                                          }else{

                                                                        v[i].sing();

                                           }            

                             }

              }

}

             

}

int main() {

              char choice='y';

              //looping until user decide to quit

              while(choice=='y' || choice=='Y'){

                             //playing the game

                             play();

                             cout<<" Do you want to play again? (y/n): ";

                             cin>>choice;

              }

}

//Output

temperature is 100

Hokemon 1 is singing!

Hokemon 2 is dancing!

Hokemon 3 is dancing!

Hokemon 4 is singing!

temperature is 103

Hokemon 3 is dead!

temperature is 107

Hokemon 2 is dead!

temperature is 103

Hokemon 4 is dead!

temperature is 104

Hokemon 1: I am the last living hokemon, I cannot die!

Do you want to play again? (y/n): y

temperature is 106

Hokemon 2 is dead!

temperature is 100

Hokemon 1 is singing!

Hokemon 3 is dancing!

Hokemon 4 is singing!

temperature is 105

Hokemon 1 is dead!

temperature is 103

Hokemon 3 is dead!

temperature is 102

Hokemon 4: I am the last living hokemon, I cannot die!

Do you want to play again? (y/n): y

temperature is 101

Hokemon 1 is singing!

Hokemon 2 is dancing!

Hokemon 3 is dancing!

Hokemon 4 is singing!

temperature is 106

Hokemon 3 is dead!

temperature is 100

Hokemon 1 is dancing!

Hokemon 2 is singing!

Hokemon 4 is dancing!

temperature is 106

Hokemon 2 is dead!

temperature is 104

Hokemon 1 is dead!

temperature is 102

Hokemon 4: I am the last living hokemon, I cannot die!

Do you want to play again? (y/n): n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote