https://www.codingame.com/multiplayer/bot-programming/tulips-and-daisies

Send your feedback or ask for help here!

Created by @WildSmilodon,validated by @field3,@EricSMSO and @Illedan.

If you have any issues, feel free to ping them.

https://www.codingame.com/multiplayer/bot-programming/tulips-and-daisies

Send your feedback or ask for help here!

Created by @WildSmilodon,validated by @field3,@EricSMSO and @Illedan.

If you have any issues, feel free to ping them.

Hello everybody,

I am having some problems with this puzzle.

I have checked the boundaries with the following code (java):

```
if (flowerX > fieldWidth) {
flowerX = Math.abs(flowerX - fieldWidth);
}
if (flowerY > fieldHeight) {
flowerY = Math.abs(flowerY - fieldHeight);
}
```

Where (flowerX, flowerY) is the position of flower to be planted.

But I keep getting the error of invalid location.

What am I doing wrong?

I could not think anything.

Thanks

Hey @kdhakal, it might be that flowerX > 2 * fieldWidth so subtracting fieldWidth will not decrease it sufficiently.

Also, why the Math.abs()? if it is greater, the value will always be positive so the abs is not needed.

2 Likes

Thanks for the reply @WINWINWIN,

So how do I make the value of flowerX sufficiently under fieldWidth?

Regarding Math.abs(), I have realised that it is not necessary and I’ve already removed it.

**Edit:** I just had a look at my code and I realised that I am using the formula

```
double f = Math.random()/Math.nextDown(1.0);
double x = x1*(1.0 - f) + x2*f;
```

to generate the random position numbers between x1 and x2, which implies that I should not even need to check the boundaries!

But still I am getting the invalid location error.

Other than side errors like a x += … somewhere, the only potential reason that I can think of is that the value of f is at an extreme --> 0.0 or 1.0, x will be x1 or x2 respectively.

In this case, I am not sure if x = x1 or x = x2 is allowed. But this will be a rare situation, so if such errors are not common, this is the likely explanation.

But if they are not, this is probably not where your error is.

Check the parts of the code where x is used and see if you are performing an operation on x which puts it out of bounds.

1 Like

Co-ordinates are 0 indexed so x == fieldWidth is out of bounds (just change your > to >=).

3 Likes

The game has crashed. Please contact the author and enclose the following error:

java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

at com.codingame.game.Player.getAction(Player.java:31)

at com.codingame.game.Referee.gameTurn(Referee.java:116)

at com.codingame.gameengine.core.GameManager.start(GameManager.java:122)

at com.codingame.gameengine.core.RefereeMain.start(RefereeMain.java:67)

at com.codingame.gameengine.core.RefereeMain.main(RefereeMain.java:50