Skip to content

Instantly share code, notes, and snippets.

@pumpkincouture
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save pumpkincouture/9cf7d288ef558153ca63 to your computer and use it in GitHub Desktop.

Select an option

Save pumpkincouture/9cf7d288ef558153ca63 to your computer and use it in GitHub Desktop.
private String checkColumns() {
int boardLength = board.getMatrix().length;
int maxIndexValue = boardLength - 1;
String[][] lineValues = new String[boardLength][boardLength];
for (int columnIndex = 0; columnIndex < boardLength; columnIndex++) {
for (int index = 0; index < boardLength; index++) {
lineValues[columnIndex][index] = getValueAtIndex(columnIndex, maxIndexValue - index);
}
}
for (int i = 0; i < boardLength; i++) {
if (isWinningCombo(lineValues[i])) {
return lineValues[i][0];
}
}
return "";
}
private String checkRows() {
int boardLength = board.getMatrix().length;
int maxIndexValue = boardLength - 1;
String[][] lineValues = new String[boardLength][boardLength];
for (int rowIndex = 0; rowIndex < boardLength; rowIndex++) {
for (int index = 0; index < boardLength; index++) {
lineValues[rowIndex][index] = getValueAtIndex(maxIndexValue - index, rowIndex);
}
}
for (int i = 0; i < boardLength; i++) {
if (isWinningCombo(lineValues[i])) {
return lineValues[i][0];
}
}
return "";
}
private String checkRightDiagonal() {
int boardLength = board.getMatrix().length;
int maxIndexValue = boardLength - 1;
String[] lineValues = new String[boardLength];
for (int index = 0; index < boardLength; index++) {
lineValues[index] = getValueAtIndex(index, maxIndexValue - index);
}
if (isWinningCombo(lineValues)) {
return lineValues[0];
}
return "";
}
private String checkLeftDiagonal() {
int boardLength = board.getMatrix().length;
String[] lineValues = new String[boardLength];
for (int index = 0; index < boardLength; index++) {
lineValues[index] = getValueAtIndex(index, index);
}
if (isWinningCombo(lineValues)) {
return lineValues[0];
}
return "";
}
private String getValueAtIndex(int startingIndex, int endingIndex) {
return board.getMatrix()[startingIndex][endingIndex];
}
private boolean isWinningCombo(String[] lineValues) {
String first = lineValues[0];
for (String value : lineValues) {
if (value != first || value == board.getBoardEmptySpace()) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment