Created
March 14, 2012 14:36
-
-
Save thomasbrus/2036909 to your computer and use it in GitHub Desktop.
Tic Tac Toe board rotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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