diff options
Diffstat (limited to 'lib/chessh/ssh/client')
-rw-r--r-- | lib/chessh/ssh/client/game/game.ex | 34 | ||||
-rw-r--r-- | lib/chessh/ssh/client/game/renderer.ex | 2 |
2 files changed, 31 insertions, 5 deletions
diff --git a/lib/chessh/ssh/client/game/game.ex b/lib/chessh/ssh/client/game/game.ex index 6b2ef60..da2bd99 100644 --- a/lib/chessh/ssh/client/game/game.ex +++ b/lib/chessh/ssh/client/game/game.ex @@ -455,7 +455,8 @@ defmodule Chessh.SSH.Client.Game do defp make_highlight_map( %State{ - game: %Game{last_move: last_move}, + game: %Game{last_move: last_move, turn: turn}, + binbo_pid: binbo_pid, flipped: flipped }, extra_highlights \\ %{} @@ -465,10 +466,33 @@ defmodule Chessh.SSH.Client.Game do [String.slice(last_move, 0..1), String.slice(last_move, 2..4)] |> Enum.map(fn coord -> Renderer.from_chess_coord(coord, flipped) end) - %{ - prev_move_from => Renderer.previous_move_background(), - prev_move_to => Renderer.previous_move_background() - } + binbo_bin_color = if(turn == :light, do: 0, else: 16) + binbo_atom_color = if(turn == :light, do: :white, else: :black) + + check_highlight = + if :binbo_position.is_in_check(binbo_bin_color, :binbo.game_state(binbo_pid)) do + {:ok, pieces_list} = :binbo.get_pieces_list(binbo_pid, :notation) + + {king_square, _, _} = + Enum.find(pieces_list, fn piece -> + case piece do + {_, ^binbo_atom_color, :king} -> true + _ -> false + end + end) + + %{Renderer.from_chess_coord(king_square, flipped) => Renderer.in_check_color()} + else + %{} + end + + Map.merge( + %{ + prev_move_from => Renderer.previous_move_background(), + prev_move_to => Renderer.previous_move_background() + }, + check_highlight + ) else %{} end diff --git a/lib/chessh/ssh/client/game/renderer.ex b/lib/chessh/ssh/client/game/renderer.ex index 6d7d7a7..c7d3a96 100644 --- a/lib/chessh/ssh/client/game/renderer.ex +++ b/lib/chessh/ssh/client/game/renderer.ex @@ -12,6 +12,7 @@ defmodule Chessh.SSH.Client.Game.Renderer do @previous_move_background ANSI.light_magenta_background() @from_select_background ANSI.light_green_background() @to_select_background ANSI.light_yellow_background() + @in_check_color ANSI.yellow_background() @dark_piece_color ANSI.red() @light_piece_color ANSI.light_cyan() @@ -21,6 +22,7 @@ defmodule Chessh.SSH.Client.Game.Renderer do def to_select_background(), do: @to_select_background def from_select_background(), do: @from_select_background def previous_move_background(), do: @previous_move_background + def in_check_color(), do: @in_check_color def to_chess_coord({y, x}) when x >= 0 and x < @chess_board_width and y >= 0 and y < @chess_board_height do |