Created
December 27, 2021 19:11
-
-
Save Rustem/27a2daf483da522b3fd99a4a568bf301 to your computer and use it in GitHub Desktop.
Super-Queens verify safe board layout
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
boolean canMove(int row, int col) { | |
// check we don't have any super0-queen on same row | |
for(int i = 0; i<col ;i++) { | |
if(board[row][i] == true) { | |
return false; | |
} | |
} | |
// check there is no already a queen not already on the left diag to the bottom of the (row, col) | |
for(int i = row, j = col; i>=0 && j >= 0; i --, j--) { | |
if(board[i][j] == true) { | |
return false; | |
} | |
} | |
// check there is no already a queen not already on the left diag to the topo of the (row, col) | |
for(int i = row, j = col; i < board.length && j >= 0; i ++, j--) { | |
if(board[i][j] == true) { | |
return false; | |
} | |
} | |
// check knight moves | |
for(int x = -2; x<=2; x++) { | |
for(int y = - 2; y<=2; y++) { | |
if(Math.abs(x - y) == 1) { | |
dx = row + x; | |
dy = col + y; | |
if(dx < 0 || dy < 0 || dx >= board.length || dy >= board.length) { | |
continue; | |
} | |
if(board[dx][dy] == true) { | |
return false | |
} | |
} | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment