[Java] How to assign a ship a status?


#1

I’m trying to make each ship contain a unique status like “collecting” or " “depositing”
But how would I try to implement that?

This is my ship class

  • = my addition

public class Ship extends Entity {
public final int halite;

  • public String status;
public Ship(final PlayerId owner, final EntityId id, final Position position, final int halite) {
    super(owner, id, position);
    this.halite = halite;
}
  • public String getStatus() {

  •   return status;
    
  • }

  • public void setStatus(String status) {

  • this.status = status;

  • }

Would this work? Or does this require more code?


#2

It seems the ships are recreated every turn, so its best you make another Map or Array,etc with key of the ship id to keep your own information that you want to persist turn to turn. really you will need to calculate a lot of different values for each ship to find optimal paths, avoid collisions and things like that.


#3

Thank you so much. I completely forgot about hashmaps. So essentially a string status would be assigned to a ship id and then it could be pulled from the hashmap. Right?


#4

yes… HashMap’s work great in Java… something like this, for more values probably want to make a ShipStatistics class…

HashMap<Integer, String> hmap = new HashMap<Integer, String>();

/Adding elements to HashMap/
hmap.put(12, “Chaitanya”);
hmap.put(2, “Rahul”);

String var= hmap.get(2);
System.out.println("Value at index 2 is: "+var);