Noob learns to code in 3 months

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob learns to code in 3 months

Post by albinopapa » September 7th, 2017, 2:36 pm

Code: Select all

Gun lol(x, y, z, blah, blah, blah);
Weapon* pLol = &lol;
roadHog.AddWeapon(pLol);
You wouldn't need to make the pLol variable though, you could just pass the address of lol

Code: Select all

Gun lol(x, y, z, blah, blah, blah);
roadHog.AddWeapon(&lol);
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 7th, 2017, 2:45 pm

^ Wtf?? I could've sworn I tried that and it didn't work.... oh well it does work :D My brain is starting to get fried I guess

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 7th, 2017, 2:47 pm

Best part of the program for those of you who doesn't have the time to read through it all

Code: Select all

bool TauntPole::Attack(const std::string& targetName, const std::string & heroName)
{
	if (cooldownTimer.limitReached())
	{
		std::cout << heroName << " used his " << name << " and it displayed this message to " << targetName << ":" << std::endl;
		std::uniform_int_distribution<int> dist(1, 10);
		const int msg = dist(rng);

		switch (msg)
		{
		case 1:
			std::cout << "\"Life isn't easy, but your mom is.\"\n\n";
			break;
		case 2:
			std::cout << "\"I would insult your mother, but cows are sacred in my country.\"\n\n";
			break;
		case 3:
			std::cout << "\"I bet your mother has a loud bark.\"\n\n";
			break;
		case 4:
			std::cout << "\"Your family tree must be a cactus because you're all pricks.\"\n\n";
			break;
		case 5:
			std::cout << "\"How were you born? Your dad has no balls!\"\n\n";
			break;
		case 6:
			std::cout << "\"You're so poor, when you asked your Mom whats for dinner she opened her legs and said crabs.\"\n\n";
			break;
		case 7:
			std::cout << "\"You mother is so old she went to an antiques auction and people bid on her.\"\n\n";
			break;
		case 8:
			std::cout << "\"Ever since I saw you in your family tree, I've wanted to cut it down.\"\n\n";
			break;
		case 9:
			std::cout << "\"Are your parents siblings?\"\n\n";
			break;
		case 10:
			std::cout << "\"Did you know they used to be called \"Jumpolines\" until your mom jumped on one?\"\n\n";
			break;
		}

		return true;
	}

	return false;
}

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob learns to code in 3 months

Post by albinopapa » September 7th, 2017, 3:38 pm

Yeah, I loved it. You should make a text file full of those, and have them load into a vector of strings. Then you just get a random number between 0 and vector size - 1. You'd avoid the switch statements and can add to the file whenever something comes to mind or find something on internet and little to nothing in code would need to change.

EDIT: Even if this project isn't going anywhere, you'd still get some file I/O practice in.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 7th, 2017, 4:59 pm

great idea!
Been a minute since I did any file I/O so could use the practice

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 7th, 2017, 5:46 pm

mmmkay this should work :) Think I'm done with this assignment now. Will see if I can get some feedback on it soon.

Just gonna add some more insults to the text file lol

Code: Select all

TauntPole::TauntPole(float cooldown, const std::string& ownerName_in)
	:
	Weapon(cooldown, ownerName_in),
	rng(rd())
{
	std::ifstream in("insults.txt");
	std::string str;

	if (in.is_open())
	{
		while (std::getline(in, str))
		{
			insults.push_back(str);
		}
		in.close();
	}
}

Code: Select all

bool TauntPole::Attack(const std::string& targetName, const std::string& heroName)
{
	if (cooldownTimer.limitReached())
	{
		std::cout << heroName << " used his " << name << " and it displayed this message to " << targetName << ":" << std::endl;

		std::uniform_int_distribution<int> dist(0, (insults.size() - 1));

		const int msg = dist(rng);

		std::cout << insults[msg] << "\n\n";
	
		return true;
	}

	return false;
}

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 7th, 2017, 9:08 pm

Quick question.

There's no point doing stuff like:
CharacterMcCree* mcCree = new CharacterMcCree(mcCreeName, 200);

Right? I see examples like int* p = new int; all the time but struggle to see the use when it's not a dynamic array or something massive that can't be stored on the stack.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 7th, 2017, 10:13 pm

nvm! chili answered it in tut 5

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » September 8th, 2017, 1:10 am

Yumtard wrote:Quick question.

There's no point doing stuff like:
CharacterMcCree* mcCree = new CharacterMcCree(mcCreeName, 200);

Right? I see examples like int* p = new int; all the time but struggle to see the use when it's not a dynamic array or something massive that can't be stored on the stack.
It makes me happy to hear these words ^^ Bonus points for having this doubt even before watching I5!

By the way, if you will never be creating a character class, you don't need to (really shouldn't in fact) create implementations of the virtual functions in there (unless you need some default behavior).

So for Attack, you can just do virtual void Attack(...) = 0;

Also, like papa mentioned, in your child classes, do [virtual] void Attack(...) override {...}
Chili

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » September 8th, 2017, 1:14 am

Oh, I should mention one more thing. We usually create on the heap if we want to use polymorphism to put a bunch of different (but related) objects in the same container.

std::vector<Character*> charptrs;

charptrs.push_back( new Mcree( ... ) );
charptrs.push_back( new Datboi( ... ) );
charptrs.push_back( new DonaldTrump( ... ) );

for( auto pc : charptrs )
{
pc->Update(...);
}

These days you'd wanna use unique_ptr though...
Chili

Post Reply