GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-May-2008, 11:25
BKizzle77 BKizzle77 is offline
New Member
 
Join Date: May 2008
Posts: 5
BKizzle77 is on a distinguished road

Classic recursion game of 8 Queens errors


Hey I am a relatively amateur java programmer using Eclipse. I have been trying for a while now to create the classic recurrsion game of 8 Queens and am stumped. Here is what I have so far:
JAVA Code:
 
package queens;
 
public class Queens {
 
 
    public static void main(String[] args) {
 
        final int SIZE = 8;
 
         QueenClass q = new QueenClass();
int t[] = q.solve();
 
 
// print solution 
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
if (j == t[i])
System.out.print(" Q ");
else
System.out.print(" _ ");
}
}
    }
 
}

And a class:
JAVA Code:
 
package queens;
 
public class QueenClass {
 
    public static final int SIZE = 8;
 
 
public int [] solve() {
int t[] = new int[SIZE];
if (nqueens(t, 0)) {
return t;
} else {
return null;
}
}
 
 
private boolean nqueens(int[] t, int i) {
for(t[i] = 0; t[i] < SIZE; t[i] += 1) {
if (empty(t,i)) {
if (i == (SIZE-1)) 
    return true;
}
else {
if (empty(t,i+1)) 
     return true;
}
}
return false;
}
 
 
private boolean empty(int [] t, int i) {
 
for(int j = 0; j < i; j++) {
if (t[j] == t[i])
return false;
if (Math.abs(t[j] - t[i]) == (i - j))
return false;
}
return true;
}
 
}

I'm getting no luck with a bunch of different errors every time i try to change something. Can someone please help?
Last edited by admin II : 27-May-2008 at 04:32. Reason: Please surround your Java code with [java] your code [/java]
  #2  
Old 26-May-2008, 17:50
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Having trouble...need some help


Quote:
Originally Posted by BKizzle77
I'm getting no luck with a bunch of different errors every time i try to change something. Can someone please help?
What are these "bunch of different errors"?

Post the errors that you get so that we can see where the problem(s) may be. It might be helpful to check this post, or this post for troubleshooting Java errors.

Also, with future Jave code, paste the codes between [java] [/java] tags.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 27-May-2008, 10:18
BKizzle77 BKizzle77 is offline
New Member
 
Join Date: May 2008
Posts: 5
BKizzle77 is on a distinguished road

Re: Having trouble...need some help


The error that i initially get before I change anything is

Exception in thread "main" java.lang.NullPointerException
at queens.Queens.main(Queens.java:17)

which refers to the print statement
  #4  
Old 27-May-2008, 10:44
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 534
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Having trouble...need some help


This would be my guess:
JAVA Code:
//for(t[i] = 0; t[i] < SIZE; t[i] += 1)
for(i = 0; i < SIZE; i += 1)
  #5  
Old 27-May-2008, 12:39
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Having trouble...need some help


Quote:
Originally Posted by BKizzle77
The error that i initially get before I change anything is

Exception in thread "main" java.lang.NullPointerException
at queens.Queens.main(Queens.java:17)
Ok, this tells us you have a runtime error, not a compile error... (see why "bunch of different errors" is vague?)

fakepoo has already made reference to the offending line.

Also, see this post for a stack dump trace example. (different situation, but they also had a "null pointer" problem)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 27-May-2008, 12:44
BKizzle77 BKizzle77 is offline
New Member
 
Join Date: May 2008
Posts: 5
BKizzle77 is on a distinguished road

Re: Having trouble...need some help


It still gives me the same error.

Is there a different way to output it just to see if the program is working...then I will worry about properly printing it out
  #7  
Old 27-May-2008, 12:59
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 534
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Having trouble...need some help


Probably what is happening is with this:
JAVA Code:
int t[] = q.solve();
because the solve() method can return null. If it does return null, you will get that runtime error when you call t[i].
  #8  
Old 30-May-2008, 15:58
BKizzle77 BKizzle77 is offline
New Member
 
Join Date: May 2008
Posts: 5
BKizzle77 is on a distinguished road

Re: Classic recursion game of 8 Queens errors


hey i reformatted the program to make it more basic, using an 8 X 8 array and such and am getting there but am still gettting a common error. Here is the code:

JAVA Code:
package eightQueens;
public class EightQueens {
 public static void main(String[] args) {
  
  //Create Board
  boolean [][] game = new boolean [8][8];
  
  //Create Queens
  int row = 0;
 
  //Place First Queen
  game[row][(int)Math.random() * 8] = true;
 
  //Place Second Queen
  row++;
  placeQueen(game, row);
  
  //Place Third Queen
  row++;
  placeQueen(game, row);
  
  //Place Fourth Queen
  row++;
  placeQueen(game, row);
  
  //Place Fifth Queen
  row++;
  placeQueen(game, row);
 
  //Place Sixth Queen
  row++;
  placeQueen(game, row);
  
  //Place Seventh Queen
  row++;
  placeQueen(game, row);
  
  //Place Eigth Queen
  row++;
  placeQueen(game, row);
  
  //Print Board
  for (int r = 0; r < game.length; r++){
   for (int c = 0; c < game[0].length; c++)
    System.out.print(game[r][c] + " ");
   System.out.println();
  }
 }
 
 public static void placeQueen(boolean[][] board, int row){
  int column = (int)(Math.random()*8);
  while(canBePlaced(board, row, column) == false){
   column = (int)(Math.random()*8);
  }
  board[row][column] = true;
 }
 
 public static boolean canBePlaced(boolean [][] board, int row, int column){
  
  for (int i = row - 1; i >= 0 ; i--){
   
   //Check Diagonal Up Right to Left
   int lDiag = column - 1;
   if(board[row][column] == board[i][lDiag] && lDiag <= 0)
    return false;
   lDiag--;
   
   //Check Diagonal Up Left to Right
   int rDiag = column + 1;
   if(board[row][column] == board[i][rDiag] && rDiag <= 7)
    return false;
   rDiag++;
   
   //Check Columns
   if(board[row][column] == board[i][column])
    return false;
  }
    
  return true;
 }
 
 
}

the error i keep getting even when i change up the code, its an index out of bounds error, usually from a -1 or an 8, and im not sure why:

Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at queens.Queens.canBePlaced(Queens.java:81) at queens.Queens.placeQueen(Queens.java:69) at queens.Queens.main(Queens.java:17)

and
Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at queens.Queens.canBePlaced(Queens.java:87) at queens.Queens.placeQueen(Queens.java:69) at queens.Queens.main(Queens.java:17)

Hope you guys can help because im stumped
Last edited by admin : 01-Jun-2008 at 06:33. Reason: Please insert your example Java codes between [JAVA] and [/JAVA] tags
  #9  
Old 30-May-2008, 16:33
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Classic recursion game of 8 Queens errors


The number with the 'out-of-bounds' is the index value that is out of range.

The array is specified 8, so that is the range 0 through 7, of course.

In both cases, though, it seems that the handling between functions 'placeQueen' and 'canBePlaced' is causing a problem.

Add some print functions to show the calculations of the row and column values as it proceeds -- that should lead to the problem.
Here's an example for the -1:
Code:
My syntax notations: pq implies placeQueen, cbp implies canBePlaced. r/c are row/column values ====================== pq, before while -- r:1, c:1 cbp loop: r:1, c:1 pq loop: 4 cbp loop: r:1, c:4 pq loop: 5 cbp loop: r:1, c:5 pq loop: 1 cbp loop: r:1, c:1 pq loop: 6 cbp loop: r:1, c:6 pq loop: 3 cbp loop: r:1, c:3 pq loop: 6 cbp loop: r:1, c:6 pq loop: 1 cbp loop: r:1, c:1 pq loop: 6 cbp loop: r:1, c:6 pq loop: 1 cbp loop: r:1, c:1 pq loop: 5 cbp loop: r:1, c:5 pq loop: 1 cbp loop: r:1, c:1 pq loop: 5 cbp loop: r:1, c:5 pq loop: 4 cbp loop: r:1, c:4 pq loop: 1 cbp loop: r:1, c:1 pq loop: 0 cbp loop: r:1, c:0 // hint: what first happens to 'column' in the loop? Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at EightQueens.canBePlaced(EightQueens.java:65) at EightQueens.placeQueen(EightQueens.java:52) at EightQueens.main(EightQueens.java:15)
That's how to debug your programs without relying on error message(s) or guessing what/where the problem may be.

If you get stumped again, post another reply.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #10  
Old 31-May-2008, 08:22
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Classic recursion game of 8 Queens errors


Quote:
Originally Posted by me
That's how to debug your programs without relying on error message(s) or guessing what/where the problem may be.
Actually, that's two alternate ways to debug but not always easier or reliable.
There's also the good old pencil and paper [aka: by hand] method to "trace-out" or plan the logic, and it's usually better to start that way.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Function Trouble(I'm having trouble with my program). dmb2140 C++ Forum 2 25-Nov-2007 10:27
Having trouble with body of programs Mrsnap32 C++ Forum 2 05-Apr-2006 08:08
Trouble with files and array linking to files. Blstretch C Programming Language 14 03-Dec-2005 04:29
C programming trouble Newworld C Programming Language 8 13-Sep-2004 00:06
Having trouble trying to format C: Nickster64 Computer Software Forum - Windows 2 27-Jul-2004 08:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.