Combat simulator game problem II

And for once its not an error question. Right now i've run into a problem. As you can see, when the "runcombat()" is started, i initialize 2 characters, a player and a monster. Then I assign them each values for their stats, then I use functions to get each characters speed and compare them, determining whom will go first.

From there, eventually the player will get to pick an action, and for shits and giggles lets say the player picks an "attack action." here is my problem, i need to have a function or block of code to let the player pick which character he wants to attack (because eventually the combat will get more complicated.) but i have no idea how to get "monster.bigger" down to "makeAttack()." I looked into references and pointers but those seemed to be dead ends. I've also heard of global scope and that I should avoid it. What do you guys think?


Combat.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
#include <iostream>
#include "Combat.h"
#include "Player.h"
#include "Monster.h"

using namespace std;



Combat::Combat()
{
}


bool Combat::runCombat()
{
	Player kyle;
	Monster bigger;
	

	kyle.init(1, 10, 10, 3, 5, 6, 15, 12, 500, 1000);
	bigger.init(1, 10, 10, 2, 5, 6, 15, 12, 500, 1000);
	
	int speed1 = kyle.accessSpeed();
	int speed2 = bigger.accessSpeed();

	int turn = checkSpeed(speed1, speed2);

	if (turn == 1) {
		kyle.pickAction();
	} 
	else {
		//bigger.pickAction();
	}


	char turn = 'a';

}

int Combat::checkSpeed(int crea1, int crea2)
{
	if (crea1 > crea2) {
		return 1;
	}
	else {
		return 2;
	}
}


Player.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
#include "Player.h"
#include <iostream>

using namespace std;


Player::Player()
{
}

void Player::init(int level, int health, int defense, int speed, int damage, int attack, int stamina, int mana, int gold, int experience)
{
	_level = level;
	_health = health;
	_defense = defense;
	_speed = speed;
	_damage = damage;
	_attack = attack;
	_stamina = stamina;
	_mana = mana;
	_gold = gold;
	_experience = experience;
}

int Player::accessSpeed()
{
	return _speed;
}

void Player::pickAction()
{
	int actionChoice;

	cout << "please pick an action!" << endl;
	cout << "1). Attack!" << endl;
	cout << "2). Cast Spell!" << endl;

	cin >> actionChoice;


	if (actionChoice == 1) {
		makeAttack();
	}
	else if (actionChoice == 2) {
		//Spell function
	}
	else {
		//Need to make this loop for invalid choice.
		cout << "You cannot do that now!" << endl;
	}

}

int Player::pickTarget()
{
	
}

void Player::makeAttack()
{
	//Pick Weapon
	int wDamage = _damage;
	int wAttack = _attack;

	//Pick target
	

	//apply damage

	//end action

}


Monster.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
#include "Monster.h"



Monster::Monster()
{
}


void Monster::init(int level, int health, int defense, int speed, int damage, int attack, int stamina, int mana, int armor, int magicResist)
{
	_level = level;
	_health = health;
	_defense = defense;
	_speed = speed;
	_damage = damage;
	_attack = attack;
	_stamina = stamina;
	_mana = mana;
	_armor = armor;
	_magicResist = magicResist;
}

int Monster::accessSpeed()
{
	return _speed;
}
bump?
Provide a name/description for each monster. Put each monster in an array and let the user decide wich monster to attack according to the index and the description. Do it similar with the spells.

By the way.
Since monster and player have so much in common: Create a common base class and inherit both. Let in the inherited class only appear the difference.
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 <vector>
#include <string>

struct target // base class for different kinds of targets
{
    explicit target ( std::string its_name ) : name( std::move(its_name) ) {}
    std::string name;
    virtual ~target() = default ;
};

struct monster : target 
{
    explicit monster( std::string name ) : target( std::move(name) ) {}
    // ...
};

struct vampire : target
{
    explicit vampire( std::string name ) : target( std::move(name) ) {}
    // ...
};

struct player
{
    target* pick_target( const std::vector<target*>& available_targets ) /* may be const */
    {
        // TODO: pick an appropriate target from among available_targets
        // in this trivialised example, we always pick the first target
        return available_targets.empty() ? nullptr : available_targets.front() ;
    }

    void make_attack( const std::vector<target*>& available_targets )
    {
        // ...
        target* the_target = pick_target( available_targets ) ;
        if( the_target != nullptr ) std::cout << "picked target '" << the_target->name << "'\n" ;
        // ...
    }
};

int main()
{
    vampire orlok( "orlok" );
    monster bigger( "bigger" );
    monster tiny( "tiny" );

    player kyle ;
    kyle.make_attack( { std::addressof(bigger), std::addressof(orlok), std::addressof(tiny) } );
    kyle.make_attack( { std::addressof(orlok), std::addressof(bigger), std::addressof(tiny) } );
}

http://coliru.stacked-crooked.com/a/d2e6ecc1f1025814
Topic archived. No new replies allowed.