PDA

View Full Version : Need help! in C++....please help me


rehmankhan
07-04-2006, 04:33 PM
Description of Program

Use the files from Assignment #2, rewrite spaceship.cpp (I have posted this file below) to write a space ship Fighting game.

Two Player Game –

Ask each player for his or her name, and then give him starting values for his stats (up to you). Now take turns asking the players whether they want to attack or fix damage on their ship. If they attack, use the roller to decide if they hit and then use their laser power and the other player’s hull strength to decide what values they need to roll in order to hit, then use laser power to decide on damage. If the other player’s damage points goes over his max damage points, then he is dead and loses the battle. Award Laser or Hull upgrades to the winner. Give them the option of playing again.


Example of calculating a hit for player 1

Roller oneToTwenty(1, 20);
Roller damageRoller(1, 1 + player1.GetLaserPower() );
int dmgAmt;
int roll = oneToTwenty.GetRoll();
if( player_2.I****(roll) )
{
dmgAmt = damageRoller.GetRoll();
player_2.GiveDamage(dmgAmt);
}
// Check if player 2 is dead , if so, inform him/her of the
// victory , otherwise continue to player 2’s turn

One Player Game –

Create several different opponent spaceships of increasing strength (at least 6) . Make the player fight them one after the other in a similar fashion to the two player game, awarding upgrades upon each victory.
You may make your game different than specified here, ONLY with instructor approval.

Here is an example of code output

Choose ..

0 - Exit
1 - One Player Game
2 - Two Player Game

1
What is your name pilot? Chris
Ok, lets take off captain Chris

You have encountered a Nebulan Fleet
What do you wish to do?
1 - attack
2 - fix damage
3 - check stats

1
You rolled a 9 on your attack
You missed
The Nebulan Fleet attacks you with its lasers
The enemy has missed

What do you wish to do?
1 - attack
2 - fix damage
3 - check stats

1
You rolled a 18 on your attack
You hit the enemy giving 1 damage
The Nebulan Fleet attacks you with its lasers
The enemy has hit you! Causing 2 damage

What do you wish to do?
1 - attack
2 - fix damage
3 - check stats

2
You fix 3 damage to your ship
The Nebulan Fleet attacks you with its lasers
The enemy has missed

What do you wish to do?
1 - attack
2 - fix damage
3 - check stats
1
You rolled a 13 on your attack
You hit the enemy giving 1 damage
The Nebulan Fleet attacks you with its lasers
The enemy has hit you! Causing 1 damage

some time later . . .

1
You rolled a 18 on your attack
You hit the enemy giving 2 damage
You have defeated the enemy Space Raptor
Congratulations
You receive
a Laser Upgrade
You have encountered a Stealth Interceptor

What do you wish to do?
1 - attack
2 - fix damage
3 - check stats

3
Your Stats are

Name = Chris
Damage = 4 out of 10
Hull Strength = 2
Laser Power = 3

What do you wish to do?
1 - attack
2 - fix damage
3 - check stats

-----------------------------------------------

I am posting my spaceship.cpp, which is working fine. This home work question is just an extension to my files. So, if anybody knows how to get this output, please let me know. My home work is due July 13th. I have posted my codes below here in reply.

-----------------
//File: Spaceship.h

# include <string>
using std::string;

class Spaceship
{
public:

// A default constructor, it takes no parameters and
//sets the data member variables to some default values.
Spaceship();

Spaceship(string, int, int, int, int);

// Get - Set accessor methods for data members

void setPilotName(string); // Data member for piolt's name
string getPilotName();
void setMaxDamagePoints(int); // Data member for Max Damage Points
int getMaxDamagePoints();
void setCurrentDamagePoints(int); // Data member for Current Damage Points
int getCurrentDamagePoints();
void setHullStrength(int); // Data member for Hull Strength
int getHullStrength();
void setLaserPower(int); // Data member for Laser Power
int getLaserPower();

bool IsDead();
// This member function Returns true if damage points
//is greater than or equal to max damage points
void FixDamage(int);
// This member function Subtracts amt from Damage Points
void UpgradeHull();
// This member function Upgrade Hull Strength
void UpgradeLasers();
// This member function Upgrades Laser Power
void GiveDamage(int);
// This member function should add amount of damage points to DP
bool I****(int);
// This member function returns true if
// roll is higher than 10 + Hull Strength
void PrintStats();
// This member function prints pilot name, damage points,
//max damage points, laser power and hull strength

private: //Private member functions

// Declaration of variables

string pilotname; // Variable for pilot name is declared as a string
int maxdamagepoints; // Variable for maximum damange points is declared as an integer
int currentdamagepoints; // Variable for current damage points declared as an integer
int hullstrength; // Variable for hull strength is declared as an integer
int laserpower; // Variable for laser power is declared as an integer

};
----------------

//File: Spaceship.cpp

#include <string>
#include <iostream>
using std::string;
using std::endl;
using std::cout;

#include "Spaceship.h"

Spaceship::Spaceship(string name, int maxdamage, int currentdamage, int strength, int power)
{
pilotname = name;
maxdamagepoints = maxdamage;
currentdamagepoints = currentdamage;
hullstrength = strength;
laserpower = power;
}

void Spaceship::setPilotName(string name)
{
pilotname = name;
}

string Spaceship::getPilotName()
{
return pilotname;
}

void Spaceship::setMaxDamagePoints(int maxdamage)
{
maxdamagepoints = maxdamage;
}

int Spaceship::getMaxDamagePoints()
{
return maxdamagepoints;
}

void Spaceship::setCurrentDamagePoints(int currentdamage)
{
currentdamagepoints = currentdamage;
}

int Spaceship::getCurrentDamagePoints()
{
return currentdamagepoints;
}

void Spaceship::setHullStrength(int strength)
{
hullstrength = strength;
}

int Spaceship::getHullStrength()
{
return hullstrength;
}

void Spaceship::setLaserPower(int power)
{
laserpower = power;
}

int Spaceship::getLaserPower()
{
return laserpower;
}

bool Spaceship::IsDead()
{
if ( currentdamagepoints >= maxdamagepoints)
// if current damage points are greater or qual to
// maximum damage points, return true
return true;
else
// else return false
return false;
}

void Spaceship::FixDamage(int amt)
{
if (amt >= currentdamagepoints)
// If amt is greater or equal to current damage points,
// currentdamagepoints is qual to currentdamagepoints - amt
currentdamagepoints = currentdamagepoints - amt;
}

void Spaceship::UpgradeHull()
{
hullstrength = hullstrength + 1;
//Adds one to hull strength and sets it as
// a new value of hull strength
}

void Spaceship::UpgradeLasers()
{
laserpower = laserpower + 1;
//Adds one to laser power strength and sets it as
// a new value of laser power
}

void Spaceship::GiveDamage(int amount)
{
currentdamagepoints = currentdamagepoints + amount;
//Adds current damage points and amount and makes it
//new value for current damage points
}

bool Spaceship::I****(int roll)
{
if ( roll > (10 + hullstrength) )
return true;
else
return false;
}

void Spaceship::PrintStats()
{
cout << endl;
cout << "YOUR STATISTICS" << endl;
cout << endl;
cout << "Your Name: " << pilotname << endl;
cout << "The Current Damage is " << currentdamagepoints << " out of possible damage of " << maxdamagepoints << endl;
cout << "The Hull Strenght: " << hullstrength << endl;
cout << "The Laser Power: " << laserpower << endl;
}
------------------

//File: Spaceshiptest.cpp

#include <iostream>
#include <ctime>
#include <string>
#include "Roller.h"
#include "Spaceship.h"

using std::endl;
using std::cout;
using std::cin;

int main()
{
srand(static_cast<unsigned int>(time(NULL)));

Roller OneTen(1, 10);
//Sets the random number generator to generate number
// between 1 to 10
Roller roll(1,30);
Roller fix(1,5);

cout << "RANDOM NUMBERS" << endl;
cout << endl;
for(int n = 0; n < 10; n++)
//For loop for n to go 0 to 9
//Loop will run 10 times
cout << OneTen.GetRoll() << endl;
cout << endl;

Spaceship Boat("", 10, 0, 1, 1);
//Delcaration of variables

string pilotsname;
cout << "Enter your Name Pilot: ";
cin >> pilotsname;
// Asks user to enter name
Boat.setPilotName(pilotsname);
//Sets the entered name
cout << "Ok, Lets take off Captain " << Boat.getPilotName() << endl;
cout << endl;

Boat.setMaxDamagePoints(10);
//Set maximum damage points to 10
Boat.setCurrentDamagePoints(0);
//Set current damage points to 0
Boat.setHullStrength(1);
//Set the hull strength to 1
Boat.setLaserPower(1);
//Set the laser power to 1
Boat.PrintStats();
//Print the statistics

Boat.UpgradeHull();
//Upgrade hull stregth
Boat.UpgradeLasers();
//Upgrade laser power
Boat.GiveDamage(Boat.I****(roll.GetRoll()));
Boat.PrintStats();
//Print the statistics
Boat.setMaxDamagePoints(10);
//Set maximum damate points to 10
Boat.setCurrentDamagePoints(13);
//Set current damage point to 11
Boat.PrintStats();
//Print the statistics

if (Boat.IsDead()== 1)
cout << endl;
cout << "GAME OVER" << endl;
cout << endl;

return 0;
}

-----------------

//File: Roller.h

#pragma once /* this tells the compiler that this
file may only be included once to
prevent linker errors */

class Roller //Class Roller
{
public: //publically accessible member functions
/* unsigned int is an integer that cannot
* be negative */
Roller(unsigned int, unsigned int);

/* GetRoll returns a number from
* (and including) m_Min to m_Max */
unsigned int GetRoll(void); /* void here is the same
as no parameters */

private: //Private member functions
// Declaration of variables
unsigned int m_Min;
unsigned int m_Max;
};
------------------

//File: Roller.cpp

#include "Roller.h"
#include <iostream>
using std::cerr;
using std::endl;

// This is a constructor to set the values of the memeber variables using the parameters
Roller::Roller(unsigned int min, unsigned int max)
{
// Check to make sure m_Min < m_Max

if(min > max)
{
/* cerr is a separate stream
* similar to cout, but for errors
* only */
cerr << "min can't be greater than max"
<< endl;
exit(1); // end the program
}

//Definig variables
m_Min = min;
m_Max = max;

}

unsigned int Roller::GetRoll(void)
{
return m_Min + rand() % (m_Max - m_Min + 1);
}