Implement a two player game. You are given a main, and a game source file and th
ID: 3761038 • Letter: I
Question
Implement a two player game. You are given a main, and a game source file and the creature class definition file.
1. Create the enumerator type in the creature.h file
2. Allocate the creature (derived) objects in the RequestCreatureType() function (game.cpp) by using the new operator.
3. Implement all the methods for the Creature class and its derived classes.
4. Make the game compile and run successfully.
Files needed is in my google drive in a zipfile -
https://drive.google.com/folderview?id=0B3xYFHxCq-GTZThlNGxsMXloSkU&usp=sharing
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <unistd.h>
#include <string>
#include "game.h"
// Request a creature type from the user
Creature *RequestCreatureType(int playerNumber){
int p;
Creature *cp = NULL;
cout << endl;
do{
cout << "Player " << playerNumber << " Type? " << endl;
cout << "Type 0: Human " << endl;
cout << "Type 1: Demon " << endl;
cout << "Type 2: Balrog " << endl;
cout << "Type 3: Elf " << endl;
cout << " -------------- " << endl;
cin >> p;
switch(p){
case HUMAN:
cp = new .....
break;
case DEMON:
cp = new .....
break;
case BALROG:
cp = new .....
break;
case ELF:
cp = new .....
break;
default:
cout << "Please enter a number between 0-3 " << endl;
break;
}
}while(cp == NULL);
return cp;
}
// Plays the game until one winner
void PlayGame(Creature **cp, int numberOfPlayers){
// Variables to hold dice
int dice[numberOfPlayers];
// Place holder for creatures being alive
bool alive[NUMBER_OF_PLAYERS] = {true};
int numberAlive;
// Stay in loop until one winner
do
{
// Rolling dice for each player
for(int i = 0; i < NUMBER_OF_PLAYERS; i++)
dice[i] = rand() % 6;
if(dice[P1] > dice[P2])
cp[P2]->fight(*cp[P1]);
else if(dice[P2] > dice[P1])
cp[P1]->fight(*cp[P2]);
else
{
cp[P1]->fight(*cp[P2]);
cp[P2]->fight(*cp[P1]);
}
// Check for only survivor
numberAlive = 0;
for(int i = 0; i < NUMBER_OF_PLAYERS; i++){
if(cp[i]->alive())
numberAlive++;
}
}while(numberAlive >= numberOfPlayers);
}
// Prints the winner
void PrintWinner(Creature **cp, int numberOfPlayers){
for(int i = 0; i < numberOfPlayers; i++){
if(cp[i]->alive()){
cout << " player "<< cp[i]->getPlayerNumber() << " wins!"<< endl;
playVideo(*cp[i]);
}
}
}
// Winner Video function
void playVideo(Creature &_c)
{
string line;
ifstream fp[N+1];
string fname, tmp;
int i=1; // number value for video files
char i_hold[4]; // array to hold the integer-to-string value
cout << endl << "Press any button to continue..." << endl;
cin.ignore();
cin.get();
if (!_c.getSpecies().compare("Human" ) )
fname = "ascii/ave/ahve";
else if (!_c.getSpecies().compare("Demon" ) )
fname = "ascii/hvd/ahvd";
else if (!_c.getSpecies().compare("Balrog" ) )
fname = "ascii/bvh/abvh";
else
fname = "ascii/evh/evh";
for(i=1;i<=N;i++)
{
// convert integer to string and concatenate to get filenames
snprintf(i_hold,sizeof(i),"%d",i);
tmp = fname + i_hold + ".txt";
fp[i].open(tmp.c_str());
if (fp[i])
{
ClearScreen();
// get length of file
fp[i].seekg (0, fp[i].end);
int length = fp[i].tellg();
fp[i].seekg (0, fp[i].beg);
char * buffer = new char [length/2]; // Lot of white space we don't need all of it
fp[i].read (buffer,length/2 );
cout << buffer;
usleep(105000);
fp[i].close();
delete[] buffer;
}
}
}
// New Game Video function
void playVideo(string _name)
{
string line;
ifstream fp[N+1];
string fname = _name;
string tmp;
int i=1;
char i_hold[4];
for(i=1;i<=N;i++)
{
snprintf(i_hold,sizeof(i),"%d",i);
tmp = fname + i_hold + ".txt";
fp[i].open(tmp.c_str());
if (fp[i])
{
ClearScreen();
// get length of file:
fp[i].seekg (0, fp[i].end);
int length = fp[i].tellg();
fp[i].seekg (0, fp[i].beg);
char * buffer = new char [length/2];
fp[i].read (buffer,length/2 );
cout << buffer ;
usleep(100000);
fp[i].close();
delete[] buffer;
}
}
}
void ClearScreen()
{
cout << string( 100, ' ' );
}
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "game.h"
using namespace std;
int main()
{
// Seed random generator
srand (time(NULL));
// Variables
Creature *cp[NUMBER_OF_PLAYERS]={NULL};
char playAgain;
// Play video
playVideo("ascii/new_game/ang");
do{
// Ask user for creature type
for(int i = 0; i < NUMBER_OF_PLAYERS; i++)
cp[i] = RequestCreatureType(i+1);
// Print summary of creatures per player
cout << endl;
for(int i = 0; i < NUMBER_OF_PLAYERS; i++)
cout << "Player " << i+1 << " is a " << cp[i]->getSpecies() << endl;
// Play the game
PlayGame(cp, NUMBER_OF_PLAYERS);
// Print the winner
PrintWinner(cp, NUMBER_OF_PLAYERS);
// Ask players if they want to play again
cout << endl;
do{
cout << endl <<"----- Play Again? (y/n) -----" << endl;
cin >> playAgain;
}while( (playAgain != 'y') && (playAgain != 'n'));
}while( playAgain == 'y' );
// Cleaning up the creature objects
for(int i = 0; i < NUMBER_OF_PLAYERS; i++)
delete cp[i];
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.