Kod:
import java.util.Random;
public class Queens {
private Square[][] board;
private int n;
public Queens(int n)
{
this.n = n;
board = new Square[n][n];
resetBoard();
setNewRandomPositioning();
}
private void resetBoard()
{
board = new Square[][]; // Reset the board
}
public void setNewRandomPositioning()
{
board = new Square[][]; //Reset the board
Random r = new Random();
n = r.nextInt(); // Fill with random
board = new Square[n][n]; // Fills the board with n queens
}
private int countQueens()
{
int counter = n*n; // If you muliply n*n, you will get number of n in the board
return counter;
}
private boolean isThreatened(int i, int j, int dx, int dy)
{
}
}
Om du har gjort rätt borde du kunna kontrollera själv. Om de kompilerar och fungerar som du ska har du gjort rätt. Testa olika indata till dem. Returneras rätt resultat?
För isThreatened rekommenderar jag att du börjar med pseudokod och förfinar den gradvis tills dess du kan skriva koden.