I'm making a Rubik's Cube scramble generator, and the turns can't be side by side or else it's useless. I'm trying to stop it from doing the following:
Displaying the same string twice, i.e R2 R2
Displaying the same kind of string twice, i.e R2 R
Displaying the inverse of the string next to it, i.e R Ri

Here's the code:

turns=( R Ri 
    L Li
    U Ui 
    D Di
    F Fi
    B Bi
    R2 L2 
    U2 D2
    F2 B2

for ((i=1; i<=$1; i++))
do {
echo ${turnArray[*]}

An example of what I want the result to turn out to be:
Script yields: D2 B Bi Bi Ri Fi R2 D Li U F2 Ri R2 R2 F2
*s around what need to be changed: D2 *B Bi Bi* Ri Fi R2 D Li U F2 Ri *R2 R2* F2

(In case you're wondering, the lowercase i in the turn means "inverted".)


turns=(R Ri L Li U Ui D Di F Fi B Bi R2 L2 U2 D2 F2 B2)

declare -A possible
for i in ${turns[@]}; do
        possible[$i]=$(printf "%s\n" "${turns[@]}" | grep -v ${i:0:1})

for ((i=0; i<$1; i++)); do
        j=$(shuf -n 1 -e $next)

echo ${turnArray[*]}
  • Precalculate next possible moves in an assoc array possible.
  • Use shuf to choose next move from arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.