Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Presa Canario
Feb 15, 2008
Forum member since 37308 A.D.
I'm trying to write an AI game search tree with a depth-first search in java. I already have a recursive version that works, but this seems like it would be a lot faster. The problem is it needs to iterate somehow so that I can stop it at a maximum depth, and so that I can do min/max at certain levels. I tried doing this:

code:
depth = 5;
s = new stack();
leaves = new queue();
State current = (current state);
s.push(current);
while(s not empty)
{
    current = s.pop();
    depth--;
    nextMoves = (possible moves from current)
    for(move in nextMoves)
    {
         State new = transitionState(current, move);
         if (depth==0)
         {
              leaves.push(new);
              depth++;
         }
         else s.push(new);

    }
}
...but depth never goes back up once it hits the bottom two levels. It there a better way or should I stick with recursive?

Adbot
ADBOT LOVES YOU

Presa Canario
Feb 15, 2008
Forum member since 37308 A.D.
Ah thanks adding a depth variable to the State class worked. Now each state starts with depth=0, then the transition() method adds one to it each time.

csammis posted:

It doesn't look like you're ever popping from leaves, is that correct?
Well the idea was that it would store all the leaves in a queue and then go back later looking for the best one.

Presa Canario fucked around with this message at 19:59 on Mar 28, 2008

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply