Created
February 25, 2013 13:47
-
-
Save martinmidtsund/5029900 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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