-
-
Save Asthriona/e2ecff2231e4ceb4ef626225c6abffe5 to your computer and use it in GitHub Desktop.
house builder pattern in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function house_builder() { | |
# floors,rooms,has_garage | |
echo "0,0,0" | |
} | |
function set_field() { | |
local f r g | |
IFS=, read f r g | |
printf -v "$1" "%s" "$2" | |
echo "$f,$r,$g" | |
} | |
function floors() { | |
set_field f $1 | |
} | |
function rooms() { | |
set_field r $1 | |
} | |
function has_garage() { | |
set_field g $1 | |
} | |
function build() { | |
local f r g h i j k | |
IFS=, read f r g | |
local rpf=$((r / f)) | |
local remainder=$((r % f)) | |
local w=$((rpf + remainder)) | |
local gws=" " | |
local h=$((w+1+f)) | |
k=0 | |
function garage() { | |
[[ $((h-k)) -eq 3 ]] && gws=' __ ' | |
[[ $((h-k)) -eq 2 ]] && gws="/__\\" | |
[[ $((h-k)) -eq 1 ]] && gws='|🚘|' | |
[[ $g == "true" ]] && echo -n "$gws" | |
} | |
# every good house starts with a roof | |
for (( i=0; i<=w; i++,k++ )); do | |
garage | |
printf '%*s' $((w - i)) | |
[[ $i -eq 0 ]] && echo "_" | |
[[ $i -ne 0 ]] && echo -n "/" | |
printf -v pad '%*s' $((i*2-1)) | |
[[ $i -eq $w ]] && pad=${pad// /_} | |
[[ $i -ne 0 ]] && echo -n "$pad" | |
[[ $i -ne 0 ]] && echo "\\" | |
done | |
# main part of the house | |
for (( i=0; i<f; i++,k++ )); do | |
garage | |
for (( j=0; j<w; j++ )); do | |
filled=_ | |
[[ $i -ne 0 ]] && [[ $j -ge $rpf ]] && filled="#" | |
echo -n "|$filled" | |
done | |
echo "|" | |
done | |
} | |
# example usage | |
# house_builder \ | |
# | floors 2 \ | |
# | rooms 3 \ | |
# | has_garage true \ | |
# | build |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment