mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Translate moves into display format
This commit is contained in:
parent
49bba88f07
commit
87c453c564
@ -2,6 +2,7 @@ defmodule Chess.Moves do
|
|||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
alias Chess.Board
|
alias Chess.Board
|
||||||
|
alias Chess.Store.Move
|
||||||
|
|
||||||
alias Chess.Moves.Pieces.Pawn
|
alias Chess.Moves.Pieces.Pawn
|
||||||
alias Chess.Moves.Pieces.Bishop
|
alias Chess.Moves.Pieces.Bishop
|
||||||
@ -10,6 +11,12 @@ defmodule Chess.Moves do
|
|||||||
alias Chess.Moves.Pieces.Queen
|
alias Chess.Moves.Pieces.Queen
|
||||||
alias Chess.Moves.Pieces.King
|
alias Chess.Moves.Pieces.King
|
||||||
|
|
||||||
|
def transform(moves) do
|
||||||
|
moves
|
||||||
|
|> Enum.map(fn(move) -> Move.translate(move) end)
|
||||||
|
|> Enum.chunk_every(2)
|
||||||
|
end
|
||||||
|
|
||||||
def available(board, {file, rank}) do
|
def available(board, {file, rank}) do
|
||||||
piece =
|
piece =
|
||||||
board
|
board
|
||||||
|
|||||||
@ -27,5 +27,13 @@ defmodule Chess.Store.Move do
|
|||||||
|> validate_required(required_attrs())
|
|> validate_required(required_attrs())
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def translate(move) do
|
||||||
|
[
|
||||||
|
<<97 + move.from.file, 49 + move.from.rank>>,
|
||||||
|
<<97 + move.to.file, 49 + move.to.rank>>
|
||||||
|
]
|
||||||
|
|> Enum.join("-")
|
||||||
|
end
|
||||||
|
|
||||||
defp required_attrs, do: ~w[game_id from to piece]a
|
defp required_attrs, do: ~w[game_id from to piece]a
|
||||||
end
|
end
|
||||||
|
|||||||
34
test/chess/moves_test.exs
Normal file
34
test/chess/moves_test.exs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
defmodule Chess.MovesTest do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
use Chess.DataCase
|
||||||
|
|
||||||
|
describe "moves" do
|
||||||
|
alias Chess.Store.Move
|
||||||
|
alias Chess.Moves
|
||||||
|
|
||||||
|
test "tranforms a list of moves" do
|
||||||
|
moves = [
|
||||||
|
%Move{
|
||||||
|
from: %{file: 4, rank: 1},
|
||||||
|
to: %{file: 4, rank: 3},
|
||||||
|
},
|
||||||
|
%Move{
|
||||||
|
from: %{file: 4, rank: 6},
|
||||||
|
to: %{file: 4, rank: 4},
|
||||||
|
},
|
||||||
|
%Move{
|
||||||
|
from: %{file: 1, rank: 0},
|
||||||
|
to: %{file: 2, rank: 2},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
expected_result = [
|
||||||
|
["e2-e4", "e7-e5"],
|
||||||
|
["b1-c3"],
|
||||||
|
]
|
||||||
|
|
||||||
|
assert Moves.transform(moves) == expected_result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -77,5 +77,15 @@ defmodule Chess.MoveTest do
|
|||||||
|
|
||||||
refute changeset.valid?
|
refute changeset.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
test "translates a move" do
|
||||||
|
move = %Move{
|
||||||
|
from: %{file: 4, rank: 1},
|
||||||
|
to: %{file: 4, rank: 3},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert Move.translate(move) == "e2-e4"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user