Reputation: 8023
I have the following segment of a java file:
Integer x; Integer y; Face facing;
enum Rotate { Clockwise, Anticlockwise };
enum Face { East, North, West, South };
and am having trouble figuring out how to implement a function to change the Face of an object (i.e. the direction that the object is facing).
The function begins as follows
private void rotateIt(Rotate rotateIt) {
{
I have begun using the switch statement as follows ( below text is inside braces above ):
switch (facing)
case North : ...?;
case West : ...?;
case East : ...?;
case South : ...?;
I'd like to use the Clockwise
enumeration to turn it from East
to South
etc. and Anticlockwise
to do the reverse IYGWIM.
Upvotes: 0
Views: 1173
Reputation: 67380
switch (facing) {
case North : facing=rotateIt==Rotate.Clockwise?Face.East:Face.West; break;
case West : facing=rotateIt==Rotate.Clockwise?Face.North:Face.South; break;
case East : facing=rotateIt==Rotate.Clockwise?Face.South:Face.North; break;
case South : facing=rotateIt==Rotate.Clockwise?Face.West:Face.East; break;
}
I should get a large percent of your grade retroactively!
Upvotes: 4
Reputation: 533492
Another option is to use the enum to do the work.
enum Face {
North, East, South, West; // must be clockwise order.
}
enum Rotate {
private static final Face[] FACES = Face.values();
Clockwise {
public Face rotate(Face face) {
return FACES[(ordinal()+1)%FACES.length];
}
},
Anticlockwise {
public Face rotate(Face face) {
return FACES[(ordinal()+FACES.length-1)%FACES.length];
}
}
public abstract Face rotate(Face face);
};
facing = rotateIt.rotate(facing);
Upvotes: 1
Reputation: 37435
I would implement rotate as a function of the Face orientation:
enum RotationDirection { Clockwise, CounterClockwise };
enum Face {
East, North, West, South ;
Face rotate(RotationDirection direction ) {
int tick = (direction == RotationDirection.Clockwise)?-1:1;
int index = this.ordinal()+tick ;
int length = Face.values().length-1;
if (index <0) index = length;
if (index >length) index = 0;
return Face.values()[index];
}
Then you can do things like:
Face face = Face.North;
face.rotate(RotationDirection.Clockwise); // East
face.rotate(RotationDirection.CounterClockwise); //West
This code makes use of the seldom used 'ordinal' property of Enums. It therefore requires that the values are in a logical turning order e.g. (east, north, west, south)
Upvotes: 2
Reputation: 59576
You are starting fine. Here is a more complete version of what you have to do regarding enum manipulation:
public void RotateIt(Rotate toRotate, Face facing) {
switch (facing) {
case North:
// Include code to rotate from north
break;
case West:
// Include code to rotate from west
break;
case East:
// Include code to rotate from east
break;
default: // South
// Include code to rotate from south
break;
}
}
Of course, this code could be optimized, but it gives you an idea of how to handle enums
in switch
statements.
Upvotes: 0
Reputation: 12205
case North:
{
if(rotateIt == Rotate.Clockwise)
facing = Face.EAST
else
facing = Face.WEST
break;
}
and so on...
Upvotes: 0