Created
October 9, 2026 02:22
-
-
Save cgsdev0/3c94e76170b3bb117c583afd8a4458de to your computer and use it in GitHub Desktop.
battleship?
This file contains hidden or 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/local/bin/bash | |
| enable -f accept accept | |
| set -u | |
| shopt -s nullglob | |
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | |
| STATE_DIR="$PWD/state" # TODO: XDG_STATE or something? :) | |
| GRID_DIR="$STATE_DIR/grid" | |
| host=false | |
| while :; do | |
| case "$1" in | |
| -l|--listen) | |
| shift | |
| host=true | |
| ;; | |
| *) | |
| break | |
| ;; | |
| esac | |
| done | |
| if [[ -z "${ACTUAL_HOSTNAME-}" ]]; then | |
| ACTUAL_HOSTNAME="$1" | |
| fi | |
| if [[ -z "${PORT-}" ]]; then | |
| PORT="$2" | |
| fi | |
| # battleship [-l] [destination] [port] | |
| clear_line () { | |
| printf '\x1B[2K' | |
| } | |
| # fleet, length per ship | |
| GRID_SIZE=10 | |
| SHIPS=('destroyer' 'battleship' 'submarine' 'patrol boat' 'aircraft carrier' 'buoy') | |
| declare -A SHIP_SIZES | |
| SHIP_SIZES['destroyer']=3 | |
| SHIP_SIZES['battleship']=4 | |
| SHIP_SIZES['submarine']=3 | |
| SHIP_SIZES['patrol boat']=2 | |
| SHIP_SIZES['aircraft carrier']=5 | |
| SHIP_SIZES['buoy']=1 | |
| # Integer between 0 (inc) and $1 (exc), without bias. Max: 2^16 | |
| function randomi() { | |
| local top=$1 | |
| local r=$RANDOM | |
| local max=$((2**16 / top * top)) | |
| if [[ $r -ge $max ]]; then | |
| randomi $1 | |
| else | |
| echo "$((r % top))" | |
| fi | |
| } | |
| function place_ship() { | |
| local name=$1 | |
| local size=$2 | |
| local i | |
| local orientation=${ randomi 2; } | |
| local limited_coord=x | |
| if [[ $orientation == 1 ]]; then | |
| limited_coord=y | |
| fi | |
| [[ x != "$limited_coord" ]]; local x=${ randomi $((GRID_SIZE - ($? * size))); } | |
| [[ y != "$limited_coord" ]]; local y=${ randomi $((GRID_SIZE - ($? * size))); } | |
| if [[ $orientation == 1 ]]; then | |
| local -n iterating_coord=y | |
| else | |
| local -n iterating_coord=x | |
| fi | |
| for ((i=0; i<size; ++i)); do | |
| ((iterating_coord++)) | |
| if [[ -f "$GRID_DIR/$y/$x" ]]; then | |
| place_ship "$@" | |
| return | |
| fi | |
| done | |
| for ((i=0; i<size; ++i)); do | |
| fname="$GRID_DIR/$y/$x" | |
| if [[ -f "$fname" ]]; then | |
| >&2 echo "Invariant violated: writing to existing pixel state/$y/$x" | |
| exit 1 | |
| fi | |
| dname="${fname%/*}" | |
| mkdir -p "$dname" | |
| echo "$name" > "$fname" | |
| ((iterating_coord--)) | |
| done | |
| } | |
| function mkstate() { | |
| local name | |
| for name in "${SHIPS[@]}"; do | |
| place_ship "$name" ${SHIP_SIZES[$name]} | |
| done | |
| touch "$STATE_DIR/captains_log" | |
| touch "$STATE_DIR/admirals_log" | |
| } | |
| # Remove a file, and if it was the last in its dir, remove the parent. NON recursive. | |
| function rm_cleanup() { | |
| local fname="${1?}" | |
| rm "$fname" | |
| local dirname="${fname%/*}" | |
| local f | |
| for f in "$dirname"/*; do | |
| return | |
| done | |
| rmdir "$dirname" | |
| } | |
| function receive_bomb () { | |
| local x="$1" | |
| local y="$2" | |
| fname="$GRID_DIR/$y/$x" | |
| if [[ -f "$fname" ]] ; then | |
| name="$(< "$fname")" | |
| rm_cleanup "$fname" | |
| if grep -F -r -q "$name" "$GRID_DIR"; then | |
| echo "HIT $x,$y" | |
| else | |
| echo "SUNK $x,$y,$name" | |
| # find / -type f -print0 | shuf -zn 1 | xargs -0 rm -f | |
| fi | |
| else | |
| echo "MISS $x,$y" | |
| fi | |
| } | |
| if [[ ! -d "$STATE_DIR" ]]; then | |
| mkstate | |
| fi | |
| if $host; then | |
| accept -b "${ACTUAL_HOSTNAME?}" ${PORT?} | |
| else | |
| exec 3<> "/dev/tcp/${ACTUAL_HOSTNAME?}/${PORT?}" | |
| ACCEPT_FD=3 | |
| fi | |
| WATER=("~" "—" "~" "-") | |
| render() { | |
| # read the stuff | |
| local -a grid | |
| local -a grid2 | |
| local cmd args x y name | |
| while read -r cmd args; do | |
| case "$cmd" in | |
| HIT) | |
| IFS=, read x y _ <<< "$args" | |
| grid[y*GRID_SIZE+x]=" X" | |
| ;; | |
| SUNK) | |
| IFS=, read x y name <<< "$args" | |
| grid[y*GRID_SIZE+x]=" X" | |
| ;; | |
| MISS) | |
| IFS=, read x y <<< "$args" | |
| grid[y*GRID_SIZE+x]=" O" | |
| ;; | |
| esac | |
| done < "$STATE_DIR/captains_log" | |
| local row col | |
| for row in "$GRID_DIR/"*; do | |
| y=${row##*/} | |
| for col in "$row/"*; do | |
| x=${col##*/} | |
| grid2[y*GRID_SIZE+x]="▓▓" | |
| done | |
| done | |
| while read -r cmd args; do | |
| case "$cmd" in | |
| HIT) | |
| IFS=, read x y <<< "$args" | |
| grid2[y*GRID_SIZE+x]="░░" | |
| ;; | |
| SUNK) | |
| IFS=, read x y name <<< "$args" | |
| grid2[y*GRID_SIZE+x]="░░" | |
| ;; | |
| MISS) | |
| IFS=, read x y <<< "$args" | |
| grid2[y*GRID_SIZE+x]=" O" | |
| ;; | |
| esac | |
| done < "$STATE_DIR/admirals_log" | |
| printf '\x1B7' # save | |
| printf '\x1B[1;1H' # move to the top left | |
| # render opponent side | |
| for ((y=0; y<GRID_SIZE; ++y)); do | |
| clear_line | |
| for ((x=0; x<GRID_SIZE; ++x)); do | |
| water="${WATER[(y + x) % ${#WATER[@]}]}" | |
| printf "%s" "${grid[y*GRID_SIZE+x]:- $water}" | |
| done | |
| echo | |
| clear_line | |
| done | |
| echo | |
| clear_line | |
| # render our side | |
| for ((y=0; y<GRID_SIZE; ++y)); do | |
| clear_line | |
| for ((x=0; x<GRID_SIZE; ++x)); do | |
| water="${WATER[(y + x) % ${#WATER[@]}]}" | |
| printf "%s" "${grid2[y*GRID_SIZE+x]:- $water}" | |
| done | |
| echo | |
| clear_line | |
| done | |
| echo | |
| clear_line | |
| printf '\x1B8' # restore | |
| } | |
| # from here on out both flows are symmetrical | |
| #set -m | |
| main() { | |
| render | |
| while read src cmd; do | |
| case "$src" in | |
| local) | |
| ###>&2 echo "XXXX main GOT LOCAL COMMAND: $cmd, forwarding to $ACCEPT_FD" | |
| echo "FIRE $cmd" >&"$ACCEPT_FD" | |
| ;; | |
| remote) | |
| ####>&2 echo "XXXX main got REMOTE command: $cmd" | |
| # assume FIRE x,y | |
| # THIS HAS BEEN DEPRECATED: | |
| # but if you want you can do FIRE,x y | |
| read -r cmdcmd args <<<"$cmd" | |
| case "$cmdcmd" in | |
| FIRE,0) | |
| # backwards compatibility | |
| ;;& | |
| FIRE) | |
| IFS=',' read x y <<<"$args" | |
| receive_bomb $x $y | tee -a "$STATE_DIR/admirals_log" >&"$ACCEPT_FD" | |
| ;; | |
| HIT|MISS|SUNK) | |
| echo "$cmd" >> "$STATE_DIR/captains_log" | |
| ;; | |
| esac | |
| render | |
| ;; | |
| *) | |
| echo "who tf are you" >&2 | |
| esac | |
| done | |
| } | |
| # message: | |
| # <CMD> [ARGS]\r\n | |
| # actual goal: | |
| # | |
| # stretch goal: | |
| # EVAL <shell> | |
| remote_input() { | |
| local target="${1?}" | |
| exec 3>"$target" | |
| while read -r; do | |
| ###>&2 echo "XXXX Got remote input: $REPLY, writing to $target" | |
| echo "remote $REPLY" >&3 | |
| done | |
| } | |
| local_input() { | |
| #>&2 echo "XXXX local input loop started" | |
| local target="${1?}" | |
| exec 3>"$target" | |
| sleep 0.5 | |
| printf '\x1B['"$((GRID_SIZE*2 + 5))"';1H' | |
| while read -r -p "Your move: "; do | |
| #>&2 echo "XXXX GOT INPUT: $REPLY, sending to $target: local $REPLY" | |
| echo "local $REPLY" >&3 | |
| printf '\x1B['"$((GRID_SIZE*2 + 5))"';1H' | |
| clear_line | |
| done | |
| } | |
| # render | |
| # exit 0 | |
| fifodir="$(mktemp -d)" | |
| FIFO="$fifodir/game-loop" | |
| mkfifo "$FIFO" | |
| cleanup() { | |
| rm -rf "${fifodir?}" | |
| tput rmcup | |
| kill $(jobs -p) | |
| } | |
| trap "cleanup" EXIT | |
| tput smcup | |
| clear | |
| local_input "$FIFO" </dev/tty & | |
| remote_input "$FIFO" <&"$ACCEPT_FD" & | |
| main < "$FIFO" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment