Created
March 1, 2016 20:05
-
-
Save lamerocheg/894174fca882179f8e98 to your computer and use it in GitHub Desktop.
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 by.iba.gomel; | |
import java.util.Scanner; | |
/** | |
* This class contains third dimensions array of char and tools for work with this array. | |
* | |
*/ | |
public class CharArray { | |
private final char[][][] arrayOfChar; | |
private static final int LENGTH = 3; | |
private static final int LENGTH_OF_CONSTRUCTOR_STRING = 27; | |
/** | |
* Constructor for third dimension char array. Constructor use string with length = 27. | |
* | |
* @param data | |
* data for your array. must be with length 27 | |
*/ | |
public CharArray(final String data) { | |
final char[][][] tmpArray = new char[CharArray.LENGTH][CharArray.LENGTH][CharArray.LENGTH]; | |
final char[] baseCharArray = data.toCharArray(); | |
for (int i = 0; i < CharArray.LENGTH; i++) { | |
for (int j = 0; j < CharArray.LENGTH; j++) { | |
for (int k = 0; k < CharArray.LENGTH; k++) { | |
tmpArray[i][j][k] = baseCharArray[(i * CharArray.LENGTH * CharArray.LENGTH) | |
+ (j * CharArray.LENGTH) + k]; | |
} | |
} | |
} | |
this.arrayOfChar = tmpArray; | |
} | |
/** | |
* this method application use for scan data to array application. | |
* | |
* @return String with length 27 | |
*/ | |
public static String getDataForArray() { | |
final StringBuilder tmp = new StringBuilder(); | |
final Scanner scanner = new Scanner(System.in); | |
while (tmp.length() < CharArray.LENGTH_OF_CONSTRUCTOR_STRING) { | |
tmp.append(scanner.nextLine()); | |
} | |
scanner.close(); | |
return tmp.substring(0, CharArray.LENGTH_OF_CONSTRUCTOR_STRING); | |
} | |
private boolean isSatisfies(final int startX, final int startY, final int startZ, | |
final int stepX, final int stepY, final int stepZ) { | |
int x = startX; | |
int y = startY; | |
int z = startZ; | |
int sx = stepX; | |
int sy = stepY; | |
int sz = stepZ; | |
final char checkable = this.arrayOfChar[x][y][z]; | |
boolean flag = true; | |
while (true) { | |
try { | |
x += sx; | |
y += sy; | |
z += sz; | |
if (this.arrayOfChar[x][y][z] != checkable) { | |
return false; | |
} | |
} catch (final ArrayIndexOutOfBoundsException e) {// NOSONAR | |
if (flag) { | |
sx *= -1; | |
sy *= -1; | |
sz *= -1; | |
x = startX; | |
y = startY; | |
z = startZ; | |
flag = false; | |
} else { | |
break; | |
} | |
} | |
} | |
return true; | |
} | |
/** | |
* this method check row of array . | |
* | |
* @param x | |
* coordinate | |
* @param y | |
* coordinate | |
* @return result of isSatisfies | |
*/ | |
public final boolean checkRow(final int x, final int y) { | |
return this.isSatisfies(x, y, 0, 0, 0, 1); | |
} | |
/** | |
* this method check subrow of array. | |
* | |
* @param x | |
* coordinates | |
* @param z | |
* coordinates | |
* @return result of isSatisfies | |
*/ | |
public final boolean checkSubrow(final int x, final int z) { | |
return this.isSatisfies(x, 0, z, 0, 1, 0); | |
} | |
/** | |
* this method check column of array. | |
* | |
* @param y | |
* coordinates | |
* @param z | |
* coordinates | |
* @return result of isSatisfies | |
*/ | |
public final boolean checkColumn(final int y, final int z) { | |
return this.isSatisfies(0, y, z, 1, 0, 0); | |
} | |
/** | |
* this method check diagonal of array with coordinates [x][y][y] and with variable dimension | |
* step. for example , if we will check [1][1][1] with dimension step (1 , 1) it will be main | |
* diagonal of array. | |
* | |
* @param x | |
* coordinate | |
* @param y | |
* coordinate | |
* @param z | |
* coordinate | |
* @param dimensionXStep | |
* step of your coordinate in 1 dimension | |
* @param dimensionYStep | |
* step of your coordinate in 2 dimension | |
* @return result of isSatisfies | |
*/ | |
public final boolean checkDiagonal(final int x, final int y, final int z, | |
final int dimensionXStep, final int dimensionYStep) { | |
return this.isSatisfies(x, y, z, dimensionXStep, dimensionYStep, 1); | |
} | |
/** | |
* check every row, subrow , column and 2 main diagonal . if something ill be satisfied return | |
* true | |
* | |
* @return true if something similar false if nothing similar | |
* | |
*/ | |
public final boolean checkEverything() { | |
for (int i = 0; i < (CharArray.LENGTH * CharArray.LENGTH); i++) { | |
if (this.checkColumn(i / CharArray.LENGTH, i % CharArray.LENGTH) | |
|| this.checkRow(i / CharArray.LENGTH, i % CharArray.LENGTH) | |
|| this.checkSubrow(i / CharArray.LENGTH, i % CharArray.LENGTH)) { | |
return true; | |
} | |
} | |
if (this.checkDiagonal(1, 1, 1, 1, 1) || this.checkDiagonal(1, 1, 1, -1, -1)) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment