0

I'm currently doing a model, however its occurred that I'm getting 1 error that which is saying that the && operator. I don't fully understand why that is. If there is a solution to this that would be great, also with a good explanation why I have made this mistake

Thanks

import java.awt.image.*;
import javax.imageio.*;
import java.io.File;

public class SlidingBlockModel
{
	BufferedImage[][] board; // the two '[][]' allows a 2-D array to be created - each element is an image
	int NUM_COLUMNS;
	int NUM_ROWS;
	int empty_col; // holds the column index of the one empty square
	int empty_row; // holds the row index of the one empty square
	int control[][];

	SlidingBlockModel(int input_num_cols,int input_num_rows,String image_filename)
	{
		NUM_COLUMNS = input_num_cols;
		NUM_ROWS = input_num_rows;
		BufferedImage original_image = null;
		board = new BufferedImage[NUM_COLUMNS][NUM_ROWS];
		control = new int [NUM_COLUMNS][NUM_ROWS];
		try
		{
			original_image = ImageIO.read(new File(image_filename));
		}
		catch (Exception e)
		{
			System.out.println("Sorry - couldn't load in file " + image_filename);
		}

		int block_width = original_image.getWidth()/NUM_COLUMNS;
		int block_height = original_image.getHeight()/NUM_ROWS;

		for (int i = 0;i<NUM_COLUMNS;i++)
			for(int j = 0;j<NUM_ROWS;j++)
			{
				int x = block_width*i;
				int y = block_height*j;
				board[i][j] = original_image.getSubimage(x, y, block_width, block_height);
			}

		empty_row = NUM_ROWS-1;
		empty_col = NUM_COLUMNS-1;
		board[empty_col][empty_row] = null; // removes the sub-image that was there

	}
	boolean go(int col, int row)
	{
			
if (((Math.abs(row-empty_row) == 1)&&(Math.abs(col-empty_col) != 1))||((Math.abs(row-empty_row)!= 1)&&(Math.abs(col-empty_col))))
			{
				board[empty_col][empty_row] = board[col][row];
				
				control[empty_col][empty_row] = control[col][row];
				board[col][row] = null;
				
				empty_col = col;
				empty_row = row;
				return true;
			}
			else
			{
				return false;
			}
	}

	public BufferedImage get(int col, int row)
	{
		if(col >= 0 || row >= 0)
		{
			return board[col][row];
		}
		else
		{
			return null;
		}
	}

public void randomize()
	{
		java.util.Random r = new java.util.Random();
		for (int x = 0; x <= 10; x++)
		{
			go(r.nextInt(NUM_COLUMNS),r.nextInt(NUM_ROWS));
		}
	}

boolean complete()
	{
		boolean current = true;
		for (int i = 0; i < NUM_COLUMNS; i++)
		{
			for (int j = 0; j < NUM_ROWS; j++)
			{
				if (control[i][j] != ((i * NUM_ROWS)+j))
				{
					current = false;
				}

			}
		}

		return current;
	}
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by BestJewSinceJC
0
((Math.abs(row-empty_row)!= 1) [B]&& (Math.abs(col-empty_col))[/B]

Why would that work? You are treating the returned value of Math.abs(), which is an integer, as if it can be tested for a truth value. In Java, only boolean types can be tested for truth. So you need to put something like this

((Math.abs(row-empty_row)!= 1) [B]&& (Math.abs(col-empty_col) > 0)[/B]

Expressions such as the one I posted above evaluate to either "true" or "false". In other words, Java looks at the returned value of Math.abs(), then looks at the operator, then looks at the value "0" on the other side of the operator and decides whether the expression is "true" or "false". In your code, you never compared the return value of Math.abs() to anything, and since the integer that it returned was not evaluated as "true" or "false" (since Java doesn't evaluate ints as true or false), it generated an error.

Edited by BestJewSinceJC: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.