From ce62dd6106f94473db919335cc49029421fde4ad Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 6 Jan 2023 17:02:56 -0700 Subject: [PATCH] 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(-) 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) - if piece_char == " ", do: String.Chars.to_string(char), else: piece_char + 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 @@ -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 ", "[#]"] } } }