-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess960.lua
More file actions
61 lines (50 loc) · 1 KB
/
chess960.lua
File metadata and controls
61 lines (50 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local chess960 = {}
local n5n = {
{1,2},
{1,3},
{1,4},
{1,5},
{2,3},
{2,4},
{2,5},
{3,4},
{3,5},
{4,5}
}
local function nth_nil(board, n)
for i = 1, 8 do
if board[i] == nil then
n = n - 1
end
if n <= 0 then
return i
end
end
end
function chess960.start_position(n)
local board = {nil, nil, nil, nil, nil, nil, nil, nil}
local board_str = ''
local n2 = n >> 2
local b1 = n % 4
local n3 = n2 >> 2
local b2 = n2 % 4
local n4 = n3 // 6
local q = n3 % 6
local ns = n5n[n4 + 1]
board[b1 * 2 + 2] = 'B'
board[b2 * 2 + 1] = 'B'
board[nth_nil(board, q + 1)] = 'Q'
board[nth_nil(board, ns[1])] = 'N'
board[nth_nil(board, ns[2 - 1])] = 'N'
board[nth_nil(board, 1)] = 'R'
board[nth_nil(board, 1)] = 'K'
board[nth_nil(board, 1)] = 'R'
for i = 1, 8 do
board_str = board_str .. (board[i] or ' ')
end
return board_str
end
function chess960.random_start_position()
return chess960.start_position(math.random(960) - 1)
end
return chess960