5 Replies - 309 Views - Last Post: 02 January 2013 - 04:36 PM Rate Topic: -----

#1 luckielordie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 18-October 11

Question About Vectors

Posted 02 January 2013 - 03:28 PM

Hi all,

The following line gives me a "Debug Assertion Failed!" error with "Expression: Vector subscript out of range". This means that I must be using my vector incorrectly. However I cannot use breakpoints and step through my code to find out the problem due to the way the project has been set, but I have managed to narrow it down to a certain method and a couple of lines.

int Minimax::SearchMethod(Node* node, int depth)
{
	node->GenerateChildren();
	vector<Node*> children = node->GetChildren();

	if(depth == 0 || !node->HasChildren())
	{
		return node->GetBoard()->getBoardValue();
	}

	int value = 0;

	if(node->IsPlayerOneTurn())
	{
		value = -100000;	

		for (int i = 0; i < children.size(); i++)
		{
			value = max(value, SearchMethod(children[i], depth - 1));
		}
	} 
	else
	{
		value = 100000;

		for (int i = 0; i < children.size(); i++)
		{
			value = min(value, SearchMethod(children[i], depth - 1));
		}
	}

	return value;
}



The second line of the method is the line that's throwing the error.
I cant find out why, I've tried initializing the children vector before passing "GetChildren()" but it still doesn't like it.

The method GetChildren() returns a "vector<Node*>" so that is also not the problem.

Any Ideas ?

Is This A Good Question/Topic? 0
  • +

Replies To: Question About Vectors

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9218
  • View blog
  • Posts: 34,612
  • Joined: 12-June 08

Re: Question About Vectors

Posted 02 January 2013 - 03:33 PM

Quote

However I cannot use breakpoints and step through my code to find out the problem due to the way the project has been set

Would you explain this? I am curious on what you did to set up your project to prevent debugging.
Was This Post Helpful? 1
  • +
  • -

#3 luckielordie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 18-October 11

Re: Question About Vectors

Posted 02 January 2013 - 04:05 PM

We're building a Bot that can play Tron.
We were given some .JAR files comprising of the main game and some bots to play against.
We are only writing a C++ application that can play Tron using the minimax algorithm. The problem with trying to step through it is that the program tries to make a call to the .JAR file *I think* and cannot get a response so the whole thing is stopped at the start.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question About Vectors

Posted 02 January 2013 - 04:17 PM

vector<Node*> children = node->GetChildren();



What is this code supposed to be doing? It looks wrong.
Was This Post Helpful? 0
  • +
  • -

#5 luckielordie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 18-October 11

Re: Question About Vectors

Posted 02 January 2013 - 04:25 PM

View PostButchDean, on 02 January 2013 - 04:17 PM, said:

vector<Node*> children = node->GetChildren();



What is this code supposed to be doing? It looks wrong.



It was creating a new Vector and copying the contents into it to initialize it. This is a bad practice though I take it?

Either way I changed the way I address the array in Node this is how my Minimax Class looks.

//This class holds the standard minimax algorithm
int Minimax::SearchMethod(Node* node, int depth)
{
	node->GenerateChildren();

	if(depth == 0 || !node->HasChildren())
	{
		return node->GetBoardValue();
	}

	int value = 0;

	if(node->IsPlayerOneTurn())
	{
		value = -100000;	

		for (int i = 0; i < node->GetNumberOfChildren(); i++)
		{
			value = max(value, SearchMethod(node->GetChild(i), depth - 1));
		}
	} 
	else
	{
		value = 100000;

		for (int i = 0; i < node->GetNumberOfChildren(); i++)
		{
			value = min(value, SearchMethod(node->GetChild(i), depth - 1));
		}
	}

	return value;
}



I use "GetChild" and "GetNumberOfChildren" to get the information I need now instead of getting the whole vector.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question About Vectors

Posted 02 January 2013 - 04:36 PM

Yes, it is bad practice. That is not how you copy to a vector.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1