Code: Select all
' fischerandom.bi
' Generate start positions of Fischer Random Chess
' https://www.dwheeler.com/essays/Fischer_Random_Chess.html
declare function StartPosition overload(a as integer, b as integer, c as integer, d as integer, e as integer) as string
declare function StartPosition(i as integer) as string
declare function StartPosition() as string
function StartPosition overload(a as integer, b as integer, c as integer, d as integer, e as integer) as string
if (a < 1) or (a > 4) _
or (b < 1) or (b > 4) _
or (c < 1) or (c > 6) _
or (d < 1) or (d > 5) _
or (e < 1) or (e > 4) then
return ""
end if
dim result as string = String(8, ".")
dim i as integer
i = 2 * a - 2
result[i] = Asc("B")
i = 2 * b - 1
result[i] = Asc("B")
i = 0
while result[i] <> Asc(".")
i += 1
wend
while c > 1
i += 1
while result[i] <> Asc(".")
i += 1
wend
c -= 1
wend
result[i] = Asc("Q")
i = 0
while result[i] <> Asc(".")
i += 1
wend
while d > 1
i += 1
while result[i] <> Asc(".")
i += 1
wend
d -= 1
wend
result[i] = Asc("N")
i = 0
while result[i] <> Asc(".")
i += 1
wend
while e > 1
i += 1
while result[i] <> Asc(".")
i += 1
wend
e -= 1
wend
result[i] = Asc("N")
i = 0
while result[i] <> Asc(".")
i += 1
wend
result[i] = Asc("R")
while result[i] <> Asc(".")
i += 1
wend
result[i] = Asc("K")
while result[i] <> Asc(".")
i += 1
wend
result[i] = Asc("R")
return LCase(result) & "/pppppppp/8/8/8/8/PPPPPPPP/" & result & " w KQkq - 0 1"
end function
function StartPosition(i as integer) as string
if (i < 1) or (i > 1920) then
return ""
end if
dim as integer a, b, c, d, e, f
f = i - 1
a = (f \ 480) + 1
f mod= 480
b = (f \ 120) + 1
f mod= 120
c = (f \ 20) + 1
f mod= 20
d = (f \ 4) + 1
f mod= 4
e = f + 1
return StartPosition(a, b, c, d, e)
end function
function StartPosition() as string
return StartPosition( _
Int(Rnd * 4) + 1, _
Int(Rnd * 4) + 1, _
Int(Rnd * 6) + 1, _
Int(Rnd * 5) + 1, _
Int(Rnd * 4) + 1 _
)
end function
Code: Select all
' demo.bas
#include "fischerandom.bi"
Randomize Timer
? StartPosition(2, 3, 3, 2, 3)
? StartPosition(767)
? StartPosition()