2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,11 @@
defmodule Chess960 do
@pieces ~w(♔ ♕ ♘ ♘ ♗ ♗ ♖ ♖) # ~w(K Q N N B B R R)
@regexes [~r/♗(..)*♗/, ~r/♖.*♔.*♖/] # [~r/B(..)*B/, ~r/R.*K.*R/]
def shuffle do
row = Enum.shuffle(@pieces) |> Enum.join
if Enum.all?(@regexes, &Regex.match?(&1, row)), do: row, else: shuffle
end
end
Enum.each(1..5, fn _ -> IO.puts Chess960.shuffle end)

View file

@ -0,0 +1,13 @@
defmodule Chess960 do
def construct do
row = Enum.reduce(~w[♕ ♘ ♘], ~w[♖ ♔ ♖], fn piece,acc ->
List.insert_at(acc, :rand.uniform(length(acc)+1)-1, piece)
end)
[Enum.random([0, 2, 4, 6]), Enum.random([1, 3, 5, 7])]
|> Enum.sort
|> Enum.reduce(row, fn pos,acc -> List.insert_at(acc, pos, "♗") end)
|> Enum.join
end
end
Enum.each(1..5, fn _ -> IO.puts Chess960.construct end)

View file

@ -0,0 +1,31 @@
defmodule Chess960 do
@krn ~w(NNRKR NRNKR NRKNR NRKRN RNNKR RNKNR RNKRN RKNNR RKNRN RKRNN)
def start_position, do: start_position(:rand.uniform(960)-1)
def start_position(id) do
pos = List.duplicate(nil, 8)
q = div(id, 4)
r = rem(id, 4)
pos = List.replace_at(pos, r * 2 + 1, "B")
q = div(q, 4)
r = rem(q, 4)
pos = List.replace_at(pos, r * 2, "B")
q = div(q, 6)
r = rem(q, 6)
i = Enum.reject(0..7, &Enum.at(pos,&1)) |> Enum.at(r)
pos = List.replace_at(pos, i, "Q")
krn = Enum.at(@krn, q) |> String.codepoints
Enum.reject(0..7, &Enum.at(pos,&1))
|> Enum.zip(krn)
|> Enum.reduce(pos, fn {i,x},acc -> List.replace_at(acc,i,x) end)
|> Enum.join
end
end
IO.puts "Generate Start Position from ID number"
Enum.each([0,518,959], fn id ->
:io.format "~3w : ~s~n", [id, Chess960.start_position(id)]
end)
IO.puts "\nGenerate random Start Position"
Enum.each(1..5, fn _ -> IO.puts Chess960.start_position end)