RosettaCodeData/Task/Knights-tour/FreeBASIC/knights-tour.basic
2023-07-01 13:44:08 -04:00

57 lines
1.4 KiB
Text

Dim Shared As Integer tamano, xc, yc, nm
Dim As Integer f, qm, nmov, n = 0
Dim As String posini
Cls : Color 11
Input "Tamaño tablero: ", tamano
Input "Posicion inicial: ", posini
Dim As Integer x = Asc(Mid(posini,1,1))-96
Dim As Integer y = Val(Mid(posini,2,1))
Dim Shared As Integer tablero(tamano,tamano), dx(8), dy(8)
For f = 1 To 8 : Read dx(f), dy(f) : Next f
Data 2,1,1,2,-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1
Sub FindMoves()
Dim As Integer i, xt, yt
If xc < 1 Or yc < 1 Or xc > tamano Or yc > tamano Then nm = 1000: Return
If tablero(xc,yc) Then nm = 2000: Return
nm = 0
For i = 1 To 8
xt = xc+dx(i)
yt = yc+dy(i)
If xt < 1 Or yt < 1 Or xt > tamano Or yt > tamano Then 'Salta este movimiento
Elseif tablero(xt,yt) Then 'Salta este movimiento
Else
nm += 1
End If
Next i
End Sub
Color 4, 7 'Pinta tablero
For f = 1 To tamano
Locate 15-tamano, 3*f: Print " "; Chr(96+f); " ";
Locate 17-f, 3*(tamano+1)+1: Print Using "##"; f;
Next f
Color 15, 0
Do
n += 1
tablero(x,y) = n
Locate 17-y, 3*x: Print Using "###"; n;
If n = tamano*tamano Then Exit Do
nmov = 100
For f = 1 To 8
xc = x+dx(f)
yc = y+dy(f)
FindMoves()
If nm < nmov Then nmov = nm: qm = f
Next f
x = x+dx(qm)
y = y+dy(qm)
Sleep 1
Loop
Color 14 : Locate Csrlin+tamano, 1
Print " Pulsa cualquier tecla para finalizar..."
Sleep
End