c++ word game

Pages: 1234
Weapon doesn't need to access player, but player needs to access weapon.

See the attack. "weap.damage" is a private member.
Thus, I add friendness.

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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>

#include "weapon.hpp"

class Player {
    friend class weapon;
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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>(pistol));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {        
    
    return EXIT_SUCCESS;
}


1
2
3
4
5
6
class weapon
{
    private:
        std::string name;
        int damage;
};

Last edited on
right now the do-while loop is useless so i'll kill it.
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>

#include "weapon.hpp"

class Player {
    friend class weapon;
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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>(pistol));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to     
    
    return EXIT_SUCCESS;
}


and we'll need a constructor but first
1
2
3
4
5
6
7
8
class weapon
{
    private:
        std::string name;
        int damage;

    public:
};
If rNum is even we can do something with it.
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>

#include "weapon.hpp"

class Player {
    friend class weapon;
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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>("pistol"));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    if(rNum % 2 == 0){
    // Do-while loop to     
    
    return EXIT_SUCCESS;
}


Aaaaaand...constructah!

1
2
3
4
5
6
7
8
9
class weapon
{
    private:
        std::string name;
        int damage;

    public:
    explicit weapon(std::string m_name = "sword", int m_damage = 5) : name (m_name), damage (m_damage);
};
Last edited on
And now to kill the comment related to the do-while loop
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>

#include "weapon.hpp"

class Player {
    friend class weapon;
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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>("pistol"));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    if(rNum % 2 == 0){
    // Do-while loop to     
    
    return EXIT_SUCCESS;
}


and an overloaded constructor.
1
2
3
4
5
6
7
8
9
10
class weapon
{
    private:
        std::string name;
        int damage;

    public:
    explicit weapon(std::string m_name = "sword", int m_damage = 5) : name (m_name), damage (m_damage);
    weapon(char* wepname, int wepdamage){name=wepname; damage=wepdamage;}
};


i do not know the usage of explicit as of now but will look into it
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
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>

#include "weapon.hpp"

class Player {
    friend class weapon;
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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>("pistol"));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    if(rNum % 2 == 0){
    
    return EXIT_SUCCESS;
}


Removed now-unrelated comment.
I thought that if class A needed access to class B, class B would need to have friend class A thats what it does http://www.cplusplus.com/doc/tutorial/inheritance/ ? or am I missing something?
Removed the constructor overload as a char* can be passed to the std::string constructor
1
2
3
4
5
6
7
8
9
class weapon
{
    private:
        std::string name;
        int damage;

    public:
        explicit weapon(std::string m_name = "sword", int m_damage = 5) : name (m_name), damage (m_damage);
};
Last edited on
Accidentally put the friend statement in the wrong class. >_>

So, removing it from Player and adding it to weapon.
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
#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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>("pistol"));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    if(rNum % 2 == 0){
    
    return EXIT_SUCCESS;
}

class weapon
{
friend class Player;
private:
std::string name;
int damage;

public:
explicit weapon(std::string m_name = "sword", int m_damage = 5) : name (m_name), damage (m_damage);
};
1
2
3
4
5
6
7
8
9
10
class weapon
{
friend class Player;
private:
std::string name;
unsigned int num_dice, unsigned int num_sides;

public:
explicit weapon(std::string m_name = "sword", int m_damage = 5) : name (m_name), damage (m_damage);
};


Changed damage on weapons to make to be XdX.
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
#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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>("pistol"));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    if(rNum % 2 == 0){
    
    return EXIT_SUCCESS;
}

Attempting to clean up the functions. Single-line functions are horrid. Hopefully some people will help me, since I can only change one line at a time.
1
2
3
4
5
6
7
8
9
10
class weapon
{
friend class Player;
private:
std::string name;
unsigned int num_dice, unsigned int num_sides;

public:
explicit weapon(std::string m_name = "sword", int m_sides = 6, int m_dice = 2) : name (m_name), num_sides (m_sides), num_dice (m_dice);
};


Changed the constructor to reflect damage changes.
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
#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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75, std::vector<weapon>("pistol"));
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    if(rNum % 2 == 0){
    
    return EXIT_SUCCESS;
}


Added an enter for the function
Lets give Havoc someone to kill...
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
#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; }

        void addWeapon ( const weapon &new_weapon ) { weaponry.push_back( new_weapon );  }

    private:
        std::vector<weapon> weaponry;
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

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;
}
50 health? I think 10 is enough for a first enemy. I won't change it unless someone agrees.
That was just a random value, i find that it is best just choose a value until the game is playable then modify the values to adjust difficulty, but that is just the way i do it...
This will end up as a new WoW, or as a remake of old Bard's Tale!

I think it should be turn based so
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
#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; }

        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;
}
I'd like to see this introduce some LUCK factor to it.

Adding a chance to critically hit an opponent by overloading quickAttack.

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
#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; }
        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;
}
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
#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;
}

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) : name (m_name), damage (m_damage);
};

If you're dead, you have no health left.
Also, added range so that guns and swords aren't equal
Added 'range' and missing braces to the constructor
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
Pages: 1234