mardi 21 avril 2015

Scheme, generating states to solve a 2x2x2 rubick's sphere

Vote count: 0

For a scheme assignment I need to write a brute force solver for a 2x2x2 rubick's sphere.

Half of the sphere can turn in positive/negative x, y and z directions.

My function genStates is meant to take in a state and generate 6 states from it saving the moves to reach those states in a second list (with matching indices to access later)

A state is a list of positions and directions ((1 1) (2 1) (3 1) (4 1) (5 3) (6 3) (7 3) (8 3)) and is coupled with a moves list ("x" "Y" "y" "Z").

My problem is that I can't seem to save the states and moves in the way I'd like.

(list
    (list
        ;all states
    )
    (list
        ;all moves
    )
)

My genStates function

(define (genStates n state moves)
    (if (= n 0)
    ;if last recurse just return current state
        (list state moves)
    ;else call helper
        (genHelper n (generateSuccessorStates state moves))

    )
)

My genHelper function

(define (genHelper n state)
    (list
        (append (append (append (append (append 
                                        (genStates (- n 1) (index (car state) 0) (index (cdr state) 0))
                                        (genStates (- n 1) (index (car state) 1) (index (index state 4) 0)))
                                (genStates (- n 1) (index (car state) 2) (index (cdr state) 2)))
                        (genStates (- n 1) (index (car state) 3) (index (cdr state) 3)))
                (genStates (- n 1) (index (car state) 4) (index (cdr state) 4)))
        (genStates (- n 1) (index (car state) 5) (index (cdr state) 5)))
    )
)

This method seems to return the states as ("x" "X" "y" "Y" "z" "Z") just before each 6 lines of states instead of as lists of moves at the end.

I have been able to produce the correct output using print statements and forking each time without appending to a list but I'm unsure how to collect the data from the recursive calls.

My generateSuccessorStates function

(define (generateSuccessorStates state prevMoves) 
(list
    (list
        (rotate "x" state)
        (rotate "X" state)
        (rotate "y" state)
        (rotate "Y" state)
        (rotate "z" state)
        (rotate "Z" state)
    )
    (list
        (append prevMoves '("x"))
        (append prevMoves '("X"))
        (append prevMoves '("y"))
        (append prevMoves '("Y"))
        (append prevMoves '("z"))
        (append prevMoves '("Z"))
    )
)
)

To any of my classmates who may likely see this, I am of course fine with you using my ideas but please don't just copy paste my code into your project. That will end badly for everyone involved.

asked 1 min ago



Scheme, generating states to solve a 2x2x2 rubick's sphere

Aucun commentaire:

Enregistrer un commentaire