c++ word game

Pages: 1234
Jul 28, 2009 at 11:39pm
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 Jul 28, 2009 at 11:46pm
Jul 28, 2009 at 11:56pm
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:
};
Jul 29, 2009 at 12:10am
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 Jul 29, 2009 at 12:11am
Jul 29, 2009 at 1:25am
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
Jul 29, 2009 at 2:38am
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.
Jul 29, 2009 at 2:42am
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?
Jul 29, 2009 at 8:56am
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 Jul 29, 2009 at 8:56am
Jul 30, 2009 at 4:34pm
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);
};
Jul 31, 2009 at 1:45am
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.
Jul 31, 2009 at 1:56am
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.
Aug 3, 2009 at 10:46pm
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.
Aug 3, 2009 at 11:39pm
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
Aug 5, 2009 at 6:06am
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;
}
Aug 6, 2009 at 1:15am
50 health? I think 10 is enough for a first enemy. I won't change it unless someone agrees.
Aug 6, 2009 at 2:36am
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...
Aug 6, 2009 at 3:28am
This will end up as a new WoW, or as a remake of old Bard's Tale!

Aug 6, 2009 at 3:33am
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;
}
Aug 6, 2009 at 4:40am
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;
}
Aug 8, 2009 at 7:27pm
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
Aug 8, 2009 at 9:19pm
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 Aug 8, 2009 at 9:20pm
Pages: 1234