J
jipster
hi, i need help converting this java program to C++...are there any
programs or nething that could be used to do this faster??
class hw2 {
static public void main (String args[]) {
hw2.test();}
Board board = new Board(8);
if ( board.fillInRemainingLevels() )
board.print();
else
System.out.println("Couldn't find solution");
}
static void test () {
Board board = new Board(8);
// board.addQueen(int, int) calls: new Queen(int, int)
if ( ! board.addQueen(5,6) )
throw new Error("failed test1");
if ( board.addQueen(6,7) )
throw new Error("failed test2");
if ( board.addQueen(4,7) )
throw new Error("failed test3");
if ( board.addQueen(4,5) )
throw new Error("failed test4");
if ( board.addQueen(5,5) )
throw new Error("failed test5");
}
}
class Board {
int size;
Queen [][] square;
int currentLevel = 0;
boolean done = false;
Board(int size) {
this.size = size;
this.square = new Queen[size][size];
}
boolean addQueen(Queen queen) {
if (queen.canCapture(this))
return false;
else {
this.square[queen.xPos][queen.yPos] = queen;
return true;
}
}
boolean addQueen(int x, int y) {
Queen queen = new Queen(x, y);
return this.addQueen( queen );
}
void removeQueen(Queen queen) {
this.square[queen.xPos][queen.yPos] = null;
}
boolean fillInRemainingLevels() {
if (this.currentLevel >= this.size) {
this.done = true;
return true;
}
int level = currentLevel;
for (int x = 0; x < this.size; x++) {
Queen queen = new Queen(x, this.currentLevel);
if (this.addQueen( queen )) {
this.currentLevel++;
this.fillInRemainingLevels();
// When we study exceptions, there will be a better
// way to handle this.
if (this.done)
return true;
// remove queen and restore currentLevel if backing up
this.removeQueen( queen );
this.currentLevel = level;
}
}
return false;
}
void print() {
for (int x = 0; x < this.size; x++) {
for (int y = 0; y < this.size; y++) {
if (this.square[x][y] == null)
System.out.print(" .");
else
System.out.print(" Q");
}
System.out.println();
}
System.out.println();
}
}
class Queen {
int xPos;
int yPos;
Queen(int x, int y) {
this.xPos = x;
this.yPos = y;
}
boolean canCapture(Board board) {
for (int i = 0; i < board.size; i++) {
if ( board.square[this.yPos] != null )
return true;
if ( board.square[this.xPos] != null )
return true;
}
for (int y = 0; y < board.size; y++) {
int delta = this.yPos - y;
int x1 = this.xPos + delta;
int x2 = this.xPos - delta;
if ( x1 >= 0 && x1 < board.size && board.square[x1][y] != null )
return true;
if ( x2 >= 0 && x2 < board.size && board.square[x2][y] != null )
return true;
}
return false;
}
}
thank you
programs or nething that could be used to do this faster??
class hw2 {
static public void main (String args[]) {
hw2.test();}
Board board = new Board(8);
if ( board.fillInRemainingLevels() )
board.print();
else
System.out.println("Couldn't find solution");
}
static void test () {
Board board = new Board(8);
// board.addQueen(int, int) calls: new Queen(int, int)
if ( ! board.addQueen(5,6) )
throw new Error("failed test1");
if ( board.addQueen(6,7) )
throw new Error("failed test2");
if ( board.addQueen(4,7) )
throw new Error("failed test3");
if ( board.addQueen(4,5) )
throw new Error("failed test4");
if ( board.addQueen(5,5) )
throw new Error("failed test5");
}
}
class Board {
int size;
Queen [][] square;
int currentLevel = 0;
boolean done = false;
Board(int size) {
this.size = size;
this.square = new Queen[size][size];
}
boolean addQueen(Queen queen) {
if (queen.canCapture(this))
return false;
else {
this.square[queen.xPos][queen.yPos] = queen;
return true;
}
}
boolean addQueen(int x, int y) {
Queen queen = new Queen(x, y);
return this.addQueen( queen );
}
void removeQueen(Queen queen) {
this.square[queen.xPos][queen.yPos] = null;
}
boolean fillInRemainingLevels() {
if (this.currentLevel >= this.size) {
this.done = true;
return true;
}
int level = currentLevel;
for (int x = 0; x < this.size; x++) {
Queen queen = new Queen(x, this.currentLevel);
if (this.addQueen( queen )) {
this.currentLevel++;
this.fillInRemainingLevels();
// When we study exceptions, there will be a better
// way to handle this.
if (this.done)
return true;
// remove queen and restore currentLevel if backing up
this.removeQueen( queen );
this.currentLevel = level;
}
}
return false;
}
void print() {
for (int x = 0; x < this.size; x++) {
for (int y = 0; y < this.size; y++) {
if (this.square[x][y] == null)
System.out.print(" .");
else
System.out.print(" Q");
}
System.out.println();
}
System.out.println();
}
}
class Queen {
int xPos;
int yPos;
Queen(int x, int y) {
this.xPos = x;
this.yPos = y;
}
boolean canCapture(Board board) {
for (int i = 0; i < board.size; i++) {
if ( board.square[this.yPos] != null )
return true;
if ( board.square[this.xPos] != null )
return true;
}
for (int y = 0; y < board.size; y++) {
int delta = this.yPos - y;
int x1 = this.xPos + delta;
int x2 = this.xPos - delta;
if ( x1 >= 0 && x1 < board.size && board.square[x1][y] != null )
return true;
if ( x2 >= 0 && x2 < board.size && board.square[x2][y] != null )
return true;
}
return false;
}
}
thank you