Hello all,

here is my first attempt at object oriented programming in cpp, a goofy duel game.

Below my first implementation.

What I am stuck with is this:

There can be potentially many weapon functions, each taking a different number of arguments. The output is always an integer (damage).

Since a player has two arms only, and I would like to assign a weapon function to each arm at initialization, effectively creating a load out. It should be possibly to leave an arm empty, say, because rifle is a two-handed weapon and assigning rifle to one arm should leave the other unused (default output zero).

So I would then like to iterate over both arms to get damage output from each one (unlike now, where the weapon function is not related to the player). What I am aiming at something like this

Code:
p1.health = p1.health - p2.leftarm(p2.skill, duelrange) - p2.rightarm(p2.skill, duelrange);
p2.health = p2.health - p1.leftarm(p1.skill, duelrange) - p1.rightarm(p1.skill, duelrange);
If a function is not initialized then I should return a zero integer.

I tried to defines a struct that inherits a player and overloads the two functions with. Did not really work (but I guess I will be able to debug it). But it bothers me that another struct is needed simply to insert two functions at initialization?

Can you please explain how to best code this?

Code:
#include <iostream>
#include <string>
#include <time.h>

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

struct player
{
	string name;
	int health;
	int skill;
	// virtual void lefthand() = 0;
	// virtual void righthand() = 0;
};

int pistol(int accuracy, int range) {
	int dmg = 0;
	int shortrange = 50;

	if (range <= shortrange) accuracy = accuracy + 1;

	int d6 = rand() % 6 + 1; // roll d6
	if (d6 >= accuracy) dmg = 15; // hit

	return dmg;
}

int rifle(int accuracy, int range) {
	int dmg = 0;

	int d6 = rand() % 6 + 1; // roll d6
	if (d6 >= accuracy) dmg = 25; // hit

	return dmg;
}

void print(player a, player b)
{
	cout << a.name << "(" << a.health << ") vs " << b.name << "(" << b.health << ")" << endl;
}

int main()
{
	srand((unsigned int) time(NULL));
	player p1{"Joe", 50, 3};
	player p2{"Jim", 50, 3};
	print(p1, p2);

	int duelrange = 100;

	while (p1.health > 0 and p2.health > 0)
	{
		p1.health = p1.health - rifle(p2.skill, duelrange);
		p2.health = p2.health - pistol(p1.skill, duelrange);
		print(p1, p2);
	}

	if (p1.health <= 0)
	{
		cout << p2.name << " kills " << p1.name << endl;
	}
	else {
		cout << p1.name << " kills " << p2.name << endl;
	}

	cin.get();
	return 0;
}