# How to create a ship temp variable

#1

I want to make my ships all suicide at the end. So my algorithm is to find the ship with the lowest amount of halite and then create a temp ship variable to store that ship. Therefore, I can tell it to just survive until the very end after all other ships have sunken

#2

You should just be able to create a variable outside the game loop and store it there.

#3

How would I initialize the variable?
Ship survivor = new Ship(parameters)?
but that gives me an error.

#4

why can’t you just do Ship survivor = (name of ship variable)?

#5

Since, the temp ship survivor is outside the game loop and my for each ship loop is inside the game loop (where I would be setting the temp ship survivor variable to the ship that should be the last one remaining). I can’t set it, right?

#6

I don’t understand.

``````Ship survive = null;
while (true) {
//update game frame, etc.
for (Ship s : me.ships.values()) {
if(someCondition(s) && s != null) {
survive = s;
}

if (s.equals(survive)) {
//do fancy things to make your ship survive!
}
}
}``````

#7

Im so sorry, my mind went autopilot there. I completely forgot about null. Thank you so much for your time and help!

#8

Haha, don’t worry, no problem! It happens to all of us. Just watch out for NullPointerExceptions…

#9

You dont have to have a ship survive. That rule is a only if you have less than 1000 halite. As long as you have enough halite to spawn a ship, you dont have to have a surviving ship.

#10

What you are saying is, at the last turn. I can add a spawn ship command after every other ship has suicided?

#11

He’s saying that you don’t need to have a Ship alive as long as you have >= 1000 Halite.

#12

But wouldn’t it be waste 1000 halite? Since the surviving ship wouldn’t be the one with anything close to 1000 halite in it’s cargo.

#13

Question is, why would you want to have one Ship alive?
If “answer” equals “to not error out”: -> Read my previous Answer. (You refers to You, the Player, not the Ship)

#14

Thank you for the clarification. I’ve always thought that you lose whenever you lose all your ships. But i missed this line: “does not have a ship or enough energy to construct a ship.” Thanks again!