1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class PieceCell extends Cell { public void flip() { Cell buffer = getNeighbour(NORTHEAST); setNeighbour(NORTHEAST, getNeighbour(NORTHWEST)); setNeighbour(NORTHWEST, buffer); buffer = getNeighbour(EAST); setNeighbour(EAST, getNeighbour(WEST)); setNeighbour(WEST, buffer); buffer = getNeighbour(SOUTHEAST); setNeighbour(SOUTHEAST, getNeighbour(SOUTHWEST)); setNeighbour(SOUTHWEST, buffer); } public void rotate() { // Clockwise rotation Cell eastNeighbour = getNeighbour(EAST); setNeighbour(EAST, getNeighbour(NORTHEAST)); setNeighbour(NORTHEAST, getNeighbour(NORTHWEST)); setNeighbour(NORTHWEST, getNeighbour(WEST)); setNeighbour(WEST, getNeighbour(SOUTHWEST)); setNeighbour(SOUTHWEST, getNeighbour(SOUTHEAST)); setNeighbour(SOUTHEAST, eastNeighbour); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class Piece { public static final int NUMBEROFCELLS = 5; public static final int NUMBEROFPERMUTATIONS = 12; private PieceCell[] pieceCells = new PieceCell[NUMBEROFCELLS]; private int currentPermutation = 0; private void rotatePiece() { for (int i = 0; i < NUMBEROFCELLS; i++) { pieceCells.rotate(); } } private void flipPiece() { for (int i = 0; i < NUMBEROFCELLS; i++) { pieceCells.flip(); } } public Piece nextPermutation() { if (currentPermutation == NUMBEROFPERMUTATIONS) currentPermutation = 0; switch (currentPermutation%6) { case 0: // Flip after every 6 rotations flipPiece(); break; default: rotatePiece(); break; } currentPermutation++; return this; } public void resetProcessed() { for (int i = 0; i < NUMBEROFCELLS; i++) { pieceCells.setProcessed(false); } } //Getters and setters have been omitted } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |