Personal tools

Rubiks Cube

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Soon you won't need to carry a cube with you at all! All you'll need is your laptop!)
(link, polish)
Line 1: Line 1:
Here is a simple model for a rubics cube.
+
Here is a simple model for a [http://en.wikipedia.org/wiki/Rubik%27s_cube Rubik's Cube].
   
 
The basic idea is that you only need to keep track of the corners and edges. Each corner has three faces. Each edge has two faces. Keeping track of a face means telling where it was before any moves were made and where it is in the current state.
 
The basic idea is that you only need to keep track of the corners and edges. Each corner has three faces. Each edge has two faces. Keeping track of a face means telling where it was before any moves were made and where it is in the current state.
Line 25: Line 25:
 
data Edge = Face Face
 
data Edge = Face Face
   
data Corner = Face Face Face
+
data Corner = Face Face Face
   
 
data Face = Was Is
 
data Face = Was Is
   
data Was = R|L|U|D|F|B
+
data Was = R|L|U|D|F|B
 
data Is = R|L|U|D|F|B
 
data Is = R|L|U|D|F|B
 
</haskell>
 
</haskell>
  +
  +
[[Category:Code]]

Revision as of 00:17, 16 November 2006

Here is a simple model for a Rubik's Cube.

The basic idea is that you only need to keep track of the corners and edges. Each corner has three faces. Each edge has two faces. Keeping track of a face means telling where it was before any moves were made and where it is in the current state.

Choose, as a convention, the ordering, right, up, front. (Math/Physics folk: this is in anology to the "right hand rule" convention which assigns an ordering to the "x y and z" axes and determines that z will be "out of" rather than "into" the plane)

For example, the lower left front corner would be represented as (Left Left) (Down Down) (Front Front) before any moves are made Then, after a rotation about the Front face, the same corner, now in the right down front position would be represented as (Left Down) (Down Right) (Front Front).

#!/usr/bin/runhugs
module Main (main) where
main                    :: IO ()
main =  do putStr "Not your ordinary language"
 
data Cube = Edges Corners
 
type Edges = [Edge]
-- or Edges = Edge Edge Edge Edge Edge Edge Edge Edge Edge Edge Edge Edge
 
type Corners = [Corner]
 
data Edge = Face Face
 
data Corner = Face Face Face
 
data Face = Was Is
 
data Was = R|L|U|D|F|B
data Is =  R|L|U|D|F|B