help converting

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
 
V

Victor Bazarov

jipster said:
hi, i need help converting this java program to C++...are there any
programs or nething that could be used to do this faster??

Text editors with syntax highlighting, maybe?

How much are you prepared to pay for this job?
 
K

Karl Heinz Buchegger

jipster said:
hi, i need help converting this java program to C++...are there any
programs or nething that could be used to do this faster??

I don't think your C++ teacher will be much impressed if you
hand in a C++ version of this poor Java '8-queens' solution.
 
H

Howard

jipster said:
hi, i need help converting this java program to C++...are there any
programs or nething that could be used to do this faster??

("nething"? You're entering the world of programming now. You might want
to use actual words. Compilers are going to be a lot pickier than your IM
buddies.)

You could search Google for something which would help translate that code.
Or, you could search Google for an actual C++ version of the 8 Queens
problem. Or, if this is for a class, you could try writing the program
yourself(!), and asking here for help with specific problems you have
getting it to work correctly (once you've actually written some C++ code,
that is).

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top