From bff9fd0a6ae8b1f034e4d20007b442957539e7ec Mon Sep 17 00:00:00 2001 From: Simponic Date: Wed, 4 Jan 2023 20:43:32 -0700 Subject: Well it's drawing *something* --- priv/ascii_chars.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'priv/ascii_chars.json') diff --git a/priv/ascii_chars.json b/priv/ascii_chars.json index e50be22..58a7cb0 100644 --- a/priv/ascii_chars.json +++ b/priv/ascii_chars.json @@ -94,7 +94,7 @@ ] }, "pieces": { - "white": { + "light": { "rook": [ " ", " [`'`'] ", @@ -137,7 +137,7 @@ " " ] }, - "black": { + "dark": { "rook": [ " ", " [`'`'] ", -- cgit v1.2.3-70-g09d2 From ce62dd6106f94473db919335cc49029421fde4ad Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 6 Jan 2023 17:02:56 -0700 Subject: Board drawing working --- lib/chessh/ssh/screens/board.ex | 37 +++++--- lib/chessh/ssh/screens/menu.ex | 2 +- priv/ascii_chars.json | 203 ++++++---------------------------------- 3 files changed, 57 insertions(+), 185 deletions(-) (limited to 'priv/ascii_chars.json') diff --git a/lib/chessh/ssh/screens/board.ex b/lib/chessh/ssh/screens/board.ex index 9b18295..3ce4fb6 100644 --- a/lib/chessh/ssh/screens/board.ex +++ b/lib/chessh/ssh/screens/board.ex @@ -34,8 +34,9 @@ defmodule Chessh.SSH.Client.Board do rows = Enum.map(0..(@chess_board_height - 1), fn i -> Enum.map(0..(@chess_board_width - 1), fn j -> - List.duplicate(if(tileIsLight(i, j), do: ' ', else: '#'), tile_width) + List.duplicate(if(tileIsLight(i, j), do: ' ', else: '▊'), tile_width) end) + |> Enum.join("") end) Enum.flat_map(rows, fn row -> Enum.map(1..tile_height, fn _ -> row end) end) @@ -70,7 +71,7 @@ defmodule Chessh.SSH.Client.Board do data, "#{rowI}, #{curr_column}", @ascii_chars["pieces"][ - if(char != String.capitalize(char), do: "light", else: "dark") + if(char != String.capitalize(char), do: "dark", else: "light") ][type] )} end @@ -86,20 +87,32 @@ defmodule Chessh.SSH.Client.Board do board = make_board(tile_dims) - Enum.zip_with([board, 1..length(board)], fn [row, rowI] -> - curr_y = div(rowI, tile_height) + Enum.zip_with([board, 0..(length(board) - 1)], fn [rowStr, row] -> + curr_y = div(row, tile_height) - Enum.zip_with([row, 1..length(row)], fn [char, col] -> + Enum.zip_with([String.graphemes(rowStr), 0..(String.length(rowStr) - 1)], fn [char, col] -> curr_x = div(col, tile_width) - key = "#{rowI}, #{col}" + key = "#{curr_y}, #{curr_x}" if Map.has_key?(coordinate_to_piece, key) do - piece_char = + piece_row = Map.fetch!(coordinate_to_piece, key) - |> Enum.at(rowI - curr_y * tile_height) - |> String.at(col - curr_x * tile_width) + |> Enum.at(row - curr_y * tile_height) + + piece_row_len = String.length(piece_row) + centered_col = div(tile_width - piece_row_len, 2) + relative_to_tile_col = col - curr_x * tile_width + Logger.debug("#{piece_row_len}, #{centered_col}, #{relative_to_tile_col}") - if piece_char == " ", do: String.Chars.to_string(char), else: piece_char + piece_char = + if relative_to_tile_col >= centered_col && + relative_to_tile_col <= tile_width - centered_col - 1, + do: String.at(piece_row, relative_to_tile_col - centered_col), + else: " " + + if piece_char == " ", + do: char, + else: piece_char else char end @@ -109,11 +122,11 @@ defmodule Chessh.SSH.Client.Board do end def render(%Client.State{} = _state) do - board = make_board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", {9, 5}) + board = make_board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", {7, 4}) [ANSI.home()] ++ Enum.map( - Enum.zip(0..(length(board) - 1), board), + Enum.zip(1..length(board), board), fn {i, line} -> [ANSI.cursor(i, 0), line] end diff --git a/lib/chessh/ssh/screens/menu.ex b/lib/chessh/ssh/screens/menu.ex index f89670f..9baa059 100644 --- a/lib/chessh/ssh/screens/menu.ex +++ b/lib/chessh/ssh/screens/menu.ex @@ -38,7 +38,7 @@ defmodule Chessh.SSH.Client.Menu do {y, x} = center_rect({logo_width, logo_height + length(text)}, {width, height}) Enum.flat_map( - Enum.zip(0..(length(text) - 1), text), + Enum.zip(1..length(text), text), fn {i, line} -> [ ANSI.cursor(y + i + dy, x + dx), diff --git a/priv/ascii_chars.json b/priv/ascii_chars.json index 58a7cb0..3f62794 100644 --- a/priv/ascii_chars.json +++ b/priv/ascii_chars.json @@ -1,184 +1,43 @@ { "letters": { - "a": [ - " ", - " /\\ ", - "/--\\" - ], - "b": [ - " __ ", - "|__)", - "|__)" - ], - "c": [ - " __ ", - "/ ", - "\\__ " - ], - "d": [ - " __ ", - "| \\", - "|__/" - ], - "e": [ - " __ ", - "|_ ", - "|__ " - ], - "f": [ - " __ ", - "|_ ", - "| " - ], - "g": [ - " __ ", - "/ _ ", - "\\__\\" - ], - "h": [ - "| |", - "|__|", - "| |" - ] + "a": [" ", " /\\ ", "/--\\"], + "b": [" __ ", "|__)", "|__)"], + "c": [" __ ", "/ ", "\\__ "], + "d": [" __ ", "| \\", "|__/"], + "e": [" __ ", "|_ ", "|__ "], + "f": [" __ ", "|_ ", "| "], + "g": [" __ ", "/ _ ", "\\__\\"], + "h": ["| |", "|__|", "| |"] }, "numbers": { - "0": [ - " _ ", - "| |", - "|_|" - ], - "1": [ - " ", - " /|", - " |" - ], - "2": [ - " _ ", - " )", - " /_" - ], - "3": [ - " _ ", - " _)", - " _)" - ], - "4": [ - " .", - " /|", - "'-|" - ], - "5": [ - " _ ", - "|_ ", - " _)" - ], - "6": [ - " ", - " / ", - "(_)" - ], - "7": [ - " __", - " /", - " / " - ], - "8": [ - " _ ", - "(_)", - "(_)" - ], - "9": [ - " _ ", - "(_)", - " )" - ] + "0": [" _ ", "| |", "|_|"], + "1": [" ", " /|", " |"], + "2": [" _ ", " )", " /_"], + "3": [" _ ", " _)", " _)"], + "4": [" .", " /|", "'-|"], + "5": [" _ ", "|_ ", " _)"], + "6": [" ", " / ", "(_)"], + "7": [" __", " /", " / "], + "8": [" _ ", "(_)", "(_)"], + "9": [" _ ", "(_)", " )"] }, + "pieces": { "light": { - "rook": [ - " ", - " [`'`'] ", - " | | ", - " |__| ", - " " - ], - "knight": [ - " _ _ ", - " \\` '/ ", - " (o o) ", - " \\ / \\", - " ^ " - ], - "queen": [ - " /\\+/\\ ", - " /(o o)\\ ", - " (_) ", - " " - ], - "king": [ - " ", - " |`+'| ", - " (o o) ", - " (_) ", - " " - ], - "bishop": [ - " ", - " |v| ", - " (0 o) ", - " (_) ", - " " - ], - "pawn": [ - " _ ", - " ( ) ", - " | | ", - " |_| ", - " " - ] + "rook": [" ", "L_|", " U ", "[.]"], + "knight": [" ", "/`)", " U ", "[.]"], + "bishop": [" . ", "(\\)", " U ", "[.]"], + "queen": [" . ", ").(", ").(", "[.]"], + "king": [" + ", ").(", ").(", "[.]"], + "pawn": [" ", " o ", " U ", "[.]"] }, "dark": { - "rook": [ - " ", - " [`'`'] ", - " |::| ", - " |::| ", - " " - ], - "knight": [ - " _ _ ", - " \\`.'/ ", - " (o:o) ", - " \\:/:\\", - " ^ " - ], - "queen": [ - " /\\+/\\ ", - " /(o:o)\\ ", - " (:) ", - " " - ], - "king": [ - " ", - " |`+'| ", - " (o:o) ", - " (:) ", - " " - ], - "bishop": [ - " ", - " |v| ", - " (o:o) ", - " (:) ", - " " - ], - "pawn": [ - " _ ", - " (:) ", - " |:| ", - " |_| ", - " " - ] + "rook": [" ", "L-|", " U ", "[#]"], + "knight": [" ", "/`)", " U ", "[#]"], + "bishop": [" . ", "(\\)", " U ", "[#]"], + "king": [" + ", ")#(", ")#(", "[#]"], + "queen": [" . ", ")#(", ")#(", "[#]"], + "pawn": [" ", " o ", " U ", "[#]"] } } } -- cgit v1.2.3-70-g09d2 From 5b8dc2cb9800ed38ba1dc02fa5a135880a2c0bac Mon Sep 17 00:00:00 2001 From: Simponic Date: Fri, 6 Jan 2023 20:55:12 -0700 Subject: Checkpoint - board is drawn! --- lib/chessh/ssh/client.ex | 2 +- lib/chessh/ssh/screens/board.ex | 172 +++++++++++++++++++++++++--------------- lib/chessh/ssh/tui.ex | 3 - priv/ascii_chars.json | 4 +- 4 files changed, 112 insertions(+), 69 deletions(-) (limited to 'priv/ascii_chars.json') diff --git a/lib/chessh/ssh/client.ex b/lib/chessh/ssh/client.ex index 4ed2fd1..fdd3875 100644 --- a/lib/chessh/ssh/client.ex +++ b/lib/chessh/ssh/client.ex @@ -12,7 +12,7 @@ defmodule Chessh.SSH.Client do ] @min_terminal_width 64 - @min_terminal_height 31 + @min_terminal_height 32 @max_terminal_width 255 @max_terminal_height 127 diff --git a/lib/chessh/ssh/screens/board.ex b/lib/chessh/ssh/screens/board.ex index 3ce4fb6..0aac50a 100644 --- a/lib/chessh/ssh/screens/board.ex +++ b/lib/chessh/ssh/screens/board.ex @@ -14,6 +14,9 @@ defmodule Chessh.SSH.Client.Board do @chess_board_height 8 @chess_board_width 8 + @dark_piece_color ANSI.magenta() + @light_piece_color ANSI.red() + def tileIsLight(row, col) do rem(row, 2) == rem(col, 2) end @@ -42,81 +45,124 @@ defmodule Chessh.SSH.Client.Board do Enum.flat_map(rows, fn row -> Enum.map(1..tile_height, fn _ -> row 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 + case Integer.parse(char) do + {skip, ""} -> + {curr_column + skip, data} + + _ -> + case piece_type(char) do + nil -> + {curr_column, data} + + type -> + shade = if(char != String.capitalize(char), do: "dark", else: "light") + + {curr_column + 1, + Map.put( + data, + "#{rowI}, #{curr_column}", + {shade, type} + )} + end + end + end + + defp make_coordinate_to_piece_art_map(fen) 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 - {skip, ""} -> - {curr_column + skip, data} - - _ -> - case piece_type(char) do - nil -> - {curr_column, data} - - type -> - {curr_column + 1, - Map.put( - data, - "#{rowI}, #{curr_column}", - @ascii_chars["pieces"][ - if(char != String.capitalize(char), do: "dark", else: "light") - ][type] - )} - end - end - end - ) + 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 - end) - |> Enum.reduce(%{}, fn pieces_map_for_this_row, acc -> - Map.merge(acc, pieces_map_for_this_row) - end) + pieces_per_row + end) + |> Enum.reduce(%{}, fn pieces_map_for_this_row, acc -> + Map.merge(acc, pieces_map_for_this_row) + 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) Enum.zip_with([board, 0..(length(board) - 1)], fn [rowStr, row] -> curr_y = div(row, tile_height) - Enum.zip_with([String.graphemes(rowStr), 0..(String.length(rowStr) - 1)], fn [char, col] -> - curr_x = div(col, tile_width) - key = "#{curr_y}, #{curr_x}" - - if Map.has_key?(coordinate_to_piece, key) do - piece_row = - Map.fetch!(coordinate_to_piece, key) - |> Enum.at(row - curr_y * tile_height) - - piece_row_len = String.length(piece_row) - centered_col = div(tile_width - piece_row_len, 2) - 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 >= centered_col && - relative_to_tile_col <= tile_width - centered_col - 1, - do: String.at(piece_row, relative_to_tile_col - centered_col), - else: " " - - if piece_char == " ", - do: char, - else: piece_char - else - char - end - end) + %{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) + key = "#{curr_y}, #{curr_x}" + + case Map.fetch(coordinate_to_piece, key) do + {:ok, {shade, type}} -> + piece = @ascii_chars["pieces"][shade][type] + piece_line = Enum.at(piece, row - curr_y * tile_height) + + piece_line_len = String.length(piece_line) + pad_left_right = div(tile_width - piece_line_len, 2) + relative_to_tile_col = col - curr_x * tile_width + + if relative_to_tile_col >= pad_left_right && + relative_to_tile_col < tile_width - pad_left_right do + piece_char = String.at(piece_line, relative_to_tile_col - pad_left_right) + new_char = if piece_char == " ", do: char, else: piece_char + + color = + if piece_char == " ", + do: ANSI.default_color(), + 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 + %{ + row_state + | current_color: current_color, + row_chars: row_chars ++ [new_char] + } + 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("") end) end diff --git a/lib/chessh/ssh/tui.ex b/lib/chessh/ssh/tui.ex index d76986f..5d43760 100644 --- a/lib/chessh/ssh/tui.ex +++ b/lib/chessh/ssh/tui.ex @@ -76,7 +76,6 @@ defmodule Chessh.SSH.Tui do {:send_data, data}, %{connection_ref: connection_ref, channel_id: channel_id} = state ) do - Logger.debug("Data was sent to TUI process #{inspect(data)}") :ssh_connection.send(connection_ref, channel_id, data) {:ok, state} end @@ -97,7 +96,6 @@ defmodule Chessh.SSH.Tui do {:ssh_cm, _connection_handler, {:data, _channel_id, _type, data}}, state ) do - Logger.debug("DATA #{inspect(data)}") send(state.client_pid, {:data, data}) {:ok, state} end @@ -132,7 +130,6 @@ defmodule Chessh.SSH.Tui do {:window_change, _channel_id, width, height, _pixwidth, _pixheight}}, %{client_pid: client_pid} = state ) do - Logger.debug("WINDOW CHANGE") send(client_pid, {:resize, {width, height}}) {:ok, diff --git a/priv/ascii_chars.json b/priv/ascii_chars.json index 3f62794..1be31f8 100644 --- a/priv/ascii_chars.json +++ b/priv/ascii_chars.json @@ -28,14 +28,14 @@ "knight": [" ", "/`)", " U ", "[.]"], "bishop": [" . ", "(\\)", " U ", "[.]"], "queen": [" . ", ").(", ").(", "[.]"], - "king": [" + ", ").(", ").(", "[.]"], + "king": [" + ", ").(", "|.|", "[.]"], "pawn": [" ", " o ", " U ", "[.]"] }, "dark": { "rook": [" ", "L-|", " U ", "[#]"], "knight": [" ", "/`)", " U ", "[#]"], "bishop": [" . ", "(\\)", " U ", "[#]"], - "king": [" + ", ")#(", ")#(", "[#]"], + "king": [" + ", ")#(", "|#|", "[#]"], "queen": [" . ", ")#(", ")#(", "[#]"], "pawn": [" ", " o ", " U ", "[#]"] } -- cgit v1.2.3-70-g09d2 From 3a6c603b0b12964d8a04ae4f78fb61bc094adf12 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 13 Jan 2023 17:02:11 -0700 Subject: Change some colors around --- lib/chessh/ssh/client/board/renderer.ex | 6 +++--- priv/ascii_chars.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'priv/ascii_chars.json') diff --git a/lib/chessh/ssh/client/board/renderer.ex b/lib/chessh/ssh/client/board/renderer.ex index 845e538..fa8648f 100644 --- a/lib/chessh/ssh/client/board/renderer.ex +++ b/lib/chessh/ssh/client/board/renderer.ex @@ -10,10 +10,10 @@ defmodule Chessh.SSH.Client.Board.Renderer do @tile_width 7 @tile_height 4 - @from_select_background ANSI.green_background() + @from_select_background ANSI.light_blue_background() @to_select_background ANSI.blue_background() - @dark_piece_color ANSI.red() - @light_piece_color ANSI.magenta() + @dark_piece_color ANSI.light_red() + @light_piece_color ANSI.light_magenta() def chess_board_height(), do: @chess_board_height def chess_board_width(), do: @chess_board_width diff --git a/priv/ascii_chars.json b/priv/ascii_chars.json index 1be31f8..fe7d12e 100644 --- a/priv/ascii_chars.json +++ b/priv/ascii_chars.json @@ -7,7 +7,7 @@ "e": [" __ ", "|_ ", "|__ "], "f": [" __ ", "|_ ", "| "], "g": [" __ ", "/ _ ", "\\__\\"], - "h": ["| |", "|__|", "| |"] + "h": [". .", "|__|", "| |"] }, "numbers": { "0": [" _ ", "| |", "|_|"], -- cgit v1.2.3-70-g09d2