help converting



 DEVELOP > c-Plus-Plus > help converting

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "jipster"
Date: 05 Dec 2004 02:34:02 PM
Object: help converting
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[i][this.yPos] != null )
return true;
if ( board.square[this.xPos][i] != 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
.

User: "Howard"

Title: Re: help converting 06 Dec 2004 11:00:27 AM
"jipster" <wigster84@aol.com> wrote in message
news:34d219a73c01c9cbda38aa9cf11f3434@localhost.talkaboutprogramming.com...

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
.

User: "Victor Bazarov"

Title: Re: help converting 05 Dec 2004 02:51:07 PM
"jipster" <wigster84@aol.com> wrote...

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?
.
User: "jipster"

Title: Re: help converting 05 Dec 2004 03:08:33 PM
:) how much are we asking
.


User: "Karl Heinz Buchegger"

Title: Re: help converting 06 Dec 2004 03:10:43 AM
jipster wrote:


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.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER