help with rpg system

im still new to c++.
love it but gosh i get so frustrated sometimes and want to give up!
but i haven't yet.
im trying to work on a small rpg for my first big project.
self/friend taught.
i had a good working system and lost it due to
c drive delete accident :'(

im new to classes and im trying to get my basic
name/hp down.
would like to have an enemy class but im not understanding inheritance.

anyways here is my base code.
my problem is getting the enemy to attack and revive damage from the attack.
goblins hp never goes down but mikeys does.


any help will be greatly appreciated :)

plus any tips on inheritance in this program will be appreciated


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
55
56
57
58
59
60
61
  
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>

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

class player
{
private:
	//player name
	string playername;
	int health;

public:
	//player info
	
	int money;

	player(int hp, const string & name)
	{
		health = hp;
		playername = name;
		money = 100;
	}
	//player stats
	void stats(std::ostream & out)
	{
		out << "name:   " << playername << " |  hp:  " << health << "  |   money:  " << money <<  endl;
	}
	//attack system
	int attack(player & enemy)
	{
		int damage = rand()%20+1;
		health -= damage;
		return health;
	}
};

int main()
{
	//random seed
	srand(unsigned (time(0)));
	//creating player and enemy
	player mikey(100,"mikey");
	player enemy(100, "goblin");
	mikey.attack(enemy);
	enemy.stats(cout);
	enemy.attack(mikey);
	mikey.stats(cout);
	
	return 0;
}

If the only difference is: "goblins hp never goes down but mikeys does", inheritance is probably not required.

Something like this, perhaps:

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 <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>

struct player
{
    player( std::string name, int hp = 100, bool goblin = false )
        : name(name), health(hp), is_goblin(goblin) {}

    std::string name ;
    int health ;
    int money = 100 ;
    bool is_goblin ; // true if this is a goblin

    void attack( player& enemy )
    {
        // goblins hp never goes down but mikeys does
        if( !is_goblin ) health -= ( std::rand()%20 + 1 ) ;
        if( !enemy.is_goblin ) enemy.health -= ( std::rand()%20 + 1 ) ;
        // note: ideally, use facilities in <random>
        //       http://en.cppreference.com/w/cpp/numeric/random
    }

    std::ostream& stats( std::ostream& out = std::cout ) const
    {
        return out << std::setw(10) << name << " {  hp:  "
                   << std::setw(3) << health << "  |   money:  " << money
                   << "   " << ( is_goblin ? " } (goblin)" : " }" ) ;
    }

    friend std::ostream& operator<< ( std::ostream& out, const player& p )
    { return p.stats(out) ; }
};

int main()
{
    std::srand( std::time(nullptr) ) ;

    player mikey( "mikey", 100 );
    player enemy( "goblin", 100, true ); // true => goblin

    std::cout << mikey << '\n' << enemy << "\n\n" ;

    mikey.attack(enemy);
    std::cout << mikey << '\n' << enemy << "\n\n" ;

    enemy.attack(mikey);
    std::cout << mikey << '\n' << enemy << "\n\n" ;
}

http://coliru.stacked-crooked.com/a/06f256e530815278
Last edited on
Version using inheritance:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>

struct player
{
    player( std::string name, int hp = 100 ) : name(name), health(hp) {}

    virtual ~player() = default ; // http://www.stroustrup.com/C++11FAQ.html#default
    // TO DO ...
    // see class base_of_five_defaults http://en.cppreference.com/w/cpp/language/rule_of_three

    void attack( player& enemy )
    {
        do_damage() ;
        enemy.do_damage() ;
    }

    std::ostream& stats( std::ostream& out = std::cout ) const { return do_stats(out) ; }

    protected:
        std::string name ;
        int health ;
        int money = 100 ;

        virtual std::ostream& do_stats( std::ostream& out ) const
        {
            return out << std::setw(10) << name << " {  hp:  " << std::setw(3)
                       << health << "  |   money:  " << money << " }" ;
        }

        // default implementation of damage to health
        virtual void do_damage() { health -= ( std::rand()%20 + 1 ) ; }

    friend std::ostream& operator<< ( std::ostream& out, const player& p )
    { return p.stats(out) ; }
};

struct goblin : player
{
    using player::player ; // http://www.stroustrup.com/C++11FAQ.html#inheriting

    protected:

        // http://www.stroustrup.com/C++11FAQ.html#override
        virtual std::ostream& do_stats( std::ostream& out ) const override
        { return player::do_stats(out) << " (goblin)" ; } // *** important: do not call player::stats()

        // goblins hp never goes down
        virtual void do_damage() override {} // so do nothing
};

int main()
{
    std::srand( std::time(nullptr) ) ;

    player mikey( "mikey", 100 );
    goblin enemy( "goblin", 100 );

    std::cout << mikey << '\n' << enemy << "\n\n" ;

    mikey.attack(enemy);
    std::cout << mikey << '\n' << enemy << "\n\n" ;

    enemy.attack(mikey);
    std::cout << mikey << '\n' << enemy << "\n\n" ;
}

http://coliru.stacked-crooked.com/a/d8ce235bb12c9614
thanks for the reply!!!
here is what i did.
i kept my class and added a few things.

main.cpp

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include "Fighter.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

void rules();


int main()
{
    std::cout << "ello world this worked " << std::endl;
    Fighter mikey("mikey", 100, 200);
	Fighter enemy("witch", 100, 200);
	int bet;

	rules();

	do

	{
		std::cout << "\n";
		enemy.roll();
		std::cout << "\nplace a bet   $";
		std::cin >> bet;
		int enemybet = rand()%15+1;

		mikey.cheatcheck(bet);


		if (mikey.roll() == enemy.roll())
		{
			mikey.win(bet, enemybet);
			enemy.lose(enemybet);
			mikey.Attack(enemy);
			std::cout << "\nyou won $" << bet + enemybet << " and attacked!!!!!" <<std::endl;
		}else
		{
			std::cout << "\nthe enemy won $" << enemybet + bet << " and attacked!!\n";
			enemy.Attack(mikey);
			enemy.win(enemybet, bet);
			mikey.lose(bet);

		}

		mikey.Display(std::cout);
		enemy.Display(std::cout);

		
	}while (enemy.IsAlive() && mikey.IsAlive() && mikey.Isnotbroke() && enemy.Isnotbroke());

	if (!mikey.IsAlive())
	{
		std::cout << "\n\n\nyou died!!!!!\ngame over!\n";
		return 0;
	}

	if (!enemy.IsAlive())
	{
		std::cout << "\n\n\nyou won!!!!\nthe enemy died!\n";
		return 0;
	}

	if(!mikey.Isnotbroke())
	{
		std::cout << "your broke!!!!\ngame over!\n";
		return 0;
	}

	if(!enemy.Isnotbroke())
	{
		std::cout << "you win!!\nthe enemy is broke!\n";
		return 0;
	}


	return 0;
}


void rules()
{
	std::cout << "---------------------------------------------------\n";
	std::cout << "| welcome to my game, just roll the same number    |\n";
	std::cout << "| as the enemy to attack, if not you get attacked  |\n";
	std::cout << "| have fun.  will add more later   :)              |\n";
	std::cout << "|--------------------------------------------------|\n";
	std::cout << "\n\nlets roll a dice and see if we can attack!!!" << std::endl;
}



Fighter.h

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
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
class Fighter
{
private:
	std::string fname;
	int fhealth;
	int fcoins;




public:
	Fighter(const std::string, int hp, int money);
	
	void	ReceiveDamage( int nAttackDamage );

//if fighter is still alive
//Attack other
	void Attack( Fighter & other );


//Display Fighter HP
	void	Display( std::ostream & out );

	bool IsAlive();

	bool Isnotbroke();

	int roll();

	int lose(int bet);
	
	int win(int bet, int enbet);

	int cheatcheck(int bet);
};



Fighter.cpp

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "stdafx.h"
#include "Fighter.h"


 Fighter::Fighter(const std::string name, int hp, int money)
{

	fname = name;
	fhealth = hp;
	fcoins = money;
}

void	Fighter::ReceiveDamage( int nAttackDamage )
{
	fhealth = fhealth - nAttackDamage;
}
//if fighter is still alive
bool	Fighter::IsAlive()
{
return fhealth > 0;
}

bool Fighter::Isnotbroke()
{
	return fcoins > 0;
}
//Attack other
void Fighter::Attack( Fighter & other )
{
int nDamage = rand()%10+1;
other.ReceiveDamage( nDamage );
}

//Display Fighter HP
void	Fighter::Display( std::ostream & out )
{
out << fname << "'s " << "hitpoints: " << fhealth << "  |  coins:  " << fcoins << std::endl;
}


int Fighter::roll()
{
	int sides = rand()%6+1;
	std::cout << "\n" << fname << " rolled a:  " << sides;
	return sides;
}

int Fighter::lose(int bet)
{
	fcoins -= bet;
	return fcoins;
}

int Fighter::win(int bet, int enbet)
{
	fcoins += bet + enbet;
	return fcoins;
}

int Fighter::cheatcheck(int bet)
{
	if (bet > fcoins || bet < 0)
	{
		std::cout << "\ncheater!!!!!";
		fhealth = 0;
		exit(1);
		
	}
}




visual studios 2012 pro edition
Last edited on
Strongly consider upgrading to the current version of the compiler.

Download Visual Studio Community 2017 from here: https://www.visualstudio.com/downloads/
In the installer, choose 'Desktop development with C++'
only 16 hours of download time left yay
Topic archived. No new replies allowed.