Skyscraper puzzle solver in JavaMax heap in JavaSudoku Week-End Challenge - Brute-Force Recursive...

Contest math problem about crossing out numbers in the table

Using only 1s, make 29 with the minimum number of digits

What's the most convenient time of year to end the world?

What flying insects could re-enter the Earth's atmosphere from space without burning up?

Why Normality assumption in linear regression

Would these multi-classing house rules cause unintended problems?

Jumping Numbers

Do authors have to be politically correct in article-writing?

Checking for the existence of multiple directories

What is the most triangles you can make from a capital "H" and 3 straight lines?

Prove the support of a real function is countable

How to prevent cleaner from hanging my lock screen in Ubuntu 16.04

Can we use the stored gravitational potential energy of a building to produce power?

Dilemma of explaining to interviewer that he is the reason for declining second interview

If I delete my router's history can my ISP still provide it to my parents?

Difference between two quite-similar Terminal commands

How do I say "Brexit" in Latin?

Can an insurance company drop you after receiving a bill and refusing to pay?

Why did this image turn out darker?

What is this metal M-shaped device for?

Broken patches on a road

Grade 10 Analytic Geometry Question 23- Incredibly hard

Cat is tipping over bed-side lamps during the night

How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?



Skyscraper puzzle solver in Java


Max heap in JavaSudoku Week-End Challenge - Brute-Force Recursive solverSymmetric SquareLocker Puzzle in JavaSums of some array elementsFill 2D array recursivelyJava OOP HomeWorkImplementation of a Generic Singly Linked List in JavaSudoku puzzle solving algorithm that uses a rule-based approach to narrow the depth searchBinary Puzzle Solver - 10000 questions













2












$begingroup$


For an assignment, I have created a solver for the Skyscraper Puzzle:




Each puzzle consists of an N×N grid with some clues along its sides. The object is to place a skyscraper in each square, with a height between 1 and N, so that no two skyscrapers in a row or column have the same number of floors. In addition, the number of visible skyscrapers, as viewed from the direction of each clue, is equal to the value of the clue. Note that higher skyscrapers block the view of lower skyscrapers located behind them.




The puzzle and the ordinances are hard coded into the program. I would like feedback on efficiency and readability. Any and all suggestions are welcome!



SkyscraperPuzzle.java



package Skyscraper;


/**
* Write a description of class SkyscraperPuzzle here.
*
* @author David White
* @version (a version number or a date)
*/

public class SkyscraperPuzzle {

private int[][] puzzle = {
{1, 4, 3, 2},
{2, 3, 4, 1},
{4, 2, 1, 3},
{3, 1, 2, 4}
};
private Ordinance[][] ords = {
{new Ordinance(3, Direction.NORTH), new Ordinance(1, Direction.NORTH), new Ordinance(2, Direction.NORTH), new Ordinance(3, Direction.NORTH)}, //NORTH
{new Ordinance(2, Direction.SOUTH), new Ordinance(4, Direction.SOUTH), new Ordinance(2, Direction.SOUTH), new Ordinance(1, Direction.SOUTH)}, //SOUTH
{new Ordinance(3, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(1, Direction.EAST)}, //EAST
{new Ordinance(2, Direction.WEST), new Ordinance(3, Direction.WEST), new Ordinance(1, Direction.WEST), new Ordinance(2, Direction.WEST)} //WEST
};

/**
* Write a constructor to initialize and populate the puzzle and list of ordinances
* so that the puzzle may be checked to see if it is (correctly) solved.
*/
public SkyscraperPuzzle() {
//Puzzle is populated above
//Ords is populated above
}

/**
* returns the puzzle
*/
public int[][] getPuzzle() {
return this.puzzle;
}

/**
* Retuns a 1D array that respresents row r of puzzle
*/
public int[] getRow(int row) {
return puzzle[row];
}

/**
* Write a method that returns a 1D array that represents column c of puzzle
*/
public int[] getColumn(int column) { // row by column
int[] arr = new int[this.puzzle.length];
for(int i = 0; i < this.puzzle.length; i++) {
arr[i] = this.puzzle[i][column];
}
return arr;
}

/**
* Write a method that returns true if the 1D array parameter does not contain duplicates
*/
public boolean noDuplicates(int[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
for(int j = i + 1; j < arr.length - 1; j++) {
if(arr[i] == arr[j]) {
return false;
}
}
}
return true;
}

/**
* Write a method that returns true if each value from 1 to the number of rows (or columns, they
* are the same), is contained in the 1D array
*
* if 5x5
* then 1 2 3 4 5 should all be in array
*/
public boolean containsOneToN(int[] arr) {
int[] array = new int[arr.length];
int count = 0;
for(int i = 0; i < array.length; i++) {
array[i] = i + 1;
}
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < array.length; j++) {
if(arr[i] == array[j]) {
count += 1;
array[j] = 0;
}
}
}
return count == arr.length;
}

/**
* Write a method that traverses the 2D array, ords, and returns true if each and
* every Ordinance requirement is met
*/
public boolean meetsOrdinances() {
for(int i = 0; i < this.puzzle.length; i++) {
/* Yet to be implemented */
}
return false; //used to allow compile
}

/**
* Write a method that returns true if the puzzle is a correctly solved Skyscraper puzzle
*/
public boolean isSolved() {
/* Each row and column */
for(int i = 0; i < this.puzzle.length; i++) {
if(!noDuplicates(this.puzzle[i]) || !noDuplicates(getColumn(i))) {
return false;
}
if(!containsOneToN(this.puzzle[i]) || !containsOneToN(getColumn(i))) {
return false;
}
if(!meetsOrdinances()) {
return false;
}
}
return true;
}
}


Direction.java



package Skyscraper;


/**
* Enumeration class Direction - write a description of the enum class here
*
* @author David White
* @version (version number or date here)
*/
public enum Direction {
NORTH, /* Above the 2D array */
SOUTH, /* Below the 2D array */
EAST, /* Right of the 2D array */
WEST /* Left of the 2D array */
}


Ordinance.java



package Skyscraper;


/**
* Write a description of class Ordinance here.
*
* @author David White
* @version (a version number or a date)
*/
public class Ordinance {

private int num;
private Direction direction;

public Ordinance(int num, Direction direction) {
this.num = num;
this.direction = direction;
}

public int getNum() {
return this.num;
}

public Direction getDirection() {
return this.direction;
}
}




The PuzzleTest.java is used to make sure the methods I've written work in the way they're intended to.



PuzzleTest.java



package Skyscraper;


/**
* Write a description of class PuzzleTest here.
*
* @author David White
* @version (a version number or a date)
*/
public class PuzzleTest {

public static void main(String[] args) {

SkyscraperPuzzle p = new SkyscraperPuzzle();
int[][] puzzle = p.getPuzzle();

/*
* Outputs entire puzzle
*/
for(int[] row : puzzle) {
for(int item : row) {
System.out.print(item + " ");
}
System.out.println();
}
System.out.println();

/*
* Test getColumn method
* Output should be
* 1
* 4
* 7
* Status: Successful
*/
int[] col = p.getColumn(0);
for(int i = 0; i < col.length; i++) {
System.out.println(col[i]);
}
System.out.println();

/*
* Test noDuplicates method
* Output should be
* true
* Status: Successful
*/
int[] arr = {1, 2, 3, 4, 5};
System.out.println(p.noDuplicates(arr));
System.out.println();
/*
* Test containsOneToN method
* Output should be
* true
* Status: Successful
*/
int[] arr2 = {1, 2, 3, 4, 5};
System.out.println(p.containsOneToN(arr2));

/*
* Test meetsOrdinances method
* Output should be
* true
* Status: To be tested
*/

}

}









share|improve this question











$endgroup$








  • 1




    $begingroup$
    @200_success Yes, just edited the post to add the description.
    $endgroup$
    – David White
    4 hours ago






  • 1




    $begingroup$
    How can this code ever work if meetsOrdinances says Yet to be implemented?
    $endgroup$
    – Roland Illig
    4 hours ago










  • $begingroup$
    A puzzle solver by definition gets only the clues and then figures out the arrangement of the skyscrapers. I cannot find any code that does this.
    $endgroup$
    – Roland Illig
    4 hours ago












  • $begingroup$
    @RolandIllig This assignment in particular has both the skyscrapers and ordinances hardcoded, and just checks if the ordinances match the skyscraper. No user input is involved. And I'm just inquiring about the state of the code now, assuming that meetOrdinances works as expected.
    $endgroup$
    – David White
    3 hours ago
















2












$begingroup$


For an assignment, I have created a solver for the Skyscraper Puzzle:




Each puzzle consists of an N×N grid with some clues along its sides. The object is to place a skyscraper in each square, with a height between 1 and N, so that no two skyscrapers in a row or column have the same number of floors. In addition, the number of visible skyscrapers, as viewed from the direction of each clue, is equal to the value of the clue. Note that higher skyscrapers block the view of lower skyscrapers located behind them.




The puzzle and the ordinances are hard coded into the program. I would like feedback on efficiency and readability. Any and all suggestions are welcome!



SkyscraperPuzzle.java



package Skyscraper;


/**
* Write a description of class SkyscraperPuzzle here.
*
* @author David White
* @version (a version number or a date)
*/

public class SkyscraperPuzzle {

private int[][] puzzle = {
{1, 4, 3, 2},
{2, 3, 4, 1},
{4, 2, 1, 3},
{3, 1, 2, 4}
};
private Ordinance[][] ords = {
{new Ordinance(3, Direction.NORTH), new Ordinance(1, Direction.NORTH), new Ordinance(2, Direction.NORTH), new Ordinance(3, Direction.NORTH)}, //NORTH
{new Ordinance(2, Direction.SOUTH), new Ordinance(4, Direction.SOUTH), new Ordinance(2, Direction.SOUTH), new Ordinance(1, Direction.SOUTH)}, //SOUTH
{new Ordinance(3, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(1, Direction.EAST)}, //EAST
{new Ordinance(2, Direction.WEST), new Ordinance(3, Direction.WEST), new Ordinance(1, Direction.WEST), new Ordinance(2, Direction.WEST)} //WEST
};

/**
* Write a constructor to initialize and populate the puzzle and list of ordinances
* so that the puzzle may be checked to see if it is (correctly) solved.
*/
public SkyscraperPuzzle() {
//Puzzle is populated above
//Ords is populated above
}

/**
* returns the puzzle
*/
public int[][] getPuzzle() {
return this.puzzle;
}

/**
* Retuns a 1D array that respresents row r of puzzle
*/
public int[] getRow(int row) {
return puzzle[row];
}

/**
* Write a method that returns a 1D array that represents column c of puzzle
*/
public int[] getColumn(int column) { // row by column
int[] arr = new int[this.puzzle.length];
for(int i = 0; i < this.puzzle.length; i++) {
arr[i] = this.puzzle[i][column];
}
return arr;
}

/**
* Write a method that returns true if the 1D array parameter does not contain duplicates
*/
public boolean noDuplicates(int[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
for(int j = i + 1; j < arr.length - 1; j++) {
if(arr[i] == arr[j]) {
return false;
}
}
}
return true;
}

/**
* Write a method that returns true if each value from 1 to the number of rows (or columns, they
* are the same), is contained in the 1D array
*
* if 5x5
* then 1 2 3 4 5 should all be in array
*/
public boolean containsOneToN(int[] arr) {
int[] array = new int[arr.length];
int count = 0;
for(int i = 0; i < array.length; i++) {
array[i] = i + 1;
}
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < array.length; j++) {
if(arr[i] == array[j]) {
count += 1;
array[j] = 0;
}
}
}
return count == arr.length;
}

/**
* Write a method that traverses the 2D array, ords, and returns true if each and
* every Ordinance requirement is met
*/
public boolean meetsOrdinances() {
for(int i = 0; i < this.puzzle.length; i++) {
/* Yet to be implemented */
}
return false; //used to allow compile
}

/**
* Write a method that returns true if the puzzle is a correctly solved Skyscraper puzzle
*/
public boolean isSolved() {
/* Each row and column */
for(int i = 0; i < this.puzzle.length; i++) {
if(!noDuplicates(this.puzzle[i]) || !noDuplicates(getColumn(i))) {
return false;
}
if(!containsOneToN(this.puzzle[i]) || !containsOneToN(getColumn(i))) {
return false;
}
if(!meetsOrdinances()) {
return false;
}
}
return true;
}
}


Direction.java



package Skyscraper;


/**
* Enumeration class Direction - write a description of the enum class here
*
* @author David White
* @version (version number or date here)
*/
public enum Direction {
NORTH, /* Above the 2D array */
SOUTH, /* Below the 2D array */
EAST, /* Right of the 2D array */
WEST /* Left of the 2D array */
}


Ordinance.java



package Skyscraper;


/**
* Write a description of class Ordinance here.
*
* @author David White
* @version (a version number or a date)
*/
public class Ordinance {

private int num;
private Direction direction;

public Ordinance(int num, Direction direction) {
this.num = num;
this.direction = direction;
}

public int getNum() {
return this.num;
}

public Direction getDirection() {
return this.direction;
}
}




The PuzzleTest.java is used to make sure the methods I've written work in the way they're intended to.



PuzzleTest.java



package Skyscraper;


/**
* Write a description of class PuzzleTest here.
*
* @author David White
* @version (a version number or a date)
*/
public class PuzzleTest {

public static void main(String[] args) {

SkyscraperPuzzle p = new SkyscraperPuzzle();
int[][] puzzle = p.getPuzzle();

/*
* Outputs entire puzzle
*/
for(int[] row : puzzle) {
for(int item : row) {
System.out.print(item + " ");
}
System.out.println();
}
System.out.println();

/*
* Test getColumn method
* Output should be
* 1
* 4
* 7
* Status: Successful
*/
int[] col = p.getColumn(0);
for(int i = 0; i < col.length; i++) {
System.out.println(col[i]);
}
System.out.println();

/*
* Test noDuplicates method
* Output should be
* true
* Status: Successful
*/
int[] arr = {1, 2, 3, 4, 5};
System.out.println(p.noDuplicates(arr));
System.out.println();
/*
* Test containsOneToN method
* Output should be
* true
* Status: Successful
*/
int[] arr2 = {1, 2, 3, 4, 5};
System.out.println(p.containsOneToN(arr2));

/*
* Test meetsOrdinances method
* Output should be
* true
* Status: To be tested
*/

}

}









share|improve this question











$endgroup$








  • 1




    $begingroup$
    @200_success Yes, just edited the post to add the description.
    $endgroup$
    – David White
    4 hours ago






  • 1




    $begingroup$
    How can this code ever work if meetsOrdinances says Yet to be implemented?
    $endgroup$
    – Roland Illig
    4 hours ago










  • $begingroup$
    A puzzle solver by definition gets only the clues and then figures out the arrangement of the skyscrapers. I cannot find any code that does this.
    $endgroup$
    – Roland Illig
    4 hours ago












  • $begingroup$
    @RolandIllig This assignment in particular has both the skyscrapers and ordinances hardcoded, and just checks if the ordinances match the skyscraper. No user input is involved. And I'm just inquiring about the state of the code now, assuming that meetOrdinances works as expected.
    $endgroup$
    – David White
    3 hours ago














2












2








2





$begingroup$


For an assignment, I have created a solver for the Skyscraper Puzzle:




Each puzzle consists of an N×N grid with some clues along its sides. The object is to place a skyscraper in each square, with a height between 1 and N, so that no two skyscrapers in a row or column have the same number of floors. In addition, the number of visible skyscrapers, as viewed from the direction of each clue, is equal to the value of the clue. Note that higher skyscrapers block the view of lower skyscrapers located behind them.




The puzzle and the ordinances are hard coded into the program. I would like feedback on efficiency and readability. Any and all suggestions are welcome!



SkyscraperPuzzle.java



package Skyscraper;


/**
* Write a description of class SkyscraperPuzzle here.
*
* @author David White
* @version (a version number or a date)
*/

public class SkyscraperPuzzle {

private int[][] puzzle = {
{1, 4, 3, 2},
{2, 3, 4, 1},
{4, 2, 1, 3},
{3, 1, 2, 4}
};
private Ordinance[][] ords = {
{new Ordinance(3, Direction.NORTH), new Ordinance(1, Direction.NORTH), new Ordinance(2, Direction.NORTH), new Ordinance(3, Direction.NORTH)}, //NORTH
{new Ordinance(2, Direction.SOUTH), new Ordinance(4, Direction.SOUTH), new Ordinance(2, Direction.SOUTH), new Ordinance(1, Direction.SOUTH)}, //SOUTH
{new Ordinance(3, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(1, Direction.EAST)}, //EAST
{new Ordinance(2, Direction.WEST), new Ordinance(3, Direction.WEST), new Ordinance(1, Direction.WEST), new Ordinance(2, Direction.WEST)} //WEST
};

/**
* Write a constructor to initialize and populate the puzzle and list of ordinances
* so that the puzzle may be checked to see if it is (correctly) solved.
*/
public SkyscraperPuzzle() {
//Puzzle is populated above
//Ords is populated above
}

/**
* returns the puzzle
*/
public int[][] getPuzzle() {
return this.puzzle;
}

/**
* Retuns a 1D array that respresents row r of puzzle
*/
public int[] getRow(int row) {
return puzzle[row];
}

/**
* Write a method that returns a 1D array that represents column c of puzzle
*/
public int[] getColumn(int column) { // row by column
int[] arr = new int[this.puzzle.length];
for(int i = 0; i < this.puzzle.length; i++) {
arr[i] = this.puzzle[i][column];
}
return arr;
}

/**
* Write a method that returns true if the 1D array parameter does not contain duplicates
*/
public boolean noDuplicates(int[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
for(int j = i + 1; j < arr.length - 1; j++) {
if(arr[i] == arr[j]) {
return false;
}
}
}
return true;
}

/**
* Write a method that returns true if each value from 1 to the number of rows (or columns, they
* are the same), is contained in the 1D array
*
* if 5x5
* then 1 2 3 4 5 should all be in array
*/
public boolean containsOneToN(int[] arr) {
int[] array = new int[arr.length];
int count = 0;
for(int i = 0; i < array.length; i++) {
array[i] = i + 1;
}
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < array.length; j++) {
if(arr[i] == array[j]) {
count += 1;
array[j] = 0;
}
}
}
return count == arr.length;
}

/**
* Write a method that traverses the 2D array, ords, and returns true if each and
* every Ordinance requirement is met
*/
public boolean meetsOrdinances() {
for(int i = 0; i < this.puzzle.length; i++) {
/* Yet to be implemented */
}
return false; //used to allow compile
}

/**
* Write a method that returns true if the puzzle is a correctly solved Skyscraper puzzle
*/
public boolean isSolved() {
/* Each row and column */
for(int i = 0; i < this.puzzle.length; i++) {
if(!noDuplicates(this.puzzle[i]) || !noDuplicates(getColumn(i))) {
return false;
}
if(!containsOneToN(this.puzzle[i]) || !containsOneToN(getColumn(i))) {
return false;
}
if(!meetsOrdinances()) {
return false;
}
}
return true;
}
}


Direction.java



package Skyscraper;


/**
* Enumeration class Direction - write a description of the enum class here
*
* @author David White
* @version (version number or date here)
*/
public enum Direction {
NORTH, /* Above the 2D array */
SOUTH, /* Below the 2D array */
EAST, /* Right of the 2D array */
WEST /* Left of the 2D array */
}


Ordinance.java



package Skyscraper;


/**
* Write a description of class Ordinance here.
*
* @author David White
* @version (a version number or a date)
*/
public class Ordinance {

private int num;
private Direction direction;

public Ordinance(int num, Direction direction) {
this.num = num;
this.direction = direction;
}

public int getNum() {
return this.num;
}

public Direction getDirection() {
return this.direction;
}
}




The PuzzleTest.java is used to make sure the methods I've written work in the way they're intended to.



PuzzleTest.java



package Skyscraper;


/**
* Write a description of class PuzzleTest here.
*
* @author David White
* @version (a version number or a date)
*/
public class PuzzleTest {

public static void main(String[] args) {

SkyscraperPuzzle p = new SkyscraperPuzzle();
int[][] puzzle = p.getPuzzle();

/*
* Outputs entire puzzle
*/
for(int[] row : puzzle) {
for(int item : row) {
System.out.print(item + " ");
}
System.out.println();
}
System.out.println();

/*
* Test getColumn method
* Output should be
* 1
* 4
* 7
* Status: Successful
*/
int[] col = p.getColumn(0);
for(int i = 0; i < col.length; i++) {
System.out.println(col[i]);
}
System.out.println();

/*
* Test noDuplicates method
* Output should be
* true
* Status: Successful
*/
int[] arr = {1, 2, 3, 4, 5};
System.out.println(p.noDuplicates(arr));
System.out.println();
/*
* Test containsOneToN method
* Output should be
* true
* Status: Successful
*/
int[] arr2 = {1, 2, 3, 4, 5};
System.out.println(p.containsOneToN(arr2));

/*
* Test meetsOrdinances method
* Output should be
* true
* Status: To be tested
*/

}

}









share|improve this question











$endgroup$




For an assignment, I have created a solver for the Skyscraper Puzzle:




Each puzzle consists of an N×N grid with some clues along its sides. The object is to place a skyscraper in each square, with a height between 1 and N, so that no two skyscrapers in a row or column have the same number of floors. In addition, the number of visible skyscrapers, as viewed from the direction of each clue, is equal to the value of the clue. Note that higher skyscrapers block the view of lower skyscrapers located behind them.




The puzzle and the ordinances are hard coded into the program. I would like feedback on efficiency and readability. Any and all suggestions are welcome!



SkyscraperPuzzle.java



package Skyscraper;


/**
* Write a description of class SkyscraperPuzzle here.
*
* @author David White
* @version (a version number or a date)
*/

public class SkyscraperPuzzle {

private int[][] puzzle = {
{1, 4, 3, 2},
{2, 3, 4, 1},
{4, 2, 1, 3},
{3, 1, 2, 4}
};
private Ordinance[][] ords = {
{new Ordinance(3, Direction.NORTH), new Ordinance(1, Direction.NORTH), new Ordinance(2, Direction.NORTH), new Ordinance(3, Direction.NORTH)}, //NORTH
{new Ordinance(2, Direction.SOUTH), new Ordinance(4, Direction.SOUTH), new Ordinance(2, Direction.SOUTH), new Ordinance(1, Direction.SOUTH)}, //SOUTH
{new Ordinance(3, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(2, Direction.EAST), new Ordinance(1, Direction.EAST)}, //EAST
{new Ordinance(2, Direction.WEST), new Ordinance(3, Direction.WEST), new Ordinance(1, Direction.WEST), new Ordinance(2, Direction.WEST)} //WEST
};

/**
* Write a constructor to initialize and populate the puzzle and list of ordinances
* so that the puzzle may be checked to see if it is (correctly) solved.
*/
public SkyscraperPuzzle() {
//Puzzle is populated above
//Ords is populated above
}

/**
* returns the puzzle
*/
public int[][] getPuzzle() {
return this.puzzle;
}

/**
* Retuns a 1D array that respresents row r of puzzle
*/
public int[] getRow(int row) {
return puzzle[row];
}

/**
* Write a method that returns a 1D array that represents column c of puzzle
*/
public int[] getColumn(int column) { // row by column
int[] arr = new int[this.puzzle.length];
for(int i = 0; i < this.puzzle.length; i++) {
arr[i] = this.puzzle[i][column];
}
return arr;
}

/**
* Write a method that returns true if the 1D array parameter does not contain duplicates
*/
public boolean noDuplicates(int[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
for(int j = i + 1; j < arr.length - 1; j++) {
if(arr[i] == arr[j]) {
return false;
}
}
}
return true;
}

/**
* Write a method that returns true if each value from 1 to the number of rows (or columns, they
* are the same), is contained in the 1D array
*
* if 5x5
* then 1 2 3 4 5 should all be in array
*/
public boolean containsOneToN(int[] arr) {
int[] array = new int[arr.length];
int count = 0;
for(int i = 0; i < array.length; i++) {
array[i] = i + 1;
}
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < array.length; j++) {
if(arr[i] == array[j]) {
count += 1;
array[j] = 0;
}
}
}
return count == arr.length;
}

/**
* Write a method that traverses the 2D array, ords, and returns true if each and
* every Ordinance requirement is met
*/
public boolean meetsOrdinances() {
for(int i = 0; i < this.puzzle.length; i++) {
/* Yet to be implemented */
}
return false; //used to allow compile
}

/**
* Write a method that returns true if the puzzle is a correctly solved Skyscraper puzzle
*/
public boolean isSolved() {
/* Each row and column */
for(int i = 0; i < this.puzzle.length; i++) {
if(!noDuplicates(this.puzzle[i]) || !noDuplicates(getColumn(i))) {
return false;
}
if(!containsOneToN(this.puzzle[i]) || !containsOneToN(getColumn(i))) {
return false;
}
if(!meetsOrdinances()) {
return false;
}
}
return true;
}
}


Direction.java



package Skyscraper;


/**
* Enumeration class Direction - write a description of the enum class here
*
* @author David White
* @version (version number or date here)
*/
public enum Direction {
NORTH, /* Above the 2D array */
SOUTH, /* Below the 2D array */
EAST, /* Right of the 2D array */
WEST /* Left of the 2D array */
}


Ordinance.java



package Skyscraper;


/**
* Write a description of class Ordinance here.
*
* @author David White
* @version (a version number or a date)
*/
public class Ordinance {

private int num;
private Direction direction;

public Ordinance(int num, Direction direction) {
this.num = num;
this.direction = direction;
}

public int getNum() {
return this.num;
}

public Direction getDirection() {
return this.direction;
}
}




The PuzzleTest.java is used to make sure the methods I've written work in the way they're intended to.



PuzzleTest.java



package Skyscraper;


/**
* Write a description of class PuzzleTest here.
*
* @author David White
* @version (a version number or a date)
*/
public class PuzzleTest {

public static void main(String[] args) {

SkyscraperPuzzle p = new SkyscraperPuzzle();
int[][] puzzle = p.getPuzzle();

/*
* Outputs entire puzzle
*/
for(int[] row : puzzle) {
for(int item : row) {
System.out.print(item + " ");
}
System.out.println();
}
System.out.println();

/*
* Test getColumn method
* Output should be
* 1
* 4
* 7
* Status: Successful
*/
int[] col = p.getColumn(0);
for(int i = 0; i < col.length; i++) {
System.out.println(col[i]);
}
System.out.println();

/*
* Test noDuplicates method
* Output should be
* true
* Status: Successful
*/
int[] arr = {1, 2, 3, 4, 5};
System.out.println(p.noDuplicates(arr));
System.out.println();
/*
* Test containsOneToN method
* Output should be
* true
* Status: Successful
*/
int[] arr2 = {1, 2, 3, 4, 5};
System.out.println(p.containsOneToN(arr2));

/*
* Test meetsOrdinances method
* Output should be
* true
* Status: To be tested
*/

}

}






java homework sudoku






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









200_success

130k16153417




130k16153417










asked 13 hours ago









David WhiteDavid White

277413




277413








  • 1




    $begingroup$
    @200_success Yes, just edited the post to add the description.
    $endgroup$
    – David White
    4 hours ago






  • 1




    $begingroup$
    How can this code ever work if meetsOrdinances says Yet to be implemented?
    $endgroup$
    – Roland Illig
    4 hours ago










  • $begingroup$
    A puzzle solver by definition gets only the clues and then figures out the arrangement of the skyscrapers. I cannot find any code that does this.
    $endgroup$
    – Roland Illig
    4 hours ago












  • $begingroup$
    @RolandIllig This assignment in particular has both the skyscrapers and ordinances hardcoded, and just checks if the ordinances match the skyscraper. No user input is involved. And I'm just inquiring about the state of the code now, assuming that meetOrdinances works as expected.
    $endgroup$
    – David White
    3 hours ago














  • 1




    $begingroup$
    @200_success Yes, just edited the post to add the description.
    $endgroup$
    – David White
    4 hours ago






  • 1




    $begingroup$
    How can this code ever work if meetsOrdinances says Yet to be implemented?
    $endgroup$
    – Roland Illig
    4 hours ago










  • $begingroup$
    A puzzle solver by definition gets only the clues and then figures out the arrangement of the skyscrapers. I cannot find any code that does this.
    $endgroup$
    – Roland Illig
    4 hours ago












  • $begingroup$
    @RolandIllig This assignment in particular has both the skyscrapers and ordinances hardcoded, and just checks if the ordinances match the skyscraper. No user input is involved. And I'm just inquiring about the state of the code now, assuming that meetOrdinances works as expected.
    $endgroup$
    – David White
    3 hours ago








1




1




$begingroup$
@200_success Yes, just edited the post to add the description.
$endgroup$
– David White
4 hours ago




$begingroup$
@200_success Yes, just edited the post to add the description.
$endgroup$
– David White
4 hours ago




1




1




$begingroup$
How can this code ever work if meetsOrdinances says Yet to be implemented?
$endgroup$
– Roland Illig
4 hours ago




$begingroup$
How can this code ever work if meetsOrdinances says Yet to be implemented?
$endgroup$
– Roland Illig
4 hours ago












$begingroup$
A puzzle solver by definition gets only the clues and then figures out the arrangement of the skyscrapers. I cannot find any code that does this.
$endgroup$
– Roland Illig
4 hours ago






$begingroup$
A puzzle solver by definition gets only the clues and then figures out the arrangement of the skyscrapers. I cannot find any code that does this.
$endgroup$
– Roland Illig
4 hours ago














$begingroup$
@RolandIllig This assignment in particular has both the skyscrapers and ordinances hardcoded, and just checks if the ordinances match the skyscraper. No user input is involved. And I'm just inquiring about the state of the code now, assuming that meetOrdinances works as expected.
$endgroup$
– David White
3 hours ago




$begingroup$
@RolandIllig This assignment in particular has both the skyscrapers and ordinances hardcoded, and just checks if the ordinances match the skyscraper. No user input is involved. And I'm just inquiring about the state of the code now, assuming that meetOrdinances works as expected.
$endgroup$
– David White
3 hours ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214569%2fskyscraper-puzzle-solver-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214569%2fskyscraper-puzzle-solver-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...