Skip to content

Instantly share code, notes, and snippets.

@thomasbrus
Created March 14, 2012 14:36
Show Gist options
  • Save thomasbrus/2036909 to your computer and use it in GitHub Desktop.
Save thomasbrus/2036909 to your computer and use it in GitHub Desktop.
Tic Tac Toe board rotation
package tictactoe.transformation;
import tictactoe.*;
public class Rotation implements Transformation {
private final int times;
public Rotation(int times, boolean clockwise) {
// Rotating more than four times is superfluous
times = times % 4;
// Take care of counterclockwise rotation
if (!clockwise) {
times = (4 - times) % 4;
}
// Assign it
this.times = times;
}
public Rotation(int times) {
this(times, true);
}
public Rotation() {
this(1);
}
public int[] calculate(int column, int row, int dimension) {
int[] position = { column, row };
for (int n = 0; n < this.times; n++) {
position = (new FlipHorizontal()).calculate(position[0], position[1], dimension);
position = (new Transpose()).calculate(position[0], position[1], dimension);
}
return position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment