diff --git a/lib/chess/board.ex b/lib/chess/board.ex index 3c575c5..78bf947 100644 --- a/lib/chess/board.ex +++ b/lib/chess/board.ex @@ -2,17 +2,19 @@ defmodule Chess.Board do @moduledoc false def transform(board) do - Enum.map(0..7, fn (rank) -> - Enum.map(0..7, fn (file) -> - board - |> piece({file, rank}) - end) + Enum.map(0..7, fn rank -> + {rank, + Enum.map(0..7, fn file -> + {file, + board + |> piece({file, rank})} + end)} end) end def search(board, %{"type" => type, "colour" => colour}) do board - |> Enum.filter(fn({_index, piece}) -> + |> Enum.filter(fn {_index, piece} -> match?(%{"type" => ^type, "colour" => ^colour}, piece) end) |> indexes_to_tuples @@ -20,7 +22,7 @@ defmodule Chess.Board do def search(board, %{"colour" => colour}) do board - |> Enum.filter(fn({_index, piece}) -> + |> Enum.filter(fn {_index, piece} -> match?(%{"colour" => ^colour}, piece) end) |> indexes_to_tuples @@ -31,9 +33,9 @@ defmodule Chess.Board do end def move_piece(board, %{ - "from" => [from_file, from_rank], - "to" => [to_file, to_rank] - }) do + from: {from_file, from_rank}, + to: {to_file, to_rank} + }) do {piece, board} = Map.pop(board, to_index({from_file, from_rank})) {piece_captured, board} = Map.pop(board, to_index({to_file, to_rank})) board = Map.put(board, to_index({to_file, to_rank}), piece) @@ -42,8 +44,8 @@ defmodule Chess.Board do if castling_move?(piece, from_file, to_file) do board |> castling_move(%{ - "from" => [from_file, from_rank], - "to" => [to_file, to_rank] + from: {from_file, from_rank}, + to: {to_file, to_rank} }) |> Map.get(:board) else @@ -55,39 +57,40 @@ defmodule Chess.Board do to: %{"file" => to_file, "rank" => to_rank}, board: board, piece: piece, - piece_captured: piece_captured, + piece_captured: piece_captured } end def castling_move?(%{"type" => "king"}, 4, to_file) do to_file == 2 || to_file == 6 end + def castling_move?(_, _, _), do: false - def castling_move(board, %{"from" => [4, rank], "to" => [2, _rank]}) do + def castling_move(board, %{from: {4, rank}, to: {2, _rank}}) do move_piece(board, %{ - "from" => [0, rank], - "to" => [3, rank], + from: {0, rank}, + to: {3, rank} }) end - def castling_move(board, %{"from" => [4, rank], "to" => [6, _rank]}) do + + def castling_move(board, %{from: {4, rank}, to: {6, _rank}}) do move_piece(board, %{ - "from" => [7, rank], - "to" => [5, rank], + from: {7, rank}, + to: {5, rank} }) end def default do %{ - "0,7" => %{"type" => "rook", "colour" => "black"}, + "0,7" => %{"type" => "rook", "colour" => "black"}, "1,7" => %{"type" => "knight", "colour" => "black"}, "2,7" => %{"type" => "bishop", "colour" => "black"}, - "3,7" => %{"type" => "queen", "colour" => "black"}, - "4,7" => %{"type" => "king", "colour" => "black"}, + "3,7" => %{"type" => "queen", "colour" => "black"}, + "4,7" => %{"type" => "king", "colour" => "black"}, "5,7" => %{"type" => "bishop", "colour" => "black"}, "6,7" => %{"type" => "knight", "colour" => "black"}, - "7,7" => %{"type" => "rook", "colour" => "black"}, - + "7,7" => %{"type" => "rook", "colour" => "black"}, "0,6" => %{"type" => "pawn", "colour" => "black"}, "1,6" => %{"type" => "pawn", "colour" => "black"}, "2,6" => %{"type" => "pawn", "colour" => "black"}, @@ -96,7 +99,6 @@ defmodule Chess.Board do "5,6" => %{"type" => "pawn", "colour" => "black"}, "6,6" => %{"type" => "pawn", "colour" => "black"}, "7,6" => %{"type" => "pawn", "colour" => "black"}, - "0,1" => %{"type" => "pawn", "colour" => "white"}, "1,1" => %{"type" => "pawn", "colour" => "white"}, "2,1" => %{"type" => "pawn", "colour" => "white"}, @@ -105,15 +107,14 @@ defmodule Chess.Board do "5,1" => %{"type" => "pawn", "colour" => "white"}, "6,1" => %{"type" => "pawn", "colour" => "white"}, "7,1" => %{"type" => "pawn", "colour" => "white"}, - - "0,0" => %{"type" => "rook", "colour" => "white"}, + "0,0" => %{"type" => "rook", "colour" => "white"}, "1,0" => %{"type" => "knight", "colour" => "white"}, "2,0" => %{"type" => "bishop", "colour" => "white"}, - "3,0" => %{"type" => "queen", "colour" => "white"}, - "4,0" => %{"type" => "king", "colour" => "white"}, + "3,0" => %{"type" => "queen", "colour" => "white"}, + "4,0" => %{"type" => "king", "colour" => "white"}, "5,0" => %{"type" => "bishop", "colour" => "white"}, "6,0" => %{"type" => "knight", "colour" => "white"}, - "7,0" => %{"type" => "rook", "colour" => "white"} + "7,0" => %{"type" => "rook", "colour" => "white"} } end @@ -123,13 +124,13 @@ defmodule Chess.Board do defp indexes_to_tuples(list) do list - |> Enum.map(fn({index, _piece}) -> index_to_tuple(index) end) + |> Enum.map(fn {index, _piece} -> index_to_tuple(index) end) end defp index_to_tuple(index) do index |> String.split(",") - |> Enum.map(&(String.to_integer(&1))) - |> List.to_tuple + |> Enum.map(&String.to_integer(&1)) + |> List.to_tuple() end end diff --git a/lib/chess/game_state.ex b/lib/chess/game_state.ex index 610128e..76fa64f 100644 --- a/lib/chess/game_state.ex +++ b/lib/chess/game_state.ex @@ -14,11 +14,15 @@ defmodule Chess.GameState do cond do player_checkmated?(board, colour) -> "checkmate" + player_stalemated?(board, colour) -> "stalemate" + king_in_check?(board, colour) -> "check" - true -> nil + + true -> + nil end end @@ -36,7 +40,7 @@ defmodule Chess.GameState do king = board |> Board.search(%{"type" => "king", "colour" => colour}) - |> List.first + |> List.first() if is_nil(king) do raise "There is no #{colour} king!" @@ -49,7 +53,7 @@ defmodule Chess.GameState do def player_cannot_move?(board, colour) do board |> Board.search(%{"colour" => colour}) - |> Enum.all?(fn({file, rank}) -> + |> Enum.all?(fn {file, rank} -> board |> piece_cannot_move?({file, rank}) end) @@ -62,9 +66,9 @@ defmodule Chess.GameState do board |> Moves.available({file, rank}) - |> Enum.all?(fn({to_file, to_rank}) -> + |> Enum.all?(fn {to_file, to_rank} -> board - |> Board.move_piece(%{"from" => [file, rank], "to" => [to_file, to_rank]}) + |> Board.move_piece(%{from: {file, rank}, to: {to_file, to_rank}}) |> Map.get(:board) |> king_in_check?(piece["colour"]) end) diff --git a/lib/chess/moves.ex b/lib/chess/moves.ex index bd0d9b9..3fc1c00 100644 --- a/lib/chess/moves.ex +++ b/lib/chess/moves.ex @@ -19,10 +19,10 @@ defmodule Chess.Moves do game.board |> Board.move_piece(move_params) - Multi.new + Multi.new() |> Multi.update(:game, Game.move_changeset(game, params)) |> Multi.insert(:move, Ecto.build_assoc(game, :moves, params)) - |> Repo.transaction + |> Repo.transaction() end def available(board, {file, rank}, move_list \\ []) do @@ -33,14 +33,19 @@ defmodule Chess.Moves do case piece do %{"type" => "pawn"} -> Pawn.moves(board, {file, rank}) + %{"type" => "rook"} -> Rook.moves(board, {file, rank}) + %{"type" => "bishop"} -> Bishop.moves(board, {file, rank}) + %{"type" => "knight"} -> Knight.moves(board, {file, rank}) + %{"type" => "king"} -> King.moves(board, {file, rank}, move_list) + %{"type" => "queen"} -> Queen.moves(board, {file, rank}) end diff --git a/lib/chess/moves/pieces/king/castling.ex b/lib/chess/moves/pieces/king/castling.ex index c5cc7e3..4aafa3d 100644 --- a/lib/chess/moves/pieces/king/castling.ex +++ b/lib/chess/moves/pieces/king/castling.ex @@ -18,12 +18,13 @@ defmodule Chess.Moves.Pieces.King.Castling do [] end end + def moves(_board, _piece, _move_list), do: [] def _moves(board, _rank, colour, move_list) do board |> Board.search(%{"type" => "rook", "colour" => colour}) - |> Enum.map(fn ({file, rank}) -> + |> Enum.map(fn {file, rank} -> case file do 0 -> queen_side_move(board, rank, colour, move_list) 7 -> king_side_move(board, rank, colour, move_list) @@ -35,44 +36,50 @@ defmodule Chess.Moves.Pieces.King.Castling do defp king_has_moved?(move_list, colour) do move_list - |> Enum.any?(fn (move) -> - match?(%Move{ - piece: %{"type" => "king", "colour" => ^colour} - }, move) + |> Enum.any?(fn move -> + match?( + %Move{ + piece: %{"type" => "king", "colour" => ^colour} + }, + move + ) end) end defp queen_side_move(board, rank, colour, move_list) do if queen_side_squares_empty?(board, rank) && - !queen_side_in_check?(board, rank, colour) && - !rook_has_moved?(0, move_list, colour) do + !queen_side_in_check?(board, rank, colour) && + !rook_has_moved?(0, move_list, colour) do {2, rank} end end defp king_side_move(board, rank, colour, move_list) do if king_side_squares_empty?(board, rank) && - !king_side_in_check?(board, rank, colour) && - !rook_has_moved?(7, move_list, colour) do + !king_side_in_check?(board, rank, colour) && + !rook_has_moved?(7, move_list, colour) do {6, rank} end end defp rook_has_moved?(file, move_list, colour) do move_list - |> Enum.any?(fn (move) -> - match?(%Move{ - piece: %{"type" => "rook", "colour" => ^colour}, - from: %{"file" => ^file}, - }, move) + |> Enum.any?(fn move -> + match?( + %Move{ + piece: %{"type" => "rook", "colour" => ^colour}, + from: %{"file" => ^file} + }, + move + ) end) end defp queen_side_in_check?(board, rank, colour) do [{2, rank}, {3, rank}] - |> Enum.any?(fn ({to_file, to_rank}) -> + |> Enum.any?(fn {to_file, to_rank} -> board - |> Board.move_piece(%{"from" => [4, rank], "to" => [to_file, to_rank]}) + |> Board.move_piece(%{from: {4, rank}, to: {to_file, to_rank}}) |> Map.get(:board) |> GameState.king_in_check?(colour) end) @@ -80,9 +87,9 @@ defmodule Chess.Moves.Pieces.King.Castling do defp king_side_in_check?(board, rank, colour) do [{5, rank}, {6, rank}] - |> Enum.any?(fn ({to_file, to_rank}) -> + |> Enum.any?(fn {to_file, to_rank} -> board - |> Board.move_piece(%{"from" => [4, rank], "to" => [to_file, to_rank]}) + |> Board.move_piece(%{from: {4, rank}, to: {to_file, to_rank}}) |> Map.get(:board) |> GameState.king_in_check?(colour) end) diff --git a/lib/chess/store/game.ex b/lib/chess/store/game.ex index 9610821..d5335f1 100644 --- a/lib/chess/store/game.ex +++ b/lib/chess/store/game.ex @@ -14,14 +14,14 @@ defmodule Chess.Store.Game do alias Chess.Store.User schema "games" do - field :board, :map, default: Board.default() - field :turn, :string, default: "white" - field :state, :string + field(:board, :map, default: Board.default()) + field(:turn, :string, default: "white") + field(:state, :string) - belongs_to :user, User - belongs_to :opponent, User, references: :id + belongs_to(:user, User) + belongs_to(:opponent, User, references: :id) - has_many :moves, Move + has_many(:moves, Move) timestamps() end @@ -55,15 +55,17 @@ defmodule Chess.Store.Game do end def for_user_id(user_id) do - from game in Game, + from(game in Game, where: game.user_id == ^user_id, or_where: game.opponent_id == ^user_id + ) end def check_game_state(changeset) do changeset |> put_change( - :state, GameState.state(changeset.changes.board, changeset.changes.turn) + :state, + GameState.state(changeset.changes.board, changeset.changes.turn) ) end @@ -78,6 +80,7 @@ defmodule Chess.Store.Game do changeset end end + def validate_king_in_check(changeset, _, _), do: changeset def ordered(query) do diff --git a/lib/chess_web/channels/game_channel.ex b/lib/chess_web/channels/game_channel.ex index 113fd1d..e3707c6 100644 --- a/lib/chess_web/channels/game_channel.ex +++ b/lib/chess_web/channels/game_channel.ex @@ -32,7 +32,7 @@ defmodule ChessWeb.GameChannel do board: Board.transform(game.board), turn: game.turn, state: game.state, - moves: MoveList.transform(game.moves), + moves: MoveList.transform(game.moves) } socket @@ -57,6 +57,7 @@ defmodule ChessWeb.GameChannel do update_opponent(socket, game) {:noreply, socket} + {:error, :game, changeset, _} -> {message, _} = changeset.errors[:board] @@ -65,21 +66,26 @@ defmodule ChessWeb.GameChannel do end def handle_in( - "game:get_available_moves", - %{"square" => [file, rank]}, - socket - ) do + "game:get_available_moves", + %{"square" => [file, rank]}, + socket + ) do game = socket.assigns.user_id |> Queries.game_with_moves(socket.assigns.game_id) - moves = Moves.available(game.board, { - String.to_integer(file), - String.to_integer(rank) - }, game.moves) + moves = + Moves.available( + game.board, + { + String.to_integer(file), + String.to_integer(rank) + }, + game.moves + ) reply = %{ - moves: Enum.map(moves, &(Tuple.to_list(&1))) + moves: Enum.map(moves, &Tuple.to_list(&1)) } {:reply, {:ok, reply}, socket} @@ -88,27 +94,29 @@ defmodule ChessWeb.GameChannel do def update_opponent(socket, game) do opponent_id = opponent(game, socket.assigns.user_id).id - |> Integer.to_string + |> Integer.to_string() send_update(socket) "game:#{game.id}" - |> Presence.list + |> Presence.list() |> case do %{^opponent_id => _} -> nil + _ -> socket |> Emails.opponent_moved_email(game) - |> Mailer.deliver_later + |> Mailer.deliver_later() end end def track_presence(socket) do - {:ok, _} = Presence.track(socket, socket.assigns.user_id, %{ - user_id: socket.assigns.user_id, - online_at: inspect(System.system_time(:second)) - }) + {:ok, _} = + Presence.track(socket, socket.assigns.user_id, %{ + user_id: socket.assigns.user_id, + online_at: inspect(System.system_time(:second)) + }) socket |> push("presence_state", Presence.list(socket)) @@ -116,8 +124,8 @@ defmodule ChessWeb.GameChannel do def convert_params(%{"from" => from, "to" => to}) do %{ - "from" => Enum.map(from, &(String.to_integer(&1))), - "to" => Enum.map(to, &(String.to_integer(&1))), + "from" => Enum.map(from, &String.to_integer(&1)), + "to" => Enum.map(to, &String.to_integer(&1)) } end @@ -130,7 +138,7 @@ defmodule ChessWeb.GameChannel do board: Board.transform(game.board), turn: game.turn, state: game.state, - moves: MoveList.transform(game.moves), + moves: MoveList.transform(game.moves) } ChessWeb.Endpoint.broadcast("game:#{game.id}", "game:update", payload) diff --git a/lib/chess_web/templates/game/board.html.leex b/lib/chess_web/templates/game/board.html.leex index 437d2e6..c5d3ede 100644 --- a/lib/chess_web/templates/game/board.html.leex +++ b/lib/chess_web/templates/game/board.html.leex @@ -1,4 +1,4 @@ -