Skip to content

Instantly share code, notes, and snippets.

@DDynamic
Last active February 16, 2020 04:09
Show Gist options
  • Save DDynamic/cd559361ddaf1a4fef51ab16bc547cc7 to your computer and use it in GitHub Desktop.
Save DDynamic/cd559361ddaf1a4fef51ab16bc547cc7 to your computer and use it in GitHub Desktop.
Non Attacking Queens Template
/*
Non Attacking Queens Template
Copyright (c) 2019 Dylan Seidt <dylan.seidt@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
public class NonAttackingQueens{
// The chessboard
private int[][] board;
// The size of our chessboard. This sets an 8 x 8 chessboard.
final int NUM_QUEENS = 8;
// Constructor, Initialize the board
public NonAttackingQueens() {
board = new int[NUM_QUEENS][NUM_QUEENS];
}
// Determine if a queen can be placed in the given row and column.
// Return true for valid placement, otherwise return false
private boolean canPlace(int row, int col) {
// If there is already a queen in this row, return false
// To accomplish this, loop through all of the columns in the row.
// If there is a queen in any of the columns, return false
// If there is already a queen in this column, return false
// To accomplish this, loop through all of the rows in this column.
// If there is a queen in any of the rows, return false
// Check up-left diagonal. Make a loop that starts at the row and col parameters.
// Continue running the loop until the row AND column are greater than or equal to zero.
// Decrement the row and column for each iteration. If there is a queen in the current row and column, return false.
// Check up-right diagonal. Make a loop that starts at the row and col parameters.
// Continue running the loop until the row AND column are greater than or equal to zero.
// Decrement the row and increment column for each iteration. If there is a queen in the current row and column, return false.
// If we made it to this point, it means that this row and col is a valid placement. Return true.
}
// Run our recursive solve function.
public void solve(int row) {
// If the row is less than NUM_QUEENS, continue checking. Otherwise, print a solution.
// To continue checking, iterate over all columns in the row.
// If you can place in the current column and row, place a queen in that position.
// Recurse by calling solve with the next row. Then, remove the queen to backtrack.
}
// Print out a solution
public void printSolution() {
// Make two loops that iterate through the rows and columns.
}
public static void main(String[] args) {
// initialize the NonAttackingQueens class and solve
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment