Homokozó:Programozás/3
.3 Use random: Write 1st game

Let’s get a random number from the first character of a random string by the special \" character introducer of the printf.

sh -c 'c="a"; printf "%d\n" \""$c"' # ASCII code of the a character

Test #3 a

1: Write a random number sequence

2: Write a very simple passphrase generator

 and be a true *h4ckeR* :)

 Write your 1st game – prototype 

Write your first game!

 Regards:
 Generate a challenge for the player by using 2 or more random numbers and least one arithmetical operator
 Create a fancy output :-) – also we can use it to web output in a CGI program if we know the basics of the SGML markup languages (for example the very minimal basics of the HTML language recommendations)
 Store your upstream of your source code in a separated directory. The Upstream means: files of your source code.

 The case pattern matching 

Finally in the programming we have various things to do for different character patterns. We can use the meta-characters (!*?[) to create our wishful character pattern as we have seen in the previous chapters. This is the case command. In short:

sh -c 'a="Love is in the air"; case $a in (*"s i"*) printf "1\n";; esac' # :)

Shorter:

sh -c 'a="asdf"; case $a in *d*) printf "1\n";; esac'

More complex example:

sh -c 'for a in ABDEF "I love abugida\!"; do case $a in *BC*|*BD*) printf "BC or BD\n";; *abugida*) printf "Not BC or BD but I Love \e[3mabugida!\e[0m :)\n"; esac; done'

 :)

Test #3 b

1: Write a game that uses character pattern matching in this way.

2: Prepare a well-commented version of your program

Look into this short part of the latter 3-sort game'' (see the commented version of this game-proto in the appendix). How can it generate unique prng-sequences? Is it better to use ALL pseudo random numbers from the string? :-)

read -p "Type the max value up to 12 and press Enter: " n;
i=0; zps=""; while [ $i -le $n ]; do read s;
# c=`printf "%c" "$s"`; cc=`printf "%d" \""$c"`;
# v=`printf "%03o" "$((cc%(n+1)))"`;
# in the case above we are using all characters
# from the pseudo random string!! # :)
v=`printf "%03o" "$((${#s}%(n+1)))"`;
 if [ $i -le $n ]; then
 case $zps in *"$v "*) ;; *) zps="$zps$v "; i=$((i+1));; esac;
 fi;
done</dev/urandom; osca="";
for w in $zps; do
if [ $w != "000" ]; then d=`printf "%d" "$w"`; osca="$osca$d "
else osca="$osca. ";
fi;
i=$((i+1)); done;
printf "$osca\n";

3: See the all 3-sort – game-proto – program in the appendix. How can you sport it into a more gripping puzzle game? :))