Johannes91
Member
- Joined
- Mar 28, 2006
- Messages
- 1,341
I don't know much Ruby, but one thing stands out: I think it would make sense to pass scramble_length as a parameter. And at least in Python it's more efficient (and IMO more elegant) to use an array and join it once instead of repeatedly concatenating strings:
FWIW, here's a Haskell scrambler I wrote a while ago:
Code:
scramble = []
...
scramble.push(turns[axis].rand + variants.rand)
...
scramble.join(" ")
FWIW, here's a Haskell scrambler I wrote a while ago:
Code:
getScramble :: Int -> IO FlatAlg
getScramble = liftM fromJust . getGeneralScramble (map (flip (,) [1,2,3]) [U,D,F,B,R,L])
getGeneralScramble :: [(Face,[Int])] -> Int -> IO (Maybe FlatAlg)
getGeneralScramble xs n = do maybeFaces <- getRandomFaces (map fst $ filter (not . null . snd) xs) n
case maybeFaces of
Nothing -> return Nothing
Just faces -> do suffixes <- mapM (liftM (Suffix . fromJust) . pick . fromJust . flip lookup xs) faces
return $ Just $ FlatAlg $ zipWith FaceMove faces suffixes
getRandomFaces :: [Face] -> Int -> IO (Maybe [Face])
getRandomFaces fcs = flip rec []
where rec :: Int -> [Face] -> IO (Maybe [Face])
rec 0 acc = return $ Just acc
rec n acc = do maybeFace <- pick (filter p fcs)
case maybeFace of
Nothing -> return Nothing
Just x -> rec (n-1) (x:acc)
where p = case acc of
[] -> const True
(x:[]) -> (/= x)
(x:y:_) | isSameSliceFace x y -> not . isSameSliceFace x
| otherwise -> (/= x)
pick :: [a] -> IO (Maybe a)
pick [] = return Nothing
pick xs = liftM (Just . (xs !!)) $ randomRIO (0, length xs - 1)