views:

289

answers:

6

Hi,

As part of a homework assignment, I have to program a simple chess game in Java. I was thinking of taking the opportunity to experiment with recursion, and I was wondering if there's an obvious candidate in chess for recursive code?

Thank you,

JDelage

A: 

Not chess, but a classic puzzle with chess figueres: http://en.wikipedia.org/wiki/Eight_queens_puzzle

PeterMmm
+5  A: 

The most obvious candidate to me would be a recursive minimax routine for searching for the best moves. This also gets into a lot of the theory behind search algorithms and would be pretty cool to implement.

Example:

http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/6/

Laplace
I even think, there's no alternative to the recursive minmax (if the idea is to develop a KI)
Andreas_D
Also useful is this link explaining alpha-beta http://www.fierz.ch/strategy1.htm
Martin Smith
Wow, this is a great article. It seems that this is a method that would be used differently at different stages. Maybe there would be one version for mate, and one version for some other goal (e.g., capture a piece), each with different depth. Hmmm... Fun.
JDelage
A: 

You are thinking about backtracking

mcabral
+1  A: 

Depth-first search is a prime candidate for recursion. so if you're programming an AI for the homework assignment, then the AI's lookhead algorithm to try to figure out a best next move would be a good candidate.

Be careful though - you can run out of memory quick. You probably want to limit the number of moves deep the AI can look.

sechastain
+2  A: 

Yes there is. If you have some function that evaluate "force" of some position for say player white. You can move a piece and call it recursively to evaluate the value of a move and choose the best move.

You should call the same function for player black, exchanging roles for blacks and whites, thus evaluating "danger" of an opponent move.

Then again for the whites, etc.

Just be aware you should not go too deep in recursion levels or it will take forever.

kriss
Thanks. I just need to find a good logic for the value of each move.
JDelage
+1  A: 

Mind dynamic programming, as you have multiple combinations which lead to the same board, you should remember to cache the moves in order to avoid repeating calculations

If you detect a recursion just lead you to a place where you have been, just break that call. This is known as backtracking

fmsf