Add alt code to 1985/shapiro
This alt code allows one to resize the maze. Obviously if one uses -D with a value that's not a number it will fail to compile. If it's <= 0 || >= 5000 it will set to the default of 39. The try.alt.sh script plays with this, first selecting a random number (in range $RANDOM % 38) to compile with and then run. After that it runs in an infinite loop, exiting only if input includes non-digit characters. A value >= 5000 sets it to the default of 39 (in the code).
This commit is contained in:
1 parent
816761dc0d
commit
b3b4027b73
6 files changed
+116
-4
No files matched your search
Executable
+47
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# try.alt.sh - demonstrate IOCCC winner 1985/shapiro alt code
|
||||
#
|
||||
|
||||
# make sure CC is set so that when we do make CC="$CC" it isn't empty. Doing it
|
||||
# this way allows us to have the user specify a different compiler in an easy
|
||||
# way.
|
||||
if [[ -z "$CC" ]]; then
|
||||
CC="cc"
|
||||
fi
|
||||
|
||||
# run with default first
|
||||
make clobber CC="$CC" alt >/dev/null || exit 1
|
||||
./shapiro.alt
|
||||
|
||||
i=0
|
||||
# while i < 5 randomly select a size and recompile it and run
|
||||
while ((i < 5)); do
|
||||
# get random value in range RANDOM % 38. <=0 sets to 39 in the code but
|
||||
# since 1 is kind of pointless so we set it to 2 if it's < 2.
|
||||
C=$((RANDOM % 38))
|
||||
if [[ "$C" -lt 2 ]]; then
|
||||
C=2
|
||||
fi
|
||||
|
||||
read -r -n 1 -p "Press any key to set to $C: "
|
||||
make clobber CC="$CC" CDEFINE="-DC=$C" alt >/dev/null
|
||||
./shapiro.alt
|
||||
((i++))
|
||||
done
|
||||
|
||||
# while input is only numbers recompile the alt code with that value. If number
|
||||
# is <=0 it sets to 39 in the code but here if one enters a negative number it
|
||||
# will exit due to the '-'. 1 is rather pointless but we don't check for
|
||||
# it either.
|
||||
while :; do
|
||||
read -r -p "Enter a value (any non-digit = quit): "
|
||||
# ^-- SC2143 (style): Use grep -q instead of comparing output with [ -n .. ].
|
||||
# shellcheck disable=SC2143
|
||||
if [[ $(echo "$REPLY" | grep -E '\<[0-9]+\>') ]]; then
|
||||
make clobber CC="$CC" CDEFINE="-DC=\"$REPLY\"" alt >/dev/null
|
||||
./shapiro.alt
|
||||
else
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
Reference in new issue
Block a user