I’m trying to include a char inside a bidimensional array, the problem is, that when I write the position for the element, the code seems to ignore it and the position is 0, 0 by default. Here’s the code of the method:
public boolean placePiece(int row, int col, char piece)
{
setPos(x, y);
setPiece(piece);
if (piece != 'K' && piece != 'C')
return false;
else {
board[x][y] = piece;
return true;
}
}
Code of the method setPos()
private void setPos(int x, int y)
{
this.x = x;
this.y = y;
if (x >= size && y >= size)
{
System.out.println("Out of range.");
}
}
Apart from that strange thing that’s happening, I’m also getting an infinite loop when executing another method. This method, counts all the ‘K’ in the selected row and prints the result to the user.
this.line = line;
this.number = number;
int counter = 0;
if (line == 'r' || line == 'R')
for (int j = 0; j < size; j++) {
while (board[number][j] == 'K')
{
counter += 1;
System.out.println("You've found " + counter + " keys!");
}
if (board[number][j] != 'K')
{
System.out.println("Try again, you haven't found any key yet.");
}
}
Don’t know how to get rid of the while and introduce another structure instead.
Hope you can help me, greetings and thanks in advance!
Replace
whilebyif