Skip to content

Instantly share code, notes, and snippets.

@mniip
Created March 25, 2015 02:33
Show Gist options
  • Save mniip/12b76a2fe78b0b2ca349 to your computer and use it in GitHub Desktop.
Save mniip/12b76a2fe78b0b2ca349 to your computer and use it in GitHub Desktop.
A 2048 clone for those who can't afford a desktop environment. Runs in sh!
#!/bin/sh
print_cell()
{
if [ "$1" = 0 ]; then
echo -en " "
else
if [ "$1" -ge 128 ]; then
echo -en '\x1B[33m'
elif [ "$1" -ge 16 ]; then
echo -en '\x1B[31m'
fi
pad="$1"
while [ "${#pad}" != 4 ]; do
pad=" $pad"
done
echo -en "$pad"'\x1B[0m'
fi
}
print_row()
{
print_cell "$1"
echo -en ' '
print_cell "$2"
echo -en ' '
print_cell "$3"
echo -en ' '
print_cell "$4"
echo
}
print_board()
{
print_row "$1" "$2" "$3" "$4"
print_row "$5" "$6" "$7" "$8"
print_row "$9" "${10}" "${11}" "${12}"
print_row "${13}" "${14}" "${15}" "${16}"
}
clear_board()
{
echo -en '\x1B[1A\x1B[2K'
echo -en '\x1B[1A\x1B[2K'
echo -en '\x1B[1A\x1B[2K'
echo -en '\x1B[1A\x1B[2K'
}
precollapse_row()
{
while [ "$#" != 0 ]; do
if [ "$1" != 0 ]; then
echo "$1"
fi
shift
done
}
collapse_row()
{
while [ "$#" != 0 ]; do
a="$1"
shift
if [ "$a" = "$1" ]; then
echo "$(($a*2))"
shift
elif [ "$a" != 0 ]; then
echo "$a"
fi
done
}
adjust_four()
{
echo "$1" "$2" "$3" "$4"
}
collapse_board()
{
adjust_four $(collapse_row $(precollapse_row "$1" "$2" "$3" "$4")) 0 0 0 0
adjust_four $(collapse_row $(precollapse_row "$5" "$6" "$7" "$8")) 0 0 0 0
adjust_four $(collapse_row $(precollapse_row "$9" "${10}" "${11}" "${12}")) 0 0 0 0
adjust_four $(collapse_row $(precollapse_row "${13}" "${14}" "${15}" "${16}")) 0 0 0 0
}
flip_board()
{
echo "$4" "$3" "$2" "$1"
echo "$8" "$7" "$6" "$5"
echo "${12}" "${11}" "${10}" "$9"
echo "${16}" "${15}" "${14}" "${13}"
}
transpose_board()
{
echo "$1" "$5" "$9" "${13}"
echo "$2" "$6" "${10}" "${14}"
echo "$3" "$7" "${11}" "${15}"
echo "$4" "$8" "${12}" "${16}"
}
id()
{
echo "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" "${16}"
}
count_slots()
{
count=0
while [ "$#" != 0 ]; do
if [ "$1" = 0 ]; then
count="$(($count+1))"
fi
shift
done
echo "$count"
}
randomly_place()
{
slots="$(count_slots "$@")"
while [ "$#" != 0 ]; do
if [ "$1" = 0 ]; then
if [ "$(($RANDOM%$slots))" = 0 ]; then
if [ "$(($RANDOM%10))" == 0 ]; then
echo 4
else
echo 2
fi
shift
echo "$@"
return
else
slots="$(($slots-1))"
echo "$1"
fi
else
echo "$1"
fi
shift
done
}
check_win()
{
while [ "$#" != 0 ]; do
if [ "$1" = 2048 ]; then
return 0
fi
shift
done
return 1
}
board="$(randomly_place 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)"
print_board $board
trap "stty echo; exit" EXIT INT TERM
stty -echo
while true; do
board="$(randomly_place $board)"
clear_board
print_board $board
if check_win $board; then
echo "You won"
exit
fi
board_left="$(collapse_board $board)"
board_right="$(flip_board $(collapse_board $(flip_board $board)))"
board_up="$(transpose_board $(collapse_board $(transpose_board $board)))"
board_down="$(transpose_board $(flip_board $(collapse_board $(flip_board $(transpose_board $board)))))"
if [ "$(id $board)" = "$(id $board_left)" -a "$(id $board)" = "$(id $board_right)" -a "$(id $board)" = "$(id $board_up)" -a "$(id $board)" = "$(id $board_down)" ]; then
echo "You lost"
exit
fi
old_board="$board"
while [ "$(id $board)" = "$(id $old_board)" ]; do
read -n1 c
if [ "$c" == $'\e' ]; then
read -n1 c
if [ "$c" == '[' ]; then
read -n1 direction
case "$direction" in
A) board="$board_up" ;;
B) board="$board_down" ;;
C) board="$board_right" ;;
D) board="$board_left" ;;
esac
fi
fi
done
done
@hjpotter92
Copy link

hjpotter92 commented Jul 3, 2016

When executing, it just says:

2048.sh: 177: read: Illegal option -n
2048.sh: 178: [: unexpected operator

Works with bash/zsh though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment