Noob learns to code in 3 months

The Partridge Family were neither partridges nor a family. Discuss.
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 14th, 2017, 10:32 pm

superconfused...

just as a test. I did this:

void BattleManager::freeMemory()
{
m_Players[0]->freeMemory();
m_Players[1]->freeMemory();
delete m_Players[0];
delete m_Players[1];
delete m_Moves["Tackle"];
}

and then I call it from main and get no crash.

then I paste this code in the destructor and call the destructor from main and get crash

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 14th, 2017, 11:00 pm

Yumtard wrote: and call the destructor from main and get crash
maybe this was the issue. idk seems to be working when not calling it

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 15th, 2017, 1:36 am

You should (almost) never ever call DTOR directly bro. I remember telling you that somewhere earlier in this thread as well (when you tackled the homework for I6) :lol:
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 15th, 2017, 1:39 am

Yumtard wrote:trying to delete all dynamically allocated objects.

Why does this not work for deleting the objects in my map?

Code: Select all

for (; it != m_Moves.end(); it++)
	{     
		if (it != m_Moves.end())
		{
			delete it->second;
			it->second = nullptr;
			std::cout << "Destroying a move..." << std::endl;
		}
	}
"map/set iterator not dereferencable"
need to see full code there bro. also, you may have other issues, but first gotta deal with your iterator issue.
Chili

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 15th, 2017, 8:41 am

Alright, this has been the biggest mess of my programming life :D

I just couldn't get it to work...
I had major issues with dumb logic, like states getting switched from everywhere.
I also couldn't get the program working the way I wanted it to.

After much time coding and many headaches I decided to rewrite/restructure almost everything.
When I did that the logic became a bit less confusing and I got the program working the way I wanted it to.
also I'm now reading in from files instead of doing it all in code.

Code: Select all

BattleManager::BattleManager()
{
	{
		std::ifstream in("damaging.txt");
		int dmg;
		std::string name;

		while (in.good())
		{
			in >> name >> dmg;
			m_Moves[name] = new DamagingMove(name, dmg);
		}
	}

	{
		std::ifstream in("absorbing.txt");
		int dmg;
		std::string name;

		while (in.good())
		{
			in >> name >> dmg;
			m_Moves[name] = new AbsorbingMove(name, dmg);
		}
	}

	{
		m_Player = new Player("Ash");
		std::ifstream in("player1pokemons.txt");
		int hp;
		std::string name, att1, att2, att3, att4;

		while (in.good())
		{
			in >> name >> att1 >> att2 >> att3 >> att4 >> hp;
			m_Pokemons[name] = new Pokemon(name, hp);
			m_Pokemons[name]->AddMove(att1);
			m_Pokemons[name]->AddMove(att2);
			m_Pokemons[name]->AddMove(att3);
			m_Pokemons[name]->AddMove(att4);
			m_Player->AddPokemon(m_Pokemons[name]);
		}
	}

	{
		m_AI = new Player("Gary");
		std::ifstream in("player2pokemons.txt");
		int hp;
		std::string name, att1, att2, att3, att4;

		while (in.good())
		{
			in >> name >> att1 >> att2 >> att3 >> att4 >> hp;
			m_Pokemons[name] = new Pokemon(name, hp);
			m_Pokemons[name]->AddMove(att1);
			m_Pokemons[name]->AddMove(att2);
			m_Pokemons[name]->AddMove(att3);
			m_Pokemons[name]->AddMove(att4);
			m_AI->AddPokemon(m_Pokemons[name]);
		}
	}
}
I'm pretty sure this could've been done in a better way but at least it's better than it was before.
I'm also pretty sure the code still sucks ass. There's some stuff I wanted to do different and I think there's a bunch of stuff I'm unaware of, like hardcoding, bad logic, bad practices, bad structure etc
but I've been coding for probably 20 hours and atm just can't think of any ways to improve it further (and I'm supposed to hand it in, in 2 hours fml).

Would be very happy if anyone would be kind enough to let me know how one would structure this better.

https://github.com/Yumtard/pokemonBattle/tree/different

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 15th, 2017, 2:04 pm

well, apparently this would've passed

Code: Select all

#include <string>

struct Pokemon
{
	int hp;
	std::string name;
};

struct Player
{
	Pokemon* pkmn;
};

int main()
{
	Player* ash = new Player;
	Player* gary = new Player;
	ash->pkmn = new Pokemon;
	gary->pkmn = new Pokemon;
	ash->pkmn->name = "Pikachu";
	gary->pkmn->name = "charizard";
	ash->pkmn->hp = 30;
	gary->pkmn->hp = 100;

	while (ash->pkmn->hp > 0 && gary->pkmn->hp > 0)
	{
		gary->pkmn->hp -= 4;
		ash->pkmn->hp -= 7;
	}
	delete ash->pkmn;
	delete gary->pkmn;
	delete ash;
	delete gary;

	return 0;
}
meanwhile I spammed 800-900 lines of codes yesterday

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 15th, 2017, 3:27 pm

Hahaha, well it's good to know what level they're expecting :D

No need to overthink things I guess, but it's good to mess around with stuff and come up with a lot of failed designs. That's how you really start building a feel for what works and what doesn't.
Chili

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 15th, 2017, 4:25 pm

chili wrote:Hahaha, well it's good to know what level they're expecting :D

No need to overthink things I guess, but it's good to mess around with stuff and come up with a lot of failed designs. That's how you really start building a feel for what works and what doesn't.
Well they're hoping for more than this ofc .
But he showed this as like the bare minimum for what would qualify as a simulation and mentioned that he would've passed it.
He expected all of us to have overthinked the assignment and probably to have attempted doing something we weren't really able to. He seemed to be correct :)

He also mentioned that it might've been a good idea to start out like this and then build upon it.
More students seemed to have had the same issues as me with trying to do eeverything at once

It felt nice to know they're not expecting us to be masters at programming. because during this assignment I started feeling like they're probably expecting more of me than I'm capable of.

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 17th, 2017, 5:41 am

Yumtard wrote:He also mentioned that it might've been a good idea to start out like this and then build upon it.
Solid advice IMO.
Chili

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 19th, 2017, 1:02 pm

Being a github tartd

https://github.com/planetchili/3D_Funda ... e/t3-rot3d

I'm watching t3 of 3d fundamentals. Cloned this repo but I only get te master branch, not the t3 rot3d branch

Post Reply