smithers.twisty
Class RubiksCube

java.lang.Object
  extended by smithers.twisty.RubiksCube
All Implemented Interfaces:
AnimatedTwistyPuzzle, StickerSwappingTwistyPuzzle, TwistyPuzzle

public class RubiksCube
extends java.lang.Object
implements StickerSwappingTwistyPuzzle, AnimatedTwistyPuzzle

Represents a Rubik's Cube, or similar larger/smaller twisty cube puzzle.


Constructor Summary
RubiksCube()
          Constructs a new 3x3x3 Rubik's Cube.
RubiksCube(int side)
          Constructs a new cube with a given side length.
 
Method Summary
 void advanceAnimation(double time)
          Advances the animation by a certain amount of time.
 int getColor(int index)
          Gets the colour of the specified sticker.
 int getLocation(int index)
          Gets the location of a sticker.
 Polygon3D getPolygon(int index)
          Gets the 3-d polygon associated with the specified sticker.
 int getRotation(int index)
          Gets the rotation of a sticker.
 double getScale()
          Returns the absolute scale of the 3-d model of the puzzle.
 int getSticker(int loc)
          Gets the index of the sticker in a specific location.
 int getStickerRotation(int loc)
          Gets the rotation of the sticker in a location.
 Twist getTwist(int index, int dir)
          Gets the twist instance determined by the sticker clicked on, and the directon to twist.
 Twist getTwist(int face, int d1, int d2, int dir)
          Gets the twist instance defined by the parameters.
 boolean isAnimating()
          Returns true iff the puzzle is partway through a twist.
 boolean isSolved()
          Works out whether this puzzle is in the solved state.
 boolean isSolvedAligned()
          Determines whether the puzzle is solved, including checking the orientation of each sticker.
 void reset()
          Resets the cube.
 void scramble()
          Scrambles the cube.
 int side()
          Gets the side length.
 int side(int top, int side, int x, int y)
          Looks at one of the faces next to face top as if it is forwards and face top is on top, and gives the index of the sticker in position (x,y).
 int stickerCount()
          Gets the number of stickers on the puzzle.
 java.lang.String toString()
          Returns a String representation of this cube.
 void twist(int face, int d1, int d2, int dir)
          Twists face face of the cube from layer d1 (inclusive) to layer d2 (exclusive).
 void twist(Twist twist)
          Applies a twist.
 void twist(Twist twist, double time)
          Twists the puzzle over a specified amount of time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RubiksCube

public RubiksCube()
Constructs a new 3x3x3 Rubik's Cube.


RubiksCube

public RubiksCube(int side)
Constructs a new cube with a given side length.

Parameters:
side - the length of the side
Method Detail

side

public int side()
Gets the side length.

Returns:
the length of a side

stickerCount

public int stickerCount()
Gets the number of stickers on the puzzle.

Specified by:
stickerCount in interface TwistyPuzzle
Returns:
the number of stickers

getColor

public int getColor(int index)
Gets the colour of the specified sticker. The colour returned is specified as ARGB.

Specified by:
getColor in interface TwistyPuzzle
Parameters:
index - the sticker index
Returns:
the colour of the sticker

getScale

public double getScale()
Returns the absolute scale of the 3-d model of the puzzle. Essentially, this should return the recommended 3-d width of the viewing window.

Specified by:
getScale in interface TwistyPuzzle
Returns:
the scale

getPolygon

public Polygon3D getPolygon(int index)
Gets the 3-d polygon associated with the specified sticker.

Specified by:
getPolygon in interface TwistyPuzzle
Parameters:
index - the sticker index
Returns:
the polygon of the sticker

getLocation

public int getLocation(int index)
Gets the location of a sticker. If the puzzle is partway through a twist, this returns the value before the twist.

Specified by:
getLocation in interface StickerSwappingTwistyPuzzle
Parameters:
index - the sticker index
Returns:
the location index
See Also:
getSticker(int), getRotation(int)

getSticker

public int getSticker(int loc)
Gets the index of the sticker in a specific location. If the puzzle is partway through a twist, this returns the value before the twist.

Specified by:
getSticker in interface StickerSwappingTwistyPuzzle
Parameters:
loc - the location index
Returns:
the sticker index
See Also:
getLocation(int), getStickerRotation(int)

getRotation

public int getRotation(int index)
Gets the rotation of a sticker. The value returned is the rotation of sticker index from 0 to 3. If the puzzle is partway through a twist, this returns the value before the twist.

Parameters:
index - the sticker index
Returns:
the rotation of the sticker
See Also:
getLocation(int)

getStickerRotation

public int getStickerRotation(int loc)
Gets the rotation of the sticker in a location. The value returned is the rotation of the sticker in in a location loc from 0 to 3. If the puzzle is partway through a twist, this returns the value before the twist.

Parameters:
loc - the location of the sticker
Returns:
the rotation of the sticker
See Also:
getSticker(int)

getTwist

public Twist getTwist(int face,
                      int d1,
                      int d2,
                      int dir)
Gets the twist instance defined by the parameters. See twist(int,int,int,int) for the description of the twist.

Parameters:
face - the face to twist parallel to
d1 - the first layer to turn
d2 - the first layer not to turn
dir - the direction
Throws:
java.lang.IndexOutOfBoundsException - if d1<0 || d2<d1 || s<d2

getTwist

public Twist getTwist(int index,
                      int dir)
Gets the twist instance determined by the sticker clicked on, and the directon to twist. This should always return an instance which will be accepted by TwistyPuzzle.twist(smithers.twisty.Twist) (i.e. will not throw ClassCastException, although the move may not be possible). The dir argument should be: 0 for anticlockwise, 1 for clockwise or 2 for a half twist.

Specified by:
getTwist in interface TwistyPuzzle
Parameters:
index - the sticker which was clicked on
dir - the directon
Returns:
the twist instance

twist

public void twist(Twist twist)
Applies a twist.

Specified by:
twist in interface TwistyPuzzle
Parameters:
twist - the twist

twist

public void twist(Twist twist,
                  double time)
Twists the puzzle over a specified amount of time. Using a time of 0 should apply the twist immediately. Supplying a negative number for the time allows the puzzle to choose the time based on the twist using the absolute value of the time as a maximum.

Specified by:
twist in interface AnimatedTwistyPuzzle
Parameters:
twist - the twist to make
time - the time to take

isAnimating

public boolean isAnimating()
Returns true iff the puzzle is partway through a twist. If this method returns true, any modifier in TwistyPuzzle may fail with an AnimatingException.

Specified by:
isAnimating in interface AnimatedTwistyPuzzle
Returns:
true iff the puzzle is animating a twist

advanceAnimation

public void advanceAnimation(double time)
Advances the animation by a certain amount of time.

Specified by:
advanceAnimation in interface AnimatedTwistyPuzzle
Parameters:
time - the amount of time to advance

twist

public final void twist(int face,
                        int d1,
                        int d2,
                        int dir)
Twists face face of the cube from layer d1 (inclusive) to layer d2 (exclusive). Notice of course that the face itself is turned iff d1==0 && d2>0, the opposite face is turned iff d2==s && d1<s. Nothing happens if d1==d2. The direction of the turn is specified by dir (mod 4): 1 = anticlockwise, 2 = half twist, 3 = clockwise.

Parameters:
face - the face to twist parallel to
d1 - the first layer to turn
d2 - the first layer not to turn
dir - the direction
Throws:
java.lang.IndexOutOfBoundsException - if d1<0 || d2<d1 || s<d2
AnimatingException - if the puzzle is part way through an animation

side

public final int side(int top,
                      int side,
                      int x,
                      int y)
Looks at one of the faces next to face top as if it is forwards and face top is on top, and gives the index of the sticker in position (x,y). x and y are interpreted modulo the side length. The side is counted around anticlockwise by side from the one on the left edge of face top.

Parameters:
top - the face to consider as the 'top'
side - which side of the top face to look next to
x - the position on the face counting parallel to 'top'
y - the position on the face counting away from 'top'
Returns:
the index of the required sticker

scramble

public void scramble()
Scrambles the cube.

Specified by:
scramble in interface TwistyPuzzle

reset

public void reset()
Resets the cube.


toString

public java.lang.String toString()
Returns a String representation of this cube.

Overrides:
toString in class java.lang.Object
Returns:
a String representing this cube

isSolved

public boolean isSolved()
Works out whether this puzzle is in the solved state.
This puzzle is considered solved if each face of the cube contains stickers of only one colour.

Specified by:
isSolved in interface TwistyPuzzle
Returns:
true iff the puzzle is solved

isSolvedAligned

public boolean isSolvedAligned()
Determines whether the puzzle is solved, including checking the orientation of each sticker.

Returns:
true if the puzzle is solved with all stickers correctly oriented