Skip to content

Instantly share code, notes, and snippets.

@martinmidtsund
Created February 25, 2013 13:47
Show Gist options
  • Save martinmidtsund/5029900 to your computer and use it in GitHub Desktop.
Save martinmidtsund/5029900 to your computer and use it in GitHub Desktop.
// Uses line-checking to find all the conflicts(violations) for the given position
private static int conflicts(Point position, int[][] board) {
int nrOfConflicts = 0;
int row = position.x;
int col = position.y;
// Conflicts below position
int runs = 1;
for(int i=(row+1); i<board.length; i++) {
// Vertical down
if(board[i][col] == 1) {
nrOfConflicts += 1;
}
// Diagonal below left
if((col-runs) >= 0 && board[i][col-runs] == 1 ) {
nrOfConflicts += 1;
}
// Diagonal below right
if((col+runs) < board.length && board[i][col+runs] == 1) {
nrOfConflicts += 1;
}
runs++;
}
// Conflicts above position
runs = 1;
for(int i=(row-1); i>=0; i--) {
// Vertical up
if(board[i][col] == 1) {
nrOfConflicts += 1;
}
// Diagonal above left
if((col-runs) >= 0 && board[i][col-runs] == 1) {
nrOfConflicts += 1;
}
// Diagonal above right
if((col+runs) < board.length && board[i][col+runs] == 1) {
nrOfConflicts += 1;
}
runs++;
}
return nrOfConflicts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment