• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot be applied to a boolean.

 
Judith
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,
I have tried everything i can think of to make this go.
I am trying to hide the mines with this one line.(it's in bold)
I keep getting error messages that state || cannot be applied to a boolean.
any help would be appreciated.
thanks


public class App {
public static void main(String[] args) {
Game game = new Game();
game.play();
}
}

class Game {
private Minefield m = new Minefield();
private Player p = new Player();
public void play() {
m.hideMines();
m.enterGrid(0, 0);
m.drawField();
System.out.println("Game over");
}
}

class Minefield {
private char[] field = new char[4 * 4];
public void hideMines() {
int mines = 0;
do {
int loc = ((int)(Math.random() * 1000)) % (field.length - 1);
if (loc != 0 && field[loc] != 'M') {
field[loc] = 'M';
mines++;
}
} while (mines < 4);
field[15] = 'E';
}
public boolean enterGrid(int pRow, int pCol) {
field[(pRow * 4) + pCol] = 'P';
return false;
}
public void exitGrid(int pRow, int pCol) {
}


public void drawField() {
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
///THIS LINE RIGHT HERE ---> if (field[(r * 4) + c] ==0________________________________) {

System.out.print("- ");
}
else {
System.out.print(field[(r * 4) + c] + " ");
}
}
System.out.println("");
}
}
}

class Player {
private int row;
private int col;
public void move() {
System.out.print("(r)ight or (d)own: ");
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
}
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you've removed the part with the || in it, so it's difficult to tell what's wrong. You're comparing a char to an int which should be fine. If not, change it tobut again I don't think that's the problem. Most likely the line actually looks like this:If so, we really need to see the "..." to know what's wrong. As you probably know, || is the logical or operator which requires a boolean expression on both sides. I'm guessing the error you actually got waswhere "int" is anything other than "boolean." No fair hiding part of the error message from us!

Okay, I've tested with a little program and the character from field is promoted to int automatically in JDK 1.5, so the problem must be in the "..." that you aren't showing us. Post that part and the full error if you don't get it yourself. In fact, post it anyway!
 
Judith
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is how it reads now.
but it isn't what i wanted completly.
i want to test the field location to see if it's empty OR if contain
a 'mine'.

as it stands now, it shows up.(where the mines are)
here is what you were asking for. i need to change the if comparison.
thanks again..
__________________________________________
public void drawField() {
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
if (field[(r * 4) + c] == 0) {
System.out.print("- ");
}
else {
System.out.print(field[(r * 4) + c] + " ");
}
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You initialize the minefield with M's for the hidden locations of the mines, correct? In that case and the empty case (0) you want a dash, correct? Normally, it would be best if you posted what you had originally that didn't work, and then I could provide a hint in the right direction. But in this case I suspect you just had a typo. Here's what you want:Because == and other logical comparisons (<, >, <=, ...) have higher precedence than || and &&, both comparisons get evaluated to boolean values and then or'd together.

While logically that's what happens, in practice if the first test evalutes to true, the || realizes that the second test is unimportant and doesn't perform it. This is called short-circuit logic.

You might also want to declare constants to represent the various values of the minefield. This makes your code more readable.If you're not familiar with the switch statement, don't worry about it for now. You'll get to it later. I'm just demonstrating how constants can make the code easier to read.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judith,

I am sorry to see that after 18 posts you still do not have a first and last name separated by a space.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic