c++ word game

Pages: 1234
Lets start to build an arsenal of attacks :)

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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }
        
        void heal( unsigned cure ) {
        int quickAttack( unsigned damage) {
        
    private:
        unsigned health_;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to    
    do {
    
    return EXIT_SUCCESS;
}
Let's not leave that many incomplete lines

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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }

        void heal( unsigned cure ) { health_ += cure; }

        int quickAttack( unsigned damage) {

    private:
        unsigned health_;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}
The real meaning of an 'inlined' function...
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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }

        void heal( unsigned cure ) { health_ += cure; }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; } 

    private:
        unsigned health_;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}
Adding SDL header :). Anyone else familiar with SDL? If not we can remove 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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }

        void heal( unsigned cure ) { health_ += cure; }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; } 

    private:
        unsigned health_;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

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

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }

        void heal( unsigned cure ) { health_ += cure; }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; } 

    private:
        unsigned health_;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}


Included std::vector, so that we can add a weapons load to Havoc.
Added in a max health variable, so that players can't abuse the heal thing.

Can I edit it up to add all that?

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

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }

        void heal( unsigned cure ) { health_ += cure; }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; } 

    private:
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}
Somebody should include <algorithm> :S
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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }

        void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; }

    private:
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}
Going to set max_health to the same thing as health is at declaration.

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

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false ), max_health ( initial_health )  { }

        void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; }

    private:
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

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

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false ), max_health ( initial_health )  { }

        void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; }

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

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}


Added a weapons vector, also we are probably going to have to start creating separate files soon.
Just a minor change. Also, is there any rule about chopping up these long lines into more readable ones? If that's ok then I gonna do it as my next step.
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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false ), max_health ( initial_health )  { }

        void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; }

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

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}
I can't wait until this becomes a complete game... I will play it. And it will be fun.

It will be fun.

If it's boring I'll kill you all, but no pressure.
Last edited on
Including <algorithm>

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

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false )  { }

        void heal( unsigned cure ) { health_ = std::min( health_ + cure, max_health); }

        int quickAttack( unsigned damage, Player &who) { if ( who.health_ > damage ) who.health_ -= damage; else who.isDead = true; }

    private:
        unsigned health_;
        unsigned max_health;
        bool isDead;
};

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}
Modified quickAttack to work with a 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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
#include "SDL.h" // Access to 2d graphics API
#include <vector>
#include <algorithm>

class Player {
    public:
        explicit Player ( unsigned initial_health = 100 ) : health_ ( initial_health ), isDead ( false ), max_health ( initial_health )  { }

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

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

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}
Last edited on
starting a weapons file
1
2
3
4
class 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
#include <cstdlib>
#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 ) : health_ ( initial_health ), isDead ( false ), max_health ( initial_health )  { }

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

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

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}


1
2
3
4
class weapon
{

};


edit: changed include '<>' to "
Last edited on
I say one modification per file. How's that work?


Alright, not experienced with vectors. Is the addition to the constructor valid?
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
#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, std::vector<weapon> weapons ) : 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; }

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

int main(int argc, char *argv[])
{
    Player Havoc(75);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {

    return EXIT_SUCCESS;
}

1
2
3
4
class weapon
{
    std::string name;
};
Made a little correction for the constructor
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
#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; }

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

int main(int argc, char *argv[])
{
    Player Havoc(75);
    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;
};
Last edited on
Added a function to give a new weapon to the player
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);
    srand( time(0) );
    int i;
    int rNum = rand() % 10 + 1;
    // Do-while loop to
    do {        
    
    return EXIT_SUCCESS;
}


1
2
3
4
5
class weapon
{
    private:
        std::string name;
};
Last edited on
1
2
3
4
5
6
class weapon
{
        friend class Player;
    private:
        std::string name;
};


made Player a friend class of weapon, so it can access the private variables.

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;
    // Do-while loop to
    do {        
    
    return EXIT_SUCCESS;
}


updated the call for Player Havoc to include a weapon.
1
2
3
4
5
6
class weapon
{

    private:
        std::string name;
};


Removed friendship, because there is no point for weapon to access Player >_>
Pages: 1234