Question About Vectors

Page 1 of 1

5 Replies - 491 Views - Last Post: 02 January 2013 - 04:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=305400&amp;s=e389e4da9ca6ffd26ca779430b3ae33a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 luckielordie

• New D.I.C Head

Reputation: 0
• 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

• Suitor #2

Reputation: 11597
• Posts: 45,760
• 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

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Lover

Reputation: 1
• 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

• New D.I.C Head

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

Re: Question About Vectors

Posted 02 January 2013 - 04:25 PM

ButchDean, 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

• D.I.C Lover

Reputation: 1
• 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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}