a routine for a chess game

martin_vista

Active member
Joined
Mar 26, 2007
Messages
29
Programming Experience
3-5
Hi,

I have a serious brain cracker. Honour to everyone that takes the time to understand the logic!
I am writing a routine for a chess game. The goal is to have the knight find the shortest way to a target. I managed to write the code, but it turned out to be a lot of code, and I am sure the code can be replaced by a rather small recursive function. I myself think recursive functions are complicated, but this one really beats me. The following code is a function that calculates the needed steps for the knight. First it tries to reach its goal in one step. if that fails, it tries to reach it in two steps, etc. The code:

If bOk = FalseThen
iLevel = 1
For i = 0 To 7
If bOk = TrueThenExitFor
x_target2 = x_target + arrayscope(i, 0)
y_target2 = y_target + arrayscope(i, 1)
bOk = makemove(x_target2, y_target2)
Next i
EndIf

If bOk = FalseThen
iLevel = 2
For i = 0 To 7
If bOk = TrueThenExitFor
x_target2 = x_target + arrayscope(i, 0)
y_target2 = y_target + arrayscope(i, 1)
bOk = makemove(x_target2, y_target2)
For i2 = 0 To 7
If bOk = TrueThenExitFor
x_target3 = x_target2 + arrayscope(i2, 0)
y_target3 = y_target2 + arrayscope(i2, 1)
bOk = makemove(x_target3, y_target3)
Next i2
Next i
EndIf

If bOk = FalseThen
iLevel = 3
For i = 0 To 7
If bOk = TrueThenExitFor
x_target2 = x_target + arrayscope(i, 0)
y_target2 = y_target + arrayscope(i, 1)
bOk = makemove(x_target2, y_target2)
For i2 = 0 To 7
If bOk = TrueThenExitFor
x_target3 = x_target2 + arrayscope(i2, 0)
y_target3 = y_target2 + arrayscope(i2, 1)
bOk = makemove(x_target3, y_target3)
For i3 = 0 To 7
If bOk = TrueThenExitFor
x_target4 = x_target3 + arrayscope(i3, 0)
y_target4 = y_target3 + arrayscope(i3, 1)
bOk = makemove(x_target4, y_target4)
Next i3
Next i2
Next i
EndIf

and so on ...and so on... for every "deeper" step the same routine is used, extended with one more for-next loop
For the logic it is not important, but for the info: arrayscope() contains the possible steps of the knight (leftleftup, upupleft, etc.)the function makemove checks if making the move brings the knight to the target.

I myself came up with the following recursive function:

Function checkKnightStep(ByVal paramX AsInteger, ByVal paramY AsInteger) AsBoolean
Dim i AsInteger = 0
Dim bOk AsBoolean = False
Dim paramXtemp AsInteger = paramX
Dim paramYTemp AsInteger = paramY
For i = 0 To 7
paramXtemp = paramX + arrayscope(i, 0)
paramY = paramY + arrayscope(i, 1)
bOk = makemove(paramX, paramY)
If bOk = TrueThen
ReturnTrue
Else
Return checkKnightStep(paramX, paramY)
EndIf
Next i
EndFunction

This doesn't work because the function should do:
First it should look if the knight is one step away from the target. If not it should look if the knight is two steps from the target, if not it should look if the knight is three steps from the target, etc. etc.

But the recursive function I wrote starts at the other end:
it looks if the knight is unlimited steps away from the target, if not it looks if the target is unlimited-1 steps from the target (... but that won't work!), etc. etc.

Well, this has kept me awake for a few nights now. I understand it's a lot of code, and it is very complex, but maybe there's some genius out there that knows how to handle this. If so, you'll get my ever lasting respect!:D
 
Back
Top