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

Game with arrays , classes, random genarator Spider is a game involving a die an

ID: 3541041 • Letter: G

Question

Game with arrays , classes, random genarator

Spider is a game involving a die and Hangman type game. It is 2 player game. In this game, each player tries to finish making a full spider before the other players and the rules are as follows: Each turn, the current player will roll one die. Depending on the number, the player will add a body part to their spider. If the player already has the maximum number of that part then their turn is over and no part is added. If a player rolls the number for a part but does not have another part that is required, then the player's turn is over and no part is added. If a part is added, the player rolls again and tries to continue adding parts until they are unable. A roll of 1 is for a body and only 1 body is allowed. A roll of 2 is for a head, only 1 head is allowed, and a body is required. A roll of 3 is for two legs, only 6 legs are allowed, and a body is required. A roll of 4 is for an eye, only 2 eyes are allowed, and a head is required. A roll of 5 is for a feeler, only 2 feelers are allowed, and a head is required. A roll of 6 is for a tail, only 1 tail is allowed, and a body is required. Whoever completes their spider first wins. Use classes and random generator

Explanation / Answer

please rate - thanks


get back to me if any problems or questions-I will make corrections


tested with DEV C++



#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int menu();
class Spider
{private:
         bool head;
         bool body;
         int legs;
         int eyes;
         int feelers;
         bool tail;
         int items_needed;
public:
       Spider()
       {head=false;
       body=false;
       legs=6;
       eyes=2;
       feelers=2;
       tail=false;
       items_needed=13;
       }
bool turn()
{int die;
bool more=true;
while(more&&items_needed>0)
{more=false;
die=rand()%6+1;
   cout<<die<<" rolled ";
   switch (die)
     {case 1: if(!body)
            { body=true;
             items_needed--;
              cout<<"body received, turn continues ";
              more=true;
              }
             else
                 cout<<"Already have a body ";
              break;
       case 2: if(!body)
                   cout<<"must have a body ";
               else if(!head)
                   { head=true;
                    items_needed--;
                    cout<<"head received, turn continues ";
                    more=true;
                   }
               else
                   cout<<"Already have a head ";
                     
                break;
        case 3: if(!body)
                     cout<<"must have a body ";
                 else if(legs>0)
                      { legs-=2;
                        items_needed-=2;
                        cout<<"2 legs received "<<legs<<" more needed turn continues ";
                        more=true;
                       }
                 else
                      cout<<"No legs needed ";
                 break;
           case 4: if(!head)
                     cout<<"must have a head ";
                 else if(eyes>0)
                      { eyes--;
                        items_needed--;
                        cout<<"eye received "<<eyes<<" more needed turn continues ";
                        more=true;
                       }
                 else
                      cout<<"No eyes needed ";
                   break;
           case 5:if(!head)
                     cout<<"must have a head ";
                 else if(feelers>0)
                      { feelers--;
                        items_needed--;
                        cout<<"feeler received "<<feelers<<" more needed turn continues ";
                        more=true;
                       }
                 else
                      cout<<"No feelers needed ";
                  break;
           case 6: if(!body)
                   cout<<"must have a body ";
               else
                  if(!tail)
                     { tail=true;
                    items_needed--;
                    cout<<"tail received, turn continues ";
                    more=true;
                   }
                  else
                       cout<<"Already have a tail ";    
                  break;
          }
    system("pause");
    }
if(items_needed>0)
      return false;
else
      return true;
     
}
};

int main()
{Spider s[2];
srand(time(0));
int i=1;
bool gameover=false;
while(!gameover)
   {i=1-i;    //flips i between 0 and 1
   cout<<"Player "<<i+1<<" turn ";
    gameover=s[i].turn();
    cout<<endl;
    }
cout<<"The winner is player "<<i+1<<endl;
system("pause");  
return 0;
}