Langton's ant: Difference between revisions

→‎{{header|UNIX Shell}}: Add implementation
(→‎{{header|VBA}}: Correct chirality; randomize initial heading; print results even if ant walks off the grid.)
(→‎{{header|UNIX Shell}}: Add implementation)
Line 8,437:
</syntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
{{works with|Z Shell}}
This uses the terminal dimensions, so shrink the font and make the window big; should be at least 80 lines by 100 columns.
<syntaxhighlight lang="sh">function main {
typeset -i width=$(tput cols)
typeset -i height=$(tput lines)
typeset -a grid
typeset -i i
for (( i=0; i<height; ++i )); do
grid+=("$(printf "%${width}s" ' ')")
done
typeset -i x=$(( width / 2 )) y=$(( height / 2 ))
(( dx=0, dy = 1 - 2*RANDOM%2 ))
if (( RANDOM % 2 )); then
(( dy=0, dx = 1 - 2*RANDOM%2 ))
fi
printf '\e[H';
printf '%s\n' "${grid[@]}"
tput civis
while (( x>=0 && x < width && y >=0 && y< height)); do
(( i=y ))
if [[ -n $ZSH_VERSION ]]; then
(( i += 1 ))
fi
ch=${grid[i]:$x:1}
if [[ $ch == '#' ]]; then
(( t=dx, dx=dy, dy=-t ))
grid[i]=${grid[i]:0:$x}' '${grid[i]:$x+1}
else
(( t=dx, dx=-dy, dy=t ))
grid[i]=${grid[i]:0:$x}'#'${grid[i]:$x+1}
fi
(( x += dx, y += dy ))
tput cup $y 0 && printf '%s' "${grid[i]}"
done
tput cnorm
read line
return
}
 
main "$@"</syntaxhighlight>
{{Out}}
Example final state:
<pre> ## # #
### # #
# ## ##
## ## #
# # # ##
## # ### #
# # ### ##
## # ####
# # ### ##
## # ####
# # ### ##
## # ####
# # ### ##
## # ####
# # ### ##
## # ####
# # ### ##
## # ####
## # # ### ##
## ## ## # ####
# # # ## # # # ### ##
### ### # # ## # ####
# #### ## # # # ### ##
## ## # ####
## ## ## # # ### ##
#### # ## # ## # ####
##### ## ### ## ## ## # # ### ##
# # # ## # ## #### ## ## # ####
#### ### #### #### ## # # # ### ##
### # # ## ## # ## ## # ####
#### ## ## ## # # # # # # ### ##
# ## # # ## ## # # # ## # ####
# #### ## # # ######## # # # # # ### ##
## ## # ## # # ## ## # # # ## ## ## # ####
# # # # # # # # ## ## # ##### # # ### ##
## #### ## # #### # # ## ## # ## # ####
## ########## ## ##### # #### # # # # ### ##
# # # ## # # # # ## ##### ## # # ## # ####
# ## # ## # # ##### # ##### # # # ### ##
# ## ### ### # # ## # ## ###### # # ## # ####
# #### ## # # ### ### ## ## ## # ## # # ### ##
## # ## # ######### ## #### # ## # ####
# # # #### # ########### ## # # # ### ##
# # #### #### ## # ## ### ## # ####
# # # # # ## ## # # ### # # # # ### ##
# ## #### #### ##### ## ## # ## # # ####
## # # ## ### # ### # # ## # ### ##
# # ### ## # ## #### # # # # # ####
# #### ## # # ### ## ## ## ### ##
# ## ## ### # ## # ### ## # # ####
## # ## ## # ## ## # ##
## # #### # # # ### ## # ###
## ### ##### # ## ## ## # # ## ### #
# # ### # ###### ## ## #### # # ###
# # # ### # #### # # ##### # # #
### ### # # ### ## # ## ###
## ## # # # ## #### ## ### # ##
### ## ## # # # # # #
# ### # # ## #
# # # # #
# ## ## # #
## # #### ##
## ############ #
</pre>
=={{header|VBA}}==
 
1,479

edits