1 Replies - 143 Views - Last Post: 04 December 2017 - 01:01 PM Rate Topic: -----

#1 sgaffigan121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-December 17

Code will read in from file but monsters won't attack each other..

Posted 04 December 2017 - 11:57 AM

My code is basically complete and it compiles and reads in the text file but im not sure how to make the monsters fight each other...

The strongest monsters in the world have entered into a world tournament to find out who is the best monster. The tournament is in a 1-on-1 “shuffled bracket” format, where every round in the tournament, the list of monsters is shuffled, and each undefeated monster faces off against another undefeated monster (possibly with 1 monster being given a “bye” if there’s an odd number of monsters).
Tournament Example
For example, suppose there’s 5 monsters at the beginning of the tournament (monsters A,B,C,D,E).
For the first round, the monsters are randomly shuffled: B, D, A, C, E
• Monster B fights monster D
• Monster A fights monster C
• Monster E has a “bye” and fights nobody
Now assume that monster D wins, and monster A wins, thus monsters D, A, and E continue to the second round of the tournament.
For the second round, the monsters are randomly shuffled: E D A
• Monster E fights monster D
• Monster A has a “bye” and fights nobody.
Now assume that monster E wins, thus monsters E and A continue to the final round of the tournament.
In the final round of the tournament, assume that monster A wins, thus monster A wins the tournament.

Monster Capabilities
Each monster is of type “Rock”, “Paper”, or “Scissors”.
Each monster enters the tournament with a certain amount of hit-points.
• A rock monster has properties for “mass” and “velocity”. When a rock monster attacks, it deals damage with the following formula: damage = mass * velocity * velocity
• A paper monster has properties for “length” and “width”. When a paper monster attacks, it deals damage with the following formula: damage = length * width
• A scissors monster has a property for “sharpness”. When a scissors monster attacks, it deals damage with the following formula: damage = sharpness
Battle Format
Monsters will attack each other sequentially.
Each battle continues until one of the monsters has 0 or fewer hit-points.
It is arbitrary which monster attacks first.
For example…
• Suppose monster A is a paper monster with 100 hit-points, length: 10, and width: 5.
• Suppose monster B is a rock monster with 90 hit-points, mass: 5, and velocity: 3.
• Monster A attacks first, dealing 10*5 = 50 damage to monster B.
o Monster B now has 90 – 50 = 40 hit-points.
• Monster B now attacks monster A, dealing 5*3*3 = 45 damage.
o Monster A now has 100 – 45 = 55 hit-points.
• Monster A now attacks monster B, again dealing 50 damage.
o Mosnter B now has 40-50 = -10 hit-points, so monster B dies and monster A has won the battle.
• Monster A continues to the next round of the tournament.
At the end of the battle, a monster’s hit-points is NOT reset. In the example above, monster A would enter the next round of the tournament with 55 hit-points.
Implementation Details
You should read in the monsters and their properties from the supplied “P6Input.txt”. Note that there are an odd number of monsters in the text file.
Your program must use polymorphism to store all monsters in one data structure (you may use STL libraries such as vector).
Your base “Monster” class should be abstract. It should have pure virtual functions for “string getMonsterType()”, which returns the monster’s type as a string, and “bool attack(Monster * target)” which uses the monster’s damage formula to deal damage to the target monster and returns “true” or “false” depending on whether or not the target monster has died.

The input file is in the following format:
For each line:
• First is the type of monster: Rock, Paper, or Scissor.
• Next is the monster’s name.
• Next is the monster’s hit-points.
• Next depends on the type of monster:
o If Rock: mass, followed by velocity
o If Paper: length, followed by width
o If Scissors: sharpness

#ifndef MONSTER_H
#define MONSTER_H
#include <string>
#include <iostream>
using namespace std;
class Monster {
public:
	Monster();
	string name;
	int target;
	virtual string getMonsterType()=0;
	virtual bool attack(Monster* target)=0;
};
#endif

#include "Monster.h"

Monster::Monster()
{
}

#ifndef ROCKMONSTER_H
#define ROCKMONSTER_H
#include "Monster.h"
#include <iostream>
#include <string>
using namespace std;

class RockMonster:public Monster{
public:
int mass;
int velocity;
RockMonster(string name, int mass, int velocity, int target);
string getMonsterType();
bool attack(Monster* target);
};
#endif 

#include "RockMonster.h"
#include <iostream>
#include <string>
using namespace std;

RockMonster::RockMonster(string name, int mass, int velocity, int target)

{

this->mass = mass;

this->velocity = velocity;

}

string RockMonster::getMonsterType()

{

return name;

}

bool RockMonster::attack(Monster * target)

{

if (target->target >= mass*velocity*velocity)

return true;

else

return false;

}

#ifndef PAPERMONSTER_H
#define PAPERMONSTER_H
#include "Monster.h"
#include <iostream>
#include <string>
using namespace std; 
class PaperMonster : public Monster
{
public:
int length;
int width;
PaperMonster(string name, int length, int width, int target);
string getMonsterType();
bool attack(Monster* target);
};
#endif

#include "PaperMonster.h"
#include <string>
#include <iostream>
using namespace std;

PaperMonster::PaperMonster(string name, int length, int width, int target)

{

	this->length = length;

	this->width = width;

}

string PaperMonster::getMonsterType()

{

	return name;

}

bool PaperMonster::attack(Monster * target){

	if (target->target >= length*width)

		return true;

	else

		return false;

}

#ifndef SCISSORSMONSTER_H
#define SCISSORSMONSTER_H
#include "Monster.h"
class ScissorsMonster : public Monster
{
public:
int sharpness;
ScissorsMonster(string name, int sharpness, int target);
string getMonsterType();
bool attack(Monster* target);
};
#endif

#include "ScissorsMonster.h"
#include <string>
#include <iostream>
using namespace std;
ScissorsMonster::ScissorsMonster(string name, int sharpness, int target)

{

this->sharpness = sharpness;

}

string ScissorsMonster::getMonsterType()

{

return name;

}

bool ScissorsMonster::attack(Monster * target)

{

if (target->target >= sharpness)

return true;

else

return false;

}

#include "Monster.h"
#include "RockMonster.h"
#include "PaperMonster.h"
#include "ScissorsMonster.h"
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

//tokenizer

vector<string> split(const string &s, char delim) {

	stringstream ss(s);

	string item;

	vector<string> tokens;

	while (getline(ss, item, delim)) {

		tokens.push_back(item);

	}

	return tokens;

}

int main()

{

	vector<Monster*> monsters;

	vector<int> targets;

	string line;

	ifstream myfile("P6Input.txt");

	if (myfile.is_open())

	{

		while (getline(myfile, line))

		{

			cout << line << '\n';

			vector<string> res = split(line,' ');

			string name;

			if (res[0] == "Rock")

			{

				name = res[1];

				int target = atoi(res[2].c_str());

				int mass = atoi(res[3].c_str());

				int velocity = atoi(res[4].c_str());

				targets.push_back(target);

				Monster *r = new RockMonster(name, mass, velocity,target);

				monsters.push_back(r);

			}

			else if (res[0] == "Paper")

			{

				name = res[1];

				int target = atoi(res[2].c_str());

				int length = atoi(res[3].c_str());

				int width = atoi(res[4].c_str());

				targets.push_back(target);

				Monster *r = new PaperMonster(name, length, width,target);

				monsters.push_back(r);

			}

			else if (res[0] == "Scissor")

			{

				name = res[1];

				int target = atoi(res[2].c_str());

				int sharpness = atoi(res[3].c_str());

				targets.push_back(target);

				Monster *r = new ScissorsMonster(name, sharpness,target);

				monsters.push_back(r);

			}

		}

		myfile.close();

	}

	else{

		cout << "Unable to open file";
	}

	int i = 0;
	for (auto it = monsters.begin(); it != monsters.end(); ++it){
		cout <<"HERE"<<endl;
		cout << "Monster Type: " << (*it)->getMonsterType() << endl;
		if ((*it)->attack(*it)){

			cout << " Monster is dead" << endl;
		}	
		else{

			cout << " Monster is still alive" << endl;
		}
	}

	//system("pause");

	return 0;

}
]
:code:

Text File:
Paper Noah 104 10 9
Rock Liam 102 7 2
Rock Mason 107 9 2
Scissor Jacob 112 7
Rock William 80 2 1
Scissor Ethan 81 8
Scissor James 86 5
Rock Alexander 107 9 1
Rock Michael 80 7 2
Paper Benjamin 80 7 8
Paper Elijah 84 6 2
Paper Daniel 107 1 4
Paper Aiden 95 1 7
Paper Logan 91 4 6
Paper Matthew 90 8 4
Rock Lucas 110 2 4
Scissor Jackson 92 10
Rock David 114 5 4
Paper Oliver 93 9 5
Scissor Jayden 82 9
Scissor Joseph 100 1
Rock Gabriel 105 10 3
Rock Samuel 92 5 4
Rock Carter 107 8 3
Paper Anthony 107 6 2
Rock John 92 8 2
Scissor Dylan 117 10
Rock Luke 112 10 4
Rock Henry 110 2 3
Scissor Andrew 112 3
Scissor Isaac 101 3
Rock Christopher 108 6 3
Paper Joshua 106 10 6
Paper Wyatt 104 7 1
Scissor Sebastian 85 7
Rock Owen 95 4 4
Rock Caleb 89 8 3
Paper Nathan 114 8 9
Scissor Ryan 80 6
Paper Jack 104 6 7
Scissor Hunter 93 8
Rock Levi 88 7 4
Rock Christian 97 5 3
Scissor Jaxon 87 4
Rock Julian 109 3 1
Rock Landon 82 3 1
Scissor Grayson 112 1
Paper Jonathan 111 1 8
Paper Isaiah 88 1 2
Rock Charles 106 8 2
Paper Thomas 91 6 5
Paper Aaron 92 7 6
Paper Eli 93 7 6
Scissor Connor 97 5
Scissor Jeremiah 98 1
Paper Cameron 114 4 3
Paper Josiah 119 10 4
Scissor Adrian 113 6
Paper Colton 82 9 7
Paper Jordan 85 8 10
Paper Brayden 85 3 5
Paper Nicholas 107 9 4
Rock Robert 101 7 4
Rock Angel 101 5 4
Rock Hudson 93 3 4
Scissor Lincoln 111 3
Paper Evan 88 8 1
Rock Dominic 114 3 3
Scissor Austin 101 1
Scissor Gavin 93 2
Scissor Nolan 89 8
Rock Parker 82 1 1
Paper Adam 119 1 2
Rock Chase 90 9 3
Scissor Jace 83 9
Rock Ian 98 3 4
Paper Cooper 114 7 10
Scissor Easton 88 9
Scissor Kevin 101 8
Rock Jose 106 6 3
Scissor Tyler 113 2
Rock Brandon 119 6 4
Rock Asher 88 9 3
Scissor Jaxson 83 4
Paper Mateo 118 3 10
Scissor Jason 97 4
Scissor Ayden 98 3
Paper Zachary 92 1 6
Rock Carson 110 8 1
Rock Xavier 115 1 3
Paper Leo 94 4 2
Paper Ezra 97 3 10
Rock Bentley 100 2 4
Scissor Sawyer 112 6
Scissor Kayden 108 1
Paper Blake 105 5 4
Rock Nathaniel 108 10 4
Scissor Ryder 114 9
Scissor Theodore 92 4

This post has been edited by modi123_1: 04 December 2017 - 12:01 PM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Code will read in from file but monsters won't attack each other..

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1408
  • View blog
  • Posts: 4,464
  • Joined: 20-March 10

Re: Code will read in from file but monsters won't attack each other..

Posted 04 December 2017 - 01:01 PM

You select two monsters randomly from the vector of monsters. call them monsterOne, monsterTwo

You decide which goes first randomly monsterOne or MonsterTwo.
update hit points decide if Monster has died ie hit points <=0.

carry on battle until one Monster dies.
remove dead Monster from vector of monsters.

loop on a while condition until there is one Monster left.

declare that Monster the winner.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1