3 store snake body coordinates in a single array
source | link

Bash: 537 533533 507 characters

C=$COLUMNS;L=$LINES;D=-1;c=9;r=9;x=(9);y=1;c=9;r=9;z=(9\ 9);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in $"${!x[@]z[@]};do((f==x[i]&&g==y[i]))&&b;done;t";do [[ $f\ $g = $i ]]&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in $"${!x[@]z[@]};do((c==x[i]&&r==y[i]))&&break";do [[ $c\ $r = $i ]]&&break 2;done
t ${x[-1]} ${y[z[-1]} \ ;t $c $r X
x=z=($c$c\ ${x[@]::l});y=($r $"${y[@]z[@]::l}")
done
echo $'\e[2J\e[H'Score: $l

As it uses the $COLUMNS and $LINES shell variables, it must be run sourced: . snake.sh. The snake can be controlled with the w/a/s/d keys.

I know, it can be easily reduced to 520493 characters by using clear to clear the screen, but I prefer to keep it pure bash, without using any external tool.

Bash: 537 533 characters

C=$COLUMNS;L=$LINES;D=-1;c=9;r=9;x=(9);y=(9);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in ${!x[@]};do((f==x[i]&&g==y[i]))&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in ${!x[@]};do((c==x[i]&&r==y[i]))&&break 2;done
t ${x[-1]} ${y[-1]} \ ;t $c $r X
x=($c ${x[@]::l});y=($r ${y[@]::l})
done
echo $'\e[2J\e[H'Score: $l

As it uses the $COLUMNS and $LINES shell variables, it must be run sourced: . snake.sh. The snake can be controlled with the w/a/s/d keys.

I know, it can be easily reduced to 520 characters by using clear to clear the screen, but I prefer to keep it pure bash, without using any external tool.

Bash: 537 533 507 characters

C=$COLUMNS;L=$LINES;D=-1;c=9;r=9;z=(9\ 9);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in "${z[@]}";do [[ $f\ $g = $i ]]&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in "${z[@]}";do [[ $c\ $r = $i ]]&&break 2;done
t ${z[-1]} \ ;t $c $r X
z=($c\ $r "${z[@]::l}")
done
echo $'\e[2J\e[H'Score: $l

As it uses the $COLUMNS and $LINES shell variables, it must be run sourced: . snake.sh. The snake can be controlled with the w/a/s/d keys.

I know, it can be easily reduced to 493 characters by using clear to clear the screen, but I prefer to keep it pure bash, without using any external tool.

2 initializing snake at coordinates 9:9 instead of 10:10 saves 4*1 characters
source | link

Bash: 537537 533 characters

C=$COLUMNS;L=$LINES;D=-1;c=10;r=10;x=1;c=9;r=9;x=($c9);y=($c9);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in ${!x[@]};do((f==x[i]&&g==y[i]))&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in ${!x[@]};do((c==x[i]&&r==y[i]))&&break 2;done
t ${x[-1]} ${y[-1]} \ ;t $c $r X
x=($c ${x[@]::l});y=($r ${y[@]::l})
done
echo $'\e[2J\e[H'Score: $l

As it uses the $COLUMNS and $LINES shell variables, it must be run sourced: . snake.sh. The snake can be controlled with the w/a/s/d keys.

I know, it can be easily reduced to 524520 characters by using clear to clear the screen, but I prefer to keep it pure bash, without using any external tool.

Bash: 537 characters

C=$COLUMNS;L=$LINES;D=-1;c=10;r=10;x=($c);y=($c);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in ${!x[@]};do((f==x[i]&&g==y[i]))&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in ${!x[@]};do((c==x[i]&&r==y[i]))&&break 2;done
t ${x[-1]} ${y[-1]} \ ;t $c $r X
x=($c ${x[@]::l});y=($r ${y[@]::l})
done
echo $'\e[2J\e[H'Score: $l

As it uses the $COLUMNS and $LINES shell variables, it must be run sourced: . snake.sh. The snake can be controlled with the w/a/s/d keys.

I know, it can be easily reduced to 524 characters by using clear to clear the screen, but I prefer to keep it pure bash, without using any external tool.

Bash: 537 533 characters

C=$COLUMNS;L=$LINES;D=-1;c=9;r=9;x=(9);y=(9);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in ${!x[@]};do((f==x[i]&&g==y[i]))&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in ${!x[@]};do((c==x[i]&&r==y[i]))&&break 2;done
t ${x[-1]} ${y[-1]} \ ;t $c $r X
x=($c ${x[@]::l});y=($r ${y[@]::l})
done
echo $'\e[2J\e[H'Score: $l

As it uses the $COLUMNS and $LINES shell variables, it must be run sourced: . snake.sh. The snake can be controlled with the w/a/s/d keys.

I know, it can be easily reduced to 520 characters by using clear to clear the screen, but I prefer to keep it pure bash, without using any external tool.

1
source | link

Bash: 537 characters

C=$COLUMNS;L=$LINES;D=-1;c=10;r=10;x=($c);y=($c);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in ${!x[@]};do((f==x[i]&&g==y[i]))&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in ${!x[@]};do((c==x[i]&&r==y[i]))&&break 2;done
t ${x[-1]} ${y[-1]} \ ;t $c $r X
x=($c ${x[@]::l});y=($r ${y[@]::l})
done
echo $'\e[2J\e[H'Score: $l

As it uses the $COLUMNS and $LINES shell variables, it must be run sourced: . snake.sh. The snake can be controlled with the w/a/s/d keys.

I know, it can be easily reduced to 524 characters by using clear to clear the screen, but I prefer to keep it pure bash, without using any external tool.