Draw board #3
@ -12,7 +12,7 @@ defmodule Chessh.SSH.Client do
|
|||||||
]
|
]
|
||||||
|
|
||||||
@min_terminal_width 64
|
@min_terminal_width 64
|
||||||
@min_terminal_height 31
|
@min_terminal_height 32
|
||||||
@max_terminal_width 255
|
@max_terminal_width 255
|
||||||
@max_terminal_height 127
|
@max_terminal_height 127
|
||||||
|
|
||||||
|
@ -14,6 +14,9 @@ defmodule Chessh.SSH.Client.Board do
|
|||||||
@chess_board_height 8
|
@chess_board_height 8
|
||||||
@chess_board_width 8
|
@chess_board_width 8
|
||||||
|
|
||||||
|
@dark_piece_color ANSI.magenta()
|
||||||
|
@light_piece_color ANSI.red()
|
||||||
|
|
||||||
def tileIsLight(row, col) do
|
def tileIsLight(row, col) do
|
||||||
rem(row, 2) == rem(col, 2)
|
rem(row, 2) == rem(col, 2)
|
||||||
end
|
end
|
||||||
@ -42,20 +45,7 @@ defmodule Chessh.SSH.Client.Board do
|
|||||||
Enum.flat_map(rows, fn row -> Enum.map(1..tile_height, fn _ -> row end) end)
|
Enum.flat_map(rows, fn row -> Enum.map(1..tile_height, fn _ -> row end) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_board(fen, {tile_width, tile_height} = tile_dims) do
|
defp skip_cols_or_place_piece_reduce(char, {curr_column, data}, rowI) do
|
||||||
rows =
|
|
||||||
String.split(fen, " ")
|
|
||||||
|> List.first()
|
|
||||||
|> String.split("/")
|
|
||||||
|
|
||||||
coordinate_to_piece =
|
|
||||||
Enum.zip(rows, 0..(length(rows) - 1))
|
|
||||||
|> Enum.map(fn {row, rowI} ->
|
|
||||||
{@chess_board_height, pieces_per_row} =
|
|
||||||
Enum.reduce(
|
|
||||||
String.split(row, ""),
|
|
||||||
{0, %{}},
|
|
||||||
fn char, {curr_column, data} ->
|
|
||||||
case Integer.parse(char) do
|
case Integer.parse(char) do
|
||||||
{skip, ""} ->
|
{skip, ""} ->
|
||||||
{curr_column + skip, data}
|
{curr_column + skip, data}
|
||||||
@ -66,17 +56,31 @@ defmodule Chessh.SSH.Client.Board do
|
|||||||
{curr_column, data}
|
{curr_column, data}
|
||||||
|
|
||||||
type ->
|
type ->
|
||||||
|
shade = if(char != String.capitalize(char), do: "dark", else: "light")
|
||||||
|
|
||||||
{curr_column + 1,
|
{curr_column + 1,
|
||||||
Map.put(
|
Map.put(
|
||||||
data,
|
data,
|
||||||
"#{rowI}, #{curr_column}",
|
"#{rowI}, #{curr_column}",
|
||||||
@ascii_chars["pieces"][
|
{shade, type}
|
||||||
if(char != String.capitalize(char), do: "dark", else: "light")
|
|
||||||
][type]
|
|
||||||
)}
|
)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp make_coordinate_to_piece_art_map(fen) do
|
||||||
|
rows =
|
||||||
|
String.split(fen, " ")
|
||||||
|
|> List.first()
|
||||||
|
|> String.split("/")
|
||||||
|
|
||||||
|
Enum.zip(rows, 0..(length(rows) - 1))
|
||||||
|
|> Enum.map(fn {row, rowI} ->
|
||||||
|
{@chess_board_height, pieces_per_row} =
|
||||||
|
Enum.reduce(
|
||||||
|
String.split(row, ""),
|
||||||
|
{0, %{}},
|
||||||
|
&skip_cols_or_place_piece_reduce(&1, &2, rowI)
|
||||||
)
|
)
|
||||||
|
|
||||||
pieces_per_row
|
pieces_per_row
|
||||||
@ -84,39 +88,81 @@ defmodule Chessh.SSH.Client.Board do
|
|||||||
|> Enum.reduce(%{}, fn pieces_map_for_this_row, acc ->
|
|> Enum.reduce(%{}, fn pieces_map_for_this_row, acc ->
|
||||||
Map.merge(acc, pieces_map_for_this_row)
|
Map.merge(acc, pieces_map_for_this_row)
|
||||||
end)
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_board(fen, {tile_width, tile_height} = tile_dims) do
|
||||||
|
coordinate_to_piece = make_coordinate_to_piece_art_map(fen)
|
||||||
board = make_board(tile_dims)
|
board = make_board(tile_dims)
|
||||||
|
|
||||||
Enum.zip_with([board, 0..(length(board) - 1)], fn [rowStr, row] ->
|
Enum.zip_with([board, 0..(length(board) - 1)], fn [rowStr, row] ->
|
||||||
curr_y = div(row, tile_height)
|
curr_y = div(row, tile_height)
|
||||||
|
|
||||||
Enum.zip_with([String.graphemes(rowStr), 0..(String.length(rowStr) - 1)], fn [char, col] ->
|
%{row_chars: row_chars} =
|
||||||
|
Enum.reduce(
|
||||||
|
Enum.zip(String.graphemes(rowStr), 0..(String.length(rowStr) - 1)),
|
||||||
|
%{current_color: ANSI.black(), row_chars: []},
|
||||||
|
fn {char, col}, %{current_color: current_color, row_chars: row_chars} = row_state ->
|
||||||
curr_x = div(col, tile_width)
|
curr_x = div(col, tile_width)
|
||||||
key = "#{curr_y}, #{curr_x}"
|
key = "#{curr_y}, #{curr_x}"
|
||||||
|
|
||||||
if Map.has_key?(coordinate_to_piece, key) do
|
case Map.fetch(coordinate_to_piece, key) do
|
||||||
piece_row =
|
{:ok, {shade, type}} ->
|
||||||
Map.fetch!(coordinate_to_piece, key)
|
piece = @ascii_chars["pieces"][shade][type]
|
||||||
|> Enum.at(row - curr_y * tile_height)
|
piece_line = Enum.at(piece, row - curr_y * tile_height)
|
||||||
|
|
||||||
piece_row_len = String.length(piece_row)
|
piece_line_len = String.length(piece_line)
|
||||||
centered_col = div(tile_width - piece_row_len, 2)
|
pad_left_right = div(tile_width - piece_line_len, 2)
|
||||||
relative_to_tile_col = col - curr_x * tile_width
|
relative_to_tile_col = col - curr_x * tile_width
|
||||||
Logger.debug("#{piece_row_len}, #{centered_col}, #{relative_to_tile_col}")
|
|
||||||
|
|
||||||
piece_char =
|
if relative_to_tile_col >= pad_left_right &&
|
||||||
if relative_to_tile_col >= centered_col &&
|
relative_to_tile_col < tile_width - pad_left_right do
|
||||||
relative_to_tile_col <= tile_width - centered_col - 1,
|
piece_char = String.at(piece_line, relative_to_tile_col - pad_left_right)
|
||||||
do: String.at(piece_row, relative_to_tile_col - centered_col),
|
new_char = if piece_char == " ", do: char, else: piece_char
|
||||||
else: " "
|
|
||||||
|
|
||||||
|
color =
|
||||||
if piece_char == " ",
|
if piece_char == " ",
|
||||||
do: char,
|
do: ANSI.default_color(),
|
||||||
else: piece_char
|
else: if(shade == "dark", do: @dark_piece_color, else: @light_piece_color)
|
||||||
|
|
||||||
|
if color != current_color do
|
||||||
|
%{
|
||||||
|
row_state
|
||||||
|
| current_color: color,
|
||||||
|
row_chars: row_chars ++ [color, new_char]
|
||||||
|
}
|
||||||
else
|
else
|
||||||
char
|
%{
|
||||||
|
row_state
|
||||||
|
| current_color: current_color,
|
||||||
|
row_chars: row_chars ++ [new_char]
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end)
|
else
|
||||||
|
%{
|
||||||
|
row_state
|
||||||
|
| current_color: ANSI.default_color(),
|
||||||
|
row_chars: row_chars ++ [ANSI.default_color(), char]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
if ANSI.white() != current_color do
|
||||||
|
%{
|
||||||
|
row_state
|
||||||
|
| current_color: ANSI.default_color(),
|
||||||
|
row_chars: row_chars ++ [ANSI.default_color(), char]
|
||||||
|
}
|
||||||
|
else
|
||||||
|
%{
|
||||||
|
row_state
|
||||||
|
| row_chars: row_chars ++ [char]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
|
row_chars
|
||||||
|> Enum.join("")
|
|> Enum.join("")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
@ -76,7 +76,6 @@ defmodule Chessh.SSH.Tui do
|
|||||||
{:send_data, data},
|
{:send_data, data},
|
||||||
%{connection_ref: connection_ref, channel_id: channel_id} = state
|
%{connection_ref: connection_ref, channel_id: channel_id} = state
|
||||||
) do
|
) do
|
||||||
Logger.debug("Data was sent to TUI process #{inspect(data)}")
|
|
||||||
:ssh_connection.send(connection_ref, channel_id, data)
|
:ssh_connection.send(connection_ref, channel_id, data)
|
||||||
{:ok, state}
|
{:ok, state}
|
||||||
end
|
end
|
||||||
@ -97,7 +96,6 @@ defmodule Chessh.SSH.Tui do
|
|||||||
{:ssh_cm, _connection_handler, {:data, _channel_id, _type, data}},
|
{:ssh_cm, _connection_handler, {:data, _channel_id, _type, data}},
|
||||||
state
|
state
|
||||||
) do
|
) do
|
||||||
Logger.debug("DATA #{inspect(data)}")
|
|
||||||
send(state.client_pid, {:data, data})
|
send(state.client_pid, {:data, data})
|
||||||
{:ok, state}
|
{:ok, state}
|
||||||
end
|
end
|
||||||
@ -132,7 +130,6 @@ defmodule Chessh.SSH.Tui do
|
|||||||
{:window_change, _channel_id, width, height, _pixwidth, _pixheight}},
|
{:window_change, _channel_id, width, height, _pixwidth, _pixheight}},
|
||||||
%{client_pid: client_pid} = state
|
%{client_pid: client_pid} = state
|
||||||
) do
|
) do
|
||||||
Logger.debug("WINDOW CHANGE")
|
|
||||||
send(client_pid, {:resize, {width, height}})
|
send(client_pid, {:resize, {width, height}})
|
||||||
|
|
||||||
{:ok,
|
{:ok,
|
||||||
|
@ -28,14 +28,14 @@
|
|||||||
"knight": [" ", "/`)", " U ", "[.]"],
|
"knight": [" ", "/`)", " U ", "[.]"],
|
||||||
"bishop": [" . ", "(\\)", " U ", "[.]"],
|
"bishop": [" . ", "(\\)", " U ", "[.]"],
|
||||||
"queen": [" . ", ").(", ").(", "[.]"],
|
"queen": [" . ", ").(", ").(", "[.]"],
|
||||||
"king": [" + ", ").(", ").(", "[.]"],
|
"king": [" + ", ").(", "|.|", "[.]"],
|
||||||
"pawn": [" ", " o ", " U ", "[.]"]
|
"pawn": [" ", " o ", " U ", "[.]"]
|
||||||
},
|
},
|
||||||
"dark": {
|
"dark": {
|
||||||
"rook": [" ", "L-|", " U ", "[#]"],
|
"rook": [" ", "L-|", " U ", "[#]"],
|
||||||
"knight": [" ", "/`)", " U ", "[#]"],
|
"knight": [" ", "/`)", " U ", "[#]"],
|
||||||
"bishop": [" . ", "(\\)", " U ", "[#]"],
|
"bishop": [" . ", "(\\)", " U ", "[#]"],
|
||||||
"king": [" + ", ")#(", ")#(", "[#]"],
|
"king": [" + ", ")#(", "|#|", "[#]"],
|
||||||
"queen": [" . ", ")#(", ")#(", "[#]"],
|
"queen": [" . ", ")#(", ")#(", "[#]"],
|
||||||
"pawn": [" ", " o ", " U ", "[#]"]
|
"pawn": [" ", " o ", " U ", "[#]"]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user