• 0

[Java] Why is this error occuring?


Question

Ok, so, I'm writing my Battleships assignment game which is juuuust about done. One problem is I get an "illegal start of expression" error when compiling the following code:

/**
 * Cell class. A Cell is one element in a Grid. A Cell knows which Ship
 * is occupying it. Cell is responsible for handling attacks.
 * As well as this, Cell can calculate what the UI should display.
 */
public class Cell {
	/* 
	 * Constants - used throughout the code
	 * DO NOT CHANGE!
	 */
	// Attack constants - used by Cell.tryAttack
	public static final int ATTACK_ALREADY = 1001;
	public static final int ATTACK_HIT = 1002;
	public static final int ATTACK_MISS = 1003;
	public static final int ATTACK_SUNK = 1004;

	// Display constants - used by Cell.displayState
	public static final int DISPLAY_BLANK = 2001;
	public static final int DISPLAY_HIT = 2002;
	public static final int DISPLAY_MISS = 2003;
	public static final int DISPLAY_OCCUPIED = 2004;

	private Player playerOwns;
	private int xCell;
	private int yCell;
	private boolean beenAttacked;
	private Ship occupyingShip;
	private boolean cellHit;

	/**
	 * Cell constructor. Puts the cell in the initial state of unoccupied,
	 * unattacked and not hit. Also sets its x and y coordinates, and who 
	 * the cell belongs to.
	 * @param owningPlayer The player who this cell belongs to.
	 * @param x The x-coordinate for this cell. Will always be in the range 0 to Game.GRID_SIZE-1, inclusive.
	 * @param y The y-coordinate for this cell. Will always be in the range 0 to Game.GRID_SIZE-1, inclusive.
	 */
	public Cell(Player owningPlayer, int x, int y) { 
	playerOwns = owningPlayer;
	xCell = x;
	yCell = y;
	}

	/** 
	 * Calculates what the user interface should display for this particular cell.
	 * The result should be one of the following four values, depending on the state of the cell:
	 *  Cell.DISPLAY_HIT - returned if this cell has been attacked, and the attack hit.
	 *  Cell.DISPLAY_MISS - returned if this cell has been attacked, but the attack missed.
	 *  Cell.DISPLAY_OCCUPIED - returned if two conditions are met:
	 *	  (a) This cell is occupied by a ship but has not been attacked.
	 *	  (b) The specified viewingPlayer is allowed to see which cells are occupied on this grid. 
	 *		  They are allowed when either: the static field UI.isCheating is true, or, 
	 *		  when the player who is viewing this cell is also the one who this cell belongs to.
	 *  Cell.DISPLAY_BLANK - returned in all other cases.
	 *
	 * @param viewingPlayer The player that is viewing this grid. 
	 * @return Returns the display state - one of Cell.DISPLAY_BLANK, Cell.DISPLAY_HIT, 
	 *  Cell.DISPLAY_MISS or Cell.DISPLAY_OCCUPIED - see above.
	 */
	public int displayState(Player viewingPlayer) { 
		if(beenAttacked == true && occupyingShip == null)
		{
			return Cell.DISPLAY_HIT;
		}
		else if(beenAttacked == true && occupyingShip !== null)
		{
			return Cell.DISPLAY_MISS;
		}
		else if(occupyingShip !== null && (UI.isCheating == true || playerOwns == viewingPlayer))
		{
			return Cell.DISPLAY_OCCUPIED;
		}
		else
		{
			return Cell.DISPLAY_BLANK;
		}

	}

	/** @return The player who this cell belongs to. */
	public Player getPlayer() {
		return playerOwns;
	}

	/** @return The ship that occupies this cell. */
	public Ship getOccupyingShip() { 
		return occupyingShip;
	}

	/** @return The x-coordinate of this cell. */
	public int getX() {
		return xCell;
	}

	/** @return The y-coordinate of this cell. */
	public int getY() { 
		return yCell;
	}

	/** 
	 * Has this cell been hit? This cell can only be hit if it meets
	 * both these conditions:
	 *  1. It is occupied.
	 *  2. It has been attacked.
	 * @return Returns true if this cell was hit, false otherwise. 
	 */
	public boolean isHit() { 
		if(beenAttacked == true && occupyingShip !== null)
		{
			cellHit = true;
		}
		else
		{
			cellHit = false;
		}
		return cellHit;
	}

	/** 
	 * Marks this cell as occupied by the specified ship. 
	 * @param ship The ship to occupy this cell with.
	 */
	public void occupyWith(Ship ship) { 
	occupyingShip = ship;
	}

	/**
	 * Attempts an attack on this cell.
	 * If this cell has been attacked before, this method should just return Cell.ATTACK_ALREADY
	 * and do nothing else. Otherwise, this method must:
	 *  1.  Mark this cell as attacked.
	 *  2.  Register one hit to the occupying ship, if there is a ship occupying this cell.
	 *  3.  Return either Cell.ATTACK_HIT, Cell.ATTACK_MISS or Cell.ATTACK_SUNK depending on 
	 *	  the outcome of the attack. See the explanations of each value below.
	 *
	 * @return Returns one of the following four values, depending on the outcome:
	 *  Cell.ATTACK_HIT - returned if the attack resulted in a hit
	 *  Cell.ATTACK_MISS - returned if the attack resulted in a miss
	 *  Cell.ATTACK_SUNK - returned if the attack resulted in a ship being sunk
	 *  Cell.ATTACK_ALREADY - returned if the specified cell has already been attacked in a previous turn.
	 */
	public int tryAttack() { 
		if(beenAttacked == true)
		{
			return Cell.ATTACK_ALREADY;
		}
		else if(occupyingShip == null)
		{
			beenAttacked = true;
			return Cell.ATTACK_MISS;
		}
		else if(occupyingShip !== null)
		{
			beenAttacked = true;
			occupyingShip.hit();
			if(occupyingShip.isSunk() == true)
			{
				return Cell.ATTACK_SUNK;
			}
			else
			{
				return Cell.ATTACK_HIT;
			}
		}		
	}

	/**
	 * Has this cell been attacked before?
	 * @return Returns true if this cell has been attacked before, false otherwise.
	 */
	public boolean wasAttacked() { 
	return beenAttacked;
	}
}

The following code is what returns the error:

public int displayState(Player viewingPlayer) { 
		if(beenAttacked == true && occupyingShip == null)
		{
			return Cell.DISPLAY_HIT;
		}
		else if(beenAttacked == true && occupyingShip !== null)
		{
			return Cell.DISPLAY_MISS;
		}
		else if(occupyingShip !== null && (UI.isCheating == true || playerOwns == viewingPlayer))
		{
			return Cell.DISPLAY_OCCUPIED;
		}
		else
		{
			return Cell.DISPLAY_BLANK;
		}

	}

I honestly cannot see why the error is occuring... I just want to get this thing finished!

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Hey Sam,

Use != instead of !==.

That's the correct way of saying "is not equals".

That *should* work (remembering to replace every !== in your code to !=).

Hope that helps!

Link to comment
Share on other sites

  • 0

Ah, sorry :D I was told that !== should be ok too, but I will change it right now and pray for it to compile. Thanks!

EDIT: Aha, it worked! Now just tidying up a couple other errors and then I am going to explode from relief.

Link to comment
Share on other sites

  • 0

	/** 
	 * Has this cell been hit? This cell can only be hit if it meets
	 * both these conditions:
	 *  1. It is occupied.
	 *  2. It has been attacked.
	 * @return Returns true if this cell was hit, false otherwise. 
	 */
	public boolean isHit() { 
		if(beenAttacked == true && occupyingShip !== null)
		{
			cellHit = true;
		}
		else
		{
			cellHit = false;
		}
		return cellHit;
	}

the following function name is not good... you are setting a value when you are asking if a the cell has been hit or not....

why cellHit scope is class level???

why not just do

 public boolean hasCellBeenHit() { 
		return (beenAttacked == true && occupyingShip != null);
 }

and remove

private boolean cellHit;

Edited by _kane81
Link to comment
Share on other sites

  • 0

also change

public int displayState(Player viewingPlayer) { 
		if(hasCellBeenHit())
		{
			return Cell.DISPLAY_HIT;
		}
		else if(!hasCellBeenHit())
		{
			return Cell.DISPLAY_MISS;
		}
		else if(occupyingShip !== null && (UI.isCheating == true || playerOwns == viewingPlayer))
		{
			return Cell.DISPLAY_OCCUPIED;
		}
		return Cell.DISPLAY_BLANK;

	}

Link to comment
Share on other sites

  • 0

suggest u check cheat mode first

if cheat (

..do cheat logic

)else (

.. do normal logic

)

^^

missed that one james :p

Link to comment
Share on other sites

  • 0

you don't need to put 'Cell' in front of ur constants if the call is in class Cell. it redundant

Cell.DISPLAY_OCCUPIED;

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.