c++ word game
Aug 10, 2009 at 3:07am UTC
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage; else { who.isDead = true ; who.health_=0;} }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
return EXIT_SUCCESS;
}
Added a '\n' and 16 ' '.
Last edited on Aug 14, 2009 at 3:48am UTC
Aug 10, 2009 at 12:48pm UTC
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage; else { who.isDead = true ; who.health_=0;} }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
return EXIT_SUCCESS;
}
Ending that if statement that produces a mismatched set of brackets.
Aug 11, 2009 at 2:24am UTC
To keep the game open,
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage; else { who.isDead = true ; who.health_=0;} }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
return EXIT_SUCCESS;
}
Aug 14, 2009 at 3:47am UTC
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage;
else { who.isDead = true ; who.health_=0;} }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
return EXIT_SUCCESS;
}
Being the code maid here.
Aug 14, 2009 at 4:30pm UTC
My way to keep things open for a while
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage;
else { who.isDead = true ; who.health_=0;} }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
bool open=true ;
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
return EXIT_SUCCESS;
}
Aug 20, 2009 at 4:44pm UTC
added else in main...thinking one could attack the other depending on rNum %2 == 0.
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage;
else { who.isDead = true ; who.health_=0;} }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
bool open=true ;
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
else {
return EXIT_SUCCESS;
}
Aug 23, 2009 at 3:54am UTC
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage;
else
who.isDead = true ; who.health_=0; }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
bool open=true ;
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
else {
return EXIT_SUCCESS;
}
1 2
22c22,23
< else { who.isDead = true ; who.health_=0;} }
> else
> who.isDead = true; who.health_=0; }
Last edited on Aug 28, 2009 at 10:12am UTC
Aug 23, 2009 at 6:43am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main(int argc, char *argv[])
{
bool open=true ;
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
else {
} while (open); //<-- added this
return EXIT_SUCCESS;
}
Aug 23, 2009 at 6:59am UTC
I have a feeling someone's going to be very happy when this is finished.
If only it was still the 80's, you could compile a .COM executable and sell it on a floppy!
When it is finished, I'll compile & link it and test it.
Aug 23, 2009 at 5:00pm UTC
Can anyone tell me where the variable naming convention of having an underscore at the end comes from?
Aug 23, 2009 at 10:24pm UTC
Can anyone tell me where the variable naming convention of having an underscore at the end comes from?
I use them for private members. I don't know when and why I started using them.
Aug 23, 2009 at 11:18pm UTC
I've seen them other places as well...
Aug 24, 2009 at 3:01am UTC
I put them at the beginning of the name, but really, it doesn't matter as long as it's consistent.
Aug 24, 2009 at 11:55am UTC
How about we leave the player and weapon class now and start initialising SDL so we will have something to show!
I've only changed #include "SDL.h" to #include <SDL.h> so we can store it seperatly in a neat SDL lib map! (when compiling you'll have to add a path variable to your compiler/ide)
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
#include <iostream>
#include <ctime>
#include <cmath>
#include <SDL.h> // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage;
else
who.isDead = true ; who.health_=0; }
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
bool open=true ;
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
else {
} while (open); //<-- added this
return EXIT_SUCCESS;
}
1 2 3 4 5 6 7 8 9
class weapon
{
private :
std::string name;
int damage, range;
public :
explicit weapon(std::string m_name = "sword" , int m_damage = 5 int range = 2) : name (m_name), damage (m_damage), range (range) {}
};
Last edited on Aug 24, 2009 at 11:59am UTC
Aug 28, 2009 at 10:10am UTC
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
#include <iostream>
#include <ctime>
#include <cmath>
#include <SDL.h> // Access to 2d graphics API
#include <vector>
#include <algorithm>
#include "weapon.hpp"
class Player {
public :
explicit Player ( unsigned initial_health = 100, const std::vector<weapon>& weapons = std::vector<weapon>() )
: health_ ( initial_health ), isDead ( false ), max_health ( initial_health ), weaponry (weapons) { }
void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }
void quickAttack( const weapon &weap, Player &who)
{
if ( who.health_ > weap.damage )
who.health_ -= weap.damage;
else
who.isDead = true ; who.health_=0;
}
void quickAttack( const weapon &weap, Player &who, float criticalityChance)
void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon ); }
private :
std::vector<weapon> weaponry;
unsigned health_;
unsigned max_health;
bool isDead;
bool isTurn;
};
int main(int argc, char *argv[])
{
bool open=true ;
Player Havoc(75, std::vector<weapon>("pistol" ));
Player Thug(50, std::vector<weapon>("Knife" ));
srand( time(0) );
int i;
do {
int rNum = rand() % 10 + 1;
if (rNum % 2 == 0){
}
else {
} while (open); //<-- added this
return EXIT_SUCCESS;
}
1 2
23c23,24
< who.isDead = true ; who.health_=0; }
> who.isDead = true; who.health_=0;
> }
Topic archived. No new replies allowed.