Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  AI search and Java  (Read 318 times)
0 Members and 1 Guest are viewing this topic.
Offline cryo75

Junior Newbie





« Posted 2013-03-17 07:59:47 »

Hi all,

I'm writing a board game and currently implementing the AI using the standard Negamax. This algorithm calls itself recursively.

To speed up things I implemented a makemove/undomove within the function and I don't pass the board object as a parameter to itself. However, due to the nature of Java not passing objects by reference, the changes to the board state when going deeper into the recursion are also reflected into the upper levels.

When I just copy the board state every time, the function works fine because each recursion works on a new copy based on a copy of one level above it. In this case I do have to pass the board state copy as a parameter and this copy copied immediately.

This has serious issues because as I hit a depth of 6 the search slows down considerably. So I'd rather have the move/undo functionality to speed up things.

So my question is: Is it possible in Java, and if yes, how? Or am I restricted to copying the board each and every time with serious performance issues?

Thanks,
C
Offline Jono

Senior Member


Medals: 1
Projects: 1



« Reply #1 - Posted 2013-03-17 09:29:24 »

Quote
To speed up things I implemented a makemove/undomove within the function
This will work fine, so long as you are doing depth-first search (which you are, because of the recursive calls).

Quote
due to the nature of Java not passing objects by reference
Typo? You mean Java always passes by reference. So no new copy of an object is made by default.

Quote
and I don't pass the board object as a parameter to itself
This is a little confusing. Does it mean that the search function is within the board class itself? Or that a single static board object exists?

Regardless, I'd have player objects decide their own moves and have any search functions in their classes. These methods should pass a board around as a parameter.

All you need to do is put your move and undo-move before and after each recursive call, as in (pseudocode):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
double search(Board board, int depth)
  if( depth=0 )
    return heuristic(board)
  else
     maxValue = 0
     for each move in children(board)
       move.makemove(board)
       maxValue = max(maxValue,search(board,depth-1)
       move.undomove(board)
     return maxValue
Offline cryo75

Junior Newbie





« Reply #2 - Posted 2013-03-17 14:15:30 »

Your pseudocode snippet is similar to my function. I'm passing the board as one of the parameters and make/unmake the same way.

My make function does:
1. place new piece on the board
2. increment new pieces for current player
3. switch players (opponent is now the current player)

My unmake function does:
1. remove new piece from the board
2. decrement new pieces for current player
3. switch players

However, after a few plies into the search the number of new pieces for each player are way off (eg. 22, -18, etc)

So what could be wrong?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jono

Senior Member


Medals: 1
Projects: 1



« Reply #3 - Posted 2013-03-17 15:32:53 »

What you're doing sounds right to me so it's probably just a bug.

Are there any other side effects on the board from adding a piece, like removing opponents pieces, that the undo might get wrong? Is the move being used for move/undo being modified at all during the search?
Offline cryo75

Junior Newbie





« Reply #4 - Posted 2013-03-17 16:19:49 »

It's only the piece counters that aren't updated property because the same board is used in the recursion. Placing/undoing a cell position works because it gets the information from the move itself, which btw... doesn't change between and make/unmake.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (34 views)
2013-05-17 21:29:12

alaslipknot (43 views)
2013-05-16 21:24:48

gouessej (72 views)
2013-05-16 00:53:38

gouessej (72 views)
2013-05-16 00:17:58

theagentd (80 views)
2013-05-15 15:01:13

theagentd (74 views)
2013-05-15 15:00:54

StreetDoggy (116 views)
2013-05-14 15:56:26

kutucuk (140 views)
2013-05-12 17:10:36

kutucuk (140 views)
2013-05-12 15:36:09

UnluckyDevil (150 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.079 seconds with 21 queries.