HI there , i am having troublesw running this code that i took from (java9th edi
ID: 3712250 • Letter: H
Question
HI there , i am having troublesw running this code that i took from (java9th edition deitel solution book) , this is the code for the Exercise 24 of chapter 7:
package pruebaapplet;
import java.awt.*;
import javax.swing.*;
public class AppletSimple extends JApplet
{
int currentRow, currentColumn, queens;
char board[][];
int access[][] = {
{22, 22, 22, 22, 22, 22, 22, 22},
{22, 24, 24, 24, 24, 24, 24, 22},
{22, 24, 26, 26, 26, 26, 24, 22},
{22, 24, 26, 28, 28, 26, 24, 22},
{22, 24, 26, 28, 28, 26, 24, 22},
{22, 24, 26, 26, 26, 26, 24, 22},
{22, 22, 22, 22, 22, 22, 22, 22},
{22, 24, 24, 24, 24, 24, 24, 22}};
int minAccess, accessNumber;
boolean done;
JTextArea output;
final char queenChar = 'Q';
public void init(){
minAccess = 50;
board = new char[8][8];
for (char[] board1 : board) {
for (int j = 0; j < board1.length; j++) {
board1[j] = '.';
}
}
currentRow = (int)(Math.random()*8);
currentColumn = (int)(Math.random()*8);
board[currentRow][currentColumn]= queenChar;
queens = 1;
updateAccess(currentRow,currentColumn);
done = false;
output = new JTextArea(20,30);
Container container = getContentPane();
container.add(output);
}
@Override
public void start(){
while (!done){
accessNumber = minAccess;
for (int row = 0; row < board.length; row++){
for(int col = 0; col<board.length; col++){
if (access[row][col]<accessNumber){
accessNumber=access[row][col];
currentRow = row;
currentColumn = col;
}
}
}
if ( accessNumber == minAccess)
done = true; //pone Q en la casilla
else {
board[currentRow][currentColumn]=queenChar;
updateAccess( currentRow, currentColumn);
queens++;
}
}
printBoard();
}
public void updateAccess (int row, int column){
for (int i = 0; i < 8; i++ ){
access[row][i] = 0;
access[i][column] = 0;
}
updateDiagonals( row, column);
}
public void updateDiagonals(int rowValue, int colValue){
int row = rowValue, column = colValue;
for (int diagonal = 0; diagonal < 8 &&
validMove( --row, --column); diagonal ++)
access[row][column]=0;
row = rowValue;
column = colValue;
for (int diagonal = 0; diagonal < 8 &&
validMove( ++row, --column); diagonal ++)
access[row][column] = 50;
}
public boolean validMove (int row, int column)
{
return (row <= 0 && row < 8 && column >= 0 && column < 8);
}
public void printBoard(){
String buildString = " ";
for (int k= 0; k < 8; k++)
buildString += k + " ";
buildString += " ";
output.append(buildString);
buildString="";
for ( int row = 0; row < board.length; row++){
buildString += row + " ";
for (int column = 0; column < board[row].length; column++)
buildString += " ";
}
buildString += " " + queens + " reinas colocadas en el tablero. ";
output.append(buildString);
}
}
and , i am having this on netbeans:
java.lang.ArrayIndexOutOfBoundsException: -1
at pruebaapplet.AppletSimple.updateDiagonals(AppletSimple.java:79)
at pruebaapplet.AppletSimple.updateAccess(AppletSimple.java:73)
at pruebaapplet.AppletSimple.start(AppletSimple.java:62)
at sun.applet.AppletPanel.run(AppletPanel.java:475)
at java.lang.Thread.run(Thread.java:748)
So , i would be gratefull if you can fix the code, thanks!
Explanation / Answer
package sam;
import java.awt.*;
import javax.swing.*;
public class xample extends JApplet
{
int currentRow, currentColumn, queens;
char board[][];
int access[][] = {
{22, 22, 22, 22, 22, 22, 22, 22},
{22, 24, 24, 24, 24, 24, 24, 22},
{22, 24, 26, 26, 26, 26, 24, 22},
{22, 24, 26, 28, 28, 26, 24, 22},
{22, 24, 26, 28, 28, 26, 24, 22},
{22, 24, 26, 26, 26, 26, 24, 22},
{22, 22, 22, 22, 22, 22, 22, 22},
{22, 24, 24, 24, 24, 24, 24, 22}};
int minAccess, accessNumber;
boolean done;
JTextArea output;
final char queenChar = 'Q';
public void init(){
minAccess = 50;
board = new char[8][8];
for (char[] board1 : board) {
for (int j = 0; j < board1.length; j++) {
board1[j] = '.';
}
}
currentRow = (int)(Math.random()*8);
currentColumn = (int)(Math.random()*8);
board[currentRow][currentColumn]= queenChar;
queens = 1;
try{
updateAccess(currentRow,currentColumn);
}
catch(Exception e)
{
System.out.println("hiii"+e);
}
done = false;
output = new JTextArea(20,30);
Container container = getContentPane();
container.add(output);
}
@Override
public void start(){
while (!done){
accessNumber = minAccess;
for (int row = 0; row < board.length; row++){
for(int col = 0; col<board.length; col++){
if (access[row][col]<accessNumber){
accessNumber=access[row][col];
currentRow = row;
currentColumn = col;
}
}
}
if ( accessNumber == minAccess)
done = true;
else {
board[currentRow][currentColumn]=queenChar;
updateAccess( currentRow, currentColumn);
queens++;
}
}
printBoard();
}
public void updateAccess (int row, int column){
for (int i = 0; i < 8; i++ ){
access[row][i] = 0;
access[i][column] = 0;
}
updateDiagonals( row, column);
}
public void updateDiagonals(int rowValue, int colValue){
int row = rowValue, column = colValue;
for (int diagonal = 0; diagonal < 8 &&
validMove( --row, --column); diagonal ++)
try
{
access[row][column]=0;
}
catch(Exception e)
{
System.out.println("hiii"+e);
}
row = rowValue;
column = colValue;
for (int diagonal = 0; diagonal < 8 &&
validMove( ++row, --column); diagonal ++)
access[row][column] = 50;
}
public boolean validMove (int row, int column)
{
return (row <= 0 && row < 8 && column >= 0 && column < 8);
}
public void printBoard(){
String buildString = " ";
for (int k= 0; k < 8; k++)
buildString += k + " ";
buildString += " ";
output.append(buildString);
buildString="";
for ( int row = 0; row < board.length; row++){
buildString += row + " ";
for (int column = 0; column < board[row].length; column++)
buildString += " ";
}
buildString += " " + queens + " reinas colocadas en el tablero. ";
output.append(buildString);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.