CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.rubik
Interface Cube

All Superinterfaces:
java.lang.Cloneable
All Known Implementing Classes:
AbstractCube, Cube6, Cube7, PocketCube, ProfessorCube, RevengeCube, RubiksCube

public interface Cube
extends java.lang.Cloneable

The interface for objects which represent the state of a Rubik's Cube-like puzzle by describing the orientation and location of its parts.

A cube consists of corner parts, edge parts, side parts and a center part. Each part has an individual location and orientation. Parts can permute their location with other parts of the same type. A corner part can assume three different orientations, an edge part can assume two, a side part four, and the center part can assume 24 orientations.

The parts are arranged in layers. The layers are oriented on the X-, Y- and Z-axis. The number of layers is the same for all axes. The axes have at least two layers and can have up to 32 layers.

Version:
3.0 2009-01-01 Added support for twisting the cube by swiping over its faces.
2.0 2007-12-31 Renamed get...Side methods to get...Face methods to clarify their intent. Added getUnsolvedParts method.
1.0 April 6, 2006 Created.
Author:
Werner Randelshofer.

Method Summary
 void addCubeListener(CubeListener l)
          Adds a listener for CubeEvent's.
 java.lang.Object clone()
          Clones the cube.
 boolean equals(java.lang.Object o)
          Compares the Cube with another Cube for equality.
 int getCornerAt(int location)
          Gets the corner part at the specified location.
 int getCornerCount()
          Returns the number of corner parts.
 int getCornerLocation(int corner)
          Gets the location of the specified corner part.
 int[] getCornerLocations()
          Returns the locations of the corner parts.
 int getCornerOrientation(int corner)
          Gets the orientation of the specified corner part.
 int[] getCornerOrientations()
          Returns the orientations of the corner parts.
 int getCubeOrientation()
          Returns the orientation of the whole cube.
 int getEdgeAt(int location)
          Gets the edge part at the specified location.
 int getEdgeCount()
          Returns the number of edge parts.
 int getEdgeLocation(int edge)
          Gets the location of the specified edge part.
 int[] getEdgeLocations()
          Returns the locations of the edge parts.
 int getEdgeOrientation(int edge)
          Gets the orientation of the specified edge part.
 int[] getEdgeOrientations()
          Returns the orientations of the edge parts.
 int getLayerCount()
          Returns the number of layers on the x, y and z axis.
 int getPartAngle(int part, int orientation)
          Returns the angle which is clockwise for the specified part orientation.
 int getPartAt(int location)
          Returns the location of the specified part.
 int getPartAxis(int part, int orientation)
          Returns the current axis on which the orientation of the part lies.
 int getPartCount()
          Returns the number of cube parts.
 int getPartFace(int part, int orient)
          Returns the side at which the indicated orientation of the part is located.
 int getPartLayerMask(int part, int orientation)
          Returns the current layer mask on which the orientation of the part lies.
 int getPartLocation(int part)
          Returns the location of the specified part.
 int getPartOrientation(int part)
          Returns the orientation of the specified part.
 int getPartSwipeAngle(int part, int orientation, int swipeDirection)
          Returns the angle on which the orientation of the part can be swiped into the specified direction.
 int getPartSwipeAxis(int part, int orientation, int swipeDirection)
          Returns the axis on which the orientation of the part can be swiped into the specified direction.
 int getPartSwipeLayerMask(int part, int orientation, int swipeDirection)
          Returns the layer mask on which the orientation of the part can be swiped into the specified direction.
 int getPartType(int part)
          Returns the type of the specified part.
 int getSideAt(int location)
          Gets the side part at the specified location.
 int getSideCount()
          Returns the number of side parts.
 int getSideLocation(int side)
          Gets the location of the specified side part.
 int[] getSideLocations()
          Returns the locations of the side parts.
 int getSideOrientation(int side)
          Gets the orientation of the specified side part.
 int[] getSideOrientations()
          Returns the orientations of the side parts.
 int[] getUnsolvedParts()
          Returns an array of part ID's, for each part in this cube, which is not at its initial location or has not its initial orientation.
 int hashCode()
          Returns the hash code for the cube.
 boolean isSolved()
          Returns true if the cube is in its solved state.
 void removeCubeListener(CubeListener l)
          Removes a listener for CubeEvent's.
 void reset()
          Resets the cube to its solved state.
 void setCorners(int[] locations, int[] orientations)
          Sets the locations and orientations of the corner parts.
 void setEdges(int[] locations, int[] orientations)
          Sets the locations and orientations of the edge parts.
 void setQuiet(boolean b)
          Turns listener notification off.
 void setSides(int[] locations, int[] orientations)
          Sets the locations and orientations of the side parts.
 void setTo(Cube tx)
          Sets the orientations and locations of the parts of this Cube to the same values as in the specified Cube.
 void setToStickers(int[][] stickers)
          Sets the cube to a state where the faces of the parts map to the provided stickers array.
 int[][] toStickers()
          Converts the cube into a stickers array reflecting the current permutation of the cube.
 void transform(Cube that)
          Applies the permutation of the specified cube to this cube and fires a cubeChanged event.
 void transform(int axis, int layerMask, int angle)
          Transforms the cube and fires a cubeTwisted event.
 

Method Detail

reset

void reset()
Resets the cube to its solved state.

If the cube was not in its solved state, this method fires a cubeChanged event.

See Also:
isSolved()

isSolved

boolean isSolved()
Returns true if the cube is in its solved state.

In the solved state of the cube, each part i is at location i and has orientation 0.


setTo

void setTo(Cube tx)
Sets the orientations and locations of the parts of this Cube to the same values as in the specified Cube.

If this cube was different from the specified cube, this method fires a cubeChanged event.

Parameters:
tx - The cube to be set to.
Throws:
java.lang.IllegalArgumentException - if the specified cube has not the same layer count like this cube.

transform

void transform(int axis,
               int layerMask,
               int angle)
Transforms the cube and fires a cubeTwisted event.

Parameters:
axis - 0=x, 1=y, 2=z axis.
layerMask - A bitmask specifying the layers to be transformed. The size of the layer mask depends on the value returned by getLayerCount(axis). For a 3x3x3 cube, the layer mask has the following meaning: 7=rotate the whole cube;
1=twist slice near the axis (left, bottom, behind)
2=twist slice in the middle of the axis
4=twist slice far away from the axis (right, top, front)
angle - positive values=clockwise rotation
negative values=counterclockwise rotation
1=90 degrees
2=180 degrees
See Also:
getLayerCount()

transform

void transform(Cube that)
Applies the permutation of the specified cube to this cube and fires a cubeChanged event.

Parameters:
that - The cube to be applied to this cube object.
Throws:
java.lang.IllegalArgumentException - if the specified cube has not the same layer count like this cube.
See Also:
getLayerCount()

equals

boolean equals(java.lang.Object o)
Compares the Cube with another Cube for equality.

Two Cubes are equal, if they have the same number of layers, and if all their parts have the same orientations and locations.

Overrides:
equals in class java.lang.Object
Returns:
Returns true if the Cube is equal.
See Also:
hashCode()

hashCode

int hashCode()
Returns the hash code for the cube. Cubes which are equal must return the same hash code.

Overrides:
hashCode in class java.lang.Object
See Also:
equals(java.lang.Object)

addCubeListener

void addCubeListener(CubeListener l)
Adds a listener for CubeEvent's.


removeCubeListener

void removeCubeListener(CubeListener l)
Removes a listener for CubeEvent's.


setQuiet

void setQuiet(boolean b)
Turns listener notification off. Setting this to false fires a cubeChanged event.


getLayerCount

int getLayerCount()
Returns the number of layers on the x, y and z axis.

Returns:
A value from 2 through 32.

getCornerCount

int getCornerCount()
Returns the number of corner parts.

Returns:
Always returns 8.

getCornerLocations

int[] getCornerLocations()
Returns the locations of the corner parts. This array must be treated as read only.

Returns:
An array with a permutation of the numbers 0 through getCornerCount() - 1.

getCornerOrientations

int[] getCornerOrientations()
Returns the orientations of the corner parts. This array must be treated as read only.

Returns:
An array of orientations. The array elements can have the values 0, 1, and 2.

setCorners

void setCorners(int[] locations,
                int[] orientations)
Sets the locations and orientations of the corner parts.

Parameters:
locations - An array with a permutaton of the numbers 0 through getCornerCount() - 1.
orientations - An array with only the values 0, 1 and 2. The length of the array must be getCornerCount().

getCornerAt

int getCornerAt(int location)
Gets the corner part at the specified location.

Returns:
A value in the range from 0 through getCornerCount() - 1.

getCornerLocation

int getCornerLocation(int corner)
Gets the location of the specified corner part.

Returns:
A value in the range from 0 through getCornerCount() - 1.

getCornerOrientation

int getCornerOrientation(int corner)
Gets the orientation of the specified corner part.

Returns:
0, 1 or 2.

getEdgeCount

int getEdgeCount()
Returns the number of edge parts.

Returns:
0 for a cube with 2 layers, 12 for a cube with 3 layers, 24 for a cube with 4 layers, ... .

getEdgeLocations

int[] getEdgeLocations()
Returns the locations of the edge parts. This array must be treated as read only.

Returns:
An array with a permutation of the numbers 0 through getEdgeCount() - 1.

getEdgeOrientations

int[] getEdgeOrientations()
Returns the orientations of the edge parts. This array must be treated as read only.

Returns:
An array of orientations. The array elements can have the values 0, and 1.

setEdges

void setEdges(int[] locations,
              int[] orientations)
Sets the locations and orientations of the edge parts.

Parameters:
locations - An array with a permutaton of the numbers 0 through getEdgeCount() - 1.
orientations - An array with only the values 0, and 1. The length of the array must be getEdgeCount().

getEdgeAt

int getEdgeAt(int location)
Gets the edge part at the specified location.

Returns:
A value in the range from 0 through getEdgeCount() - 1.

getEdgeLocation

int getEdgeLocation(int edge)
Gets the location of the specified edge part.

Returns:
A value in the range from 0 through getEdgeCount() - 1.

getEdgeOrientation

int getEdgeOrientation(int edge)
Gets the orientation of the specified edge part.

Returns:
0 or 1.

getSideCount

int getSideCount()
Returns the number of side parts.

Returns:
0 for a cube with 2 layers, 6 for a cube with 3 layers, 24 for a cube with 4 layers, ... .

getSideLocations

int[] getSideLocations()
Returns the locations of the side parts. This array must be treated as read only.

Returns:
An array with a permutation of the numbers 0 through getSideCount() - 1.

getSideOrientations

int[] getSideOrientations()
Returns the orientations of the side parts. This array must be treated as read only.

Returns:
An array of orientations. The array elements can have the values 0, 1, 2 and 3.

setSides

void setSides(int[] locations,
              int[] orientations)
Sets the locations and orientations of the side parts.

Parameters:
locations - An array with a permutaton of the numbers 0 through getSideCount() - 1.
orientations - An array with only the values 0, 1, 2, and 3. The length of the array must be getSideCount().

getSideAt

int getSideAt(int location)
Gets the side part at the specified location.

Returns:
A value in the range from 0 through getSideCount() - 1.

getSideLocation

int getSideLocation(int side)
Gets the location of the specified side part.

Returns:
A value in the range from 0 through getSideCount() - 1.

getSideOrientation

int getSideOrientation(int side)
Gets the orientation of the specified side part.

Returns:
0, 1, 2 or 3.

getPartFace

int getPartFace(int part,
                int orient)
Returns the side at which the indicated orientation of the part is located.

Returns:
A value in the range from 0 through 5.

getPartOrientation

int getPartOrientation(int part)
Returns the orientation of the specified part.

Returns:
Returns 0..2 for a corner part, 0..1 for an edge part, 0..3 for a side part, 0..24 for the center part.

getPartLocation

int getPartLocation(int part)
Returns the location of the specified part.

Returns:
Returns the location for a corner part, location + getCornerPartCount() for an edge part, location + getCornerPartCount() + getEdgePartCount() for a side part, getPartCount() - 1 for the center part.

getPartAxis

int getPartAxis(int part,
                int orientation)
Returns the current axis on which the orientation of the part lies.

Returns:
0 for the x-axis, 1 for the y-axis, 2 for the z-axis. -1 if the part lies on none or multiple axis (the center part).

getPartLayerMask

int getPartLayerMask(int part,
                     int orientation)
Returns the current layer mask on which the orientation of the part lies. Returns 0 if no mask can be determined (the center part).

Returns:
2^layer number

getPartAngle

int getPartAngle(int part,
                 int orientation)
Returns the angle which is clockwise for the specified part orientation. Returns 1 or -1. Returns 0 if the direction can not be determined (the center part).


getPartSwipeAxis

int getPartSwipeAxis(int part,
                     int orientation,
                     int swipeDirection)
Returns the axis on which the orientation of the part can be swiped into the specified direction.

Parameters:
part - The part index.
orientation - The orientation of the part where swiping is performed.
swipeDirection - The direction of the swipe. 0=up,1=right,2=down,4=left.
Returns:
0 for the x-axis, 1 for the y-axis, 2 for the z-axis. -1 if the part lies on none or multiple axis (the center part).

getPartSwipeLayerMask

int getPartSwipeLayerMask(int part,
                          int orientation,
                          int swipeDirection)
Returns the layer mask on which the orientation of the part can be swiped into the specified direction. Returns 0 if no mask can be determined (the center part).

Parameters:
part - The part index.
orientation - The orientation of the part where swiping is performed.
swipeDirection - The direction of the swipe. 0=up,1=right,2=down,4=left.
Returns:
2^layer number

getPartSwipeAngle

int getPartSwipeAngle(int part,
                      int orientation,
                      int swipeDirection)
Returns the angle on which the orientation of the part can be swiped into the specified direction.

Parameters:
part - The part index.
orientation - The orientation of the part where swiping is performed.
swipeDirection - The direction of the swipe. 0=up,1=right,2=down,4=left.
Returns:
Returns 1 or -1. Returns 0 if the direction can not be determined (the center part).

getPartType

int getPartType(int part)
Returns the type of the specified part.

Returns:
0 for corner parts, 1 for edge parts, 2 for side parts, 3 for the center part.

getPartAt

int getPartAt(int location)
Returns the location of the specified part.


getCubeOrientation

int getCubeOrientation()
Returns the orientation of the whole cube. FIXME - Replace this by getCenterOrientation.

Returns:
The orientation of the cube, or -1 if the orientation can not be determined.

clone

java.lang.Object clone()
Clones the cube.


getPartCount

int getPartCount()
Returns the number of cube parts.


getUnsolvedParts

int[] getUnsolvedParts()
Returns an array of part ID's, for each part in this cube, which is not at its initial location or has not its initial orientation.


toStickers

int[][] toStickers()
Converts the cube into a stickers array reflecting the current permutation of the cube.

Returns:
Array of stickers: int[6][getLayerCount()*getLayerCount()]. Same structure as in method setStickers().
See Also:
setToStickers(int[][])

setToStickers

void setToStickers(int[][] stickers)
Sets the cube to a state where the faces of the parts map to the provided stickers array.

Parameters:
stickers - Sticker array to be reused. The method creates a new array if this parameter is null.

(c) Werner Randelshofer.
All rights reserved.