0

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:

    #!/bin/bash
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 {
     turnArray[$i]=${turns[$((RANDOM%18))]}
}
done
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".)

0
#!/bin/bash

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})
done

next=${turns[*]}
for ((i=0; i<$1; i++)); do
        j=$(shuf -n 1 -e $next)
        turnArray[$i]=$j
        next=${possible[$j]}
done

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.