Subscribe to Metaphorically Speaking        RSS Feed
-----

Quickie

Icon Leave Comment
Ok so I have not been in contact with my partner today in the code but have
decided to start a little bit on it just to get some options. So today I created a
class that will hold player resources and allow you to add or subtract from
them. Basically in any turn based strategy where you build units or buildings you
have to gain resources so I created a class to manipulate the info easy and it allows
me to use the same code for the Human Player as well as the computer player.

below is the header file to my class.

//PlayerResources.h
//class derived for the control of a players resources be it human or cpu

//Includes


//BEGIN
class playerResources   //the abstract class
{
public:
	//constructors
	playerResources();
	playerResources(int wood, int stone, int metal, int gold);
	
	//Accessors
	int showWood();
	int showStone();
	int showMetal();
	int showGold();

	//Modifiers
	void addWood(int);
	void addStone(int);
	void addMetal(int);
	void addGold(int);

	void removeWood(int);
	void removeStone(int);
	void removeMetal(int);
	void removeGold(int);

private:
	int sWood;
	int sStone;
	int sMetal;
	int sGold;



};



Now I show you the underlying .cpp that helps run this class.

//main code for playerResources class

//Includes
#include "playerResources.h"

playerResources::playerResources()
{
	sWood = 0;
	sStone = 0;
	sMetal = 0;
	sGold = 0;

}

playerResources::playerResources(int wood, int stone, int metal, int gold)
{
	sWood = wood;
	sStone = stone;
	sMetal = metal;
	sGold = gold;

}

int playerResources::showWood()
{
	return sWood;
}

int playerResources::showStone()
{
	return sStone;
}

int playerResources::showMetal()
{
	return sMetal;
}

int playerResources::showGold()
{
	return sGold;
}

void playerResources::addWood(int amount)
{
	sWood += amount;
}

void playerResources::addStone(int amount)
{
	sStone += amount;
}

void playerResources::addMetal(int amount)
{
	sMetal += amount;
}

void playerResources::addGold(int amount)
{
	sGold += amount;
}

void playerResources::removeWood(int amount)
{
	sWood -= amount;
}

void playerResources::removeStone(int amount)
{
	sStone -= amount;
}

void playerResources::removeMetal(int amount)
{
	sMetal -= amount;
}

void playerResources::removeGold(int amount)
{
	sGold -= amount;
}



Really simple and easy to use with a lot of room for additions. One thing I will be
adding is a limiter on the amount of resources you can add. That way you can't end
up with 1,000,000 resources there should be a limit.

Finally, I am going to show you the test .cpp that I created to test this code.

#include <iostream>
#include "playerResources.h"

using namespace std;

int main()
{
	playerResources firstPlayer(100, 75, 62, 100);

	cout << "Wood: " << firstPlayer.showWood() << endl;
	cout << "Stone: " << firstPlayer.showStone() << endl;
	cout << "Metal: " << firstPlayer.showMetal() << endl;
	cout << "Gold: " << firstPlayer.showGold() << endl;

	firstPlayer.addWood(20);
	firstPlayer.addStone(5);
	firstPlayer.addMetal(7);
	firstPlayer.addGold(200);

	cout << endl << endl;

	cout << "Wood + 20: " << firstPlayer.showWood() << endl;
	cout << "Stone + 5: " << firstPlayer.showStone() << endl;
	cout << "Metal + 7: " << firstPlayer.showMetal() << endl;
	cout << "Gold + 200: " << firstPlayer.showGold() << endl << endl << endl;

	firstPlayer.removeWood(25);
	firstPlayer.removeStone(10);
	firstPlayer.removeMetal(14);
	firstPlayer.removeGold(300);

	cout << "Wood - 25: " << firstPlayer.showWood() << endl;
	cout << "Stone - 10: " << firstPlayer.showStone() << endl;
	cout << "Metal - 14: " << firstPlayer.showMetal() << endl;
	cout << "Gold - 300: " << firstPlayer.showGold() << endl << endl << endl;

	return 0;
}



Oh yah we show add and subtract. w00t it works. B) Now to show you what the output
is as a good visual and also as proof that it works.
Posted Image

Well there we go my first go at my new project let me know what you think.

0 Comments On This Entry

 

Recent Entries

Recent Comments

Search My Blog

January 2022

S M T W T F S
      1
2345678
9101112131415
1617181920 21 22
23242526272829
3031     

Files Used In Blog

3 user(s) viewing

3 Guests
0 member(s)
0 anonymous member(s)