Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kore-2022 Java interpreter errors when wrapping #192

Open
JakeForsey opened this issue Apr 19, 2022 · 4 comments
Open

Kore-2022 Java interpreter errors when wrapping #192

JakeForsey opened this issue Apr 19, 2022 · 4 comments

Comments

@JakeForsey
Copy link

I think I've found a bug when using the Board::next() function when there is a fleet on the edge of the map.

Below is a test I wrote to reproduce, looks like the code that handles positions / indexes is not quite right. I might have a play and see if I can find the bug. Help appreciated though, this would be the nail in the coffin of my java based approach :(

...

import java.util.List;
import kore.Player;

public class BoardTest {

    @Test
    public void fleetWrapsAroundTheBoard_northSouth() throws IOException {
        Board board = getStarterBoard();
        
        int x = board.size / 2;
        int playerId = 0;

        Point p1 = new Point(x, board.size);
        Fleet f1 = new Fleet("f1", 50, Direction.NORTH, p1, 100.0, "N", playerId, board);
        final String f1Id = f1.id;
        board.addFleet(f1);

        Board nextBoard = board.next();

        Player player0 = nextBoard.players[playerId];
        Fleet nextF1 = List.of(player0.fleets()).stream().filter(fleet -> fleet.id == f1Id).findFirst().get();

        Assert.assertEquals("position.x should not change when wrapping", nextF1.position.x, x);
        Assert.assertEquals("position.y should wrap to 0", nextF1.position.y, 0);
    }
...
java.lang.ArrayIndexOutOfBoundsException: Index -16 out of bounds for length 961
 at kore.Board.getCellAtPosition([Board.java:115]())
 at kore.Fleet.cell([Fleet.java:30]())
 at kore.Board.addFleet([Board.java:120]())
 at test.BoardTest.fleetWrapsAroundTheBoard_northSouth([BoardTest.java:571]())
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke([NativeMethodAccessorImpl.java:62]())
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke([DelegatingMethodAccessorImpl.java:43]())
 at java.base/java.lang.reflect.Method.invoke([Method.java:566]())
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall([FrameworkMethod.java:59]())
 at org.junit.internal.runners.model.ReflectiveCallable.run([ReflectiveCallable.java:12]())
 at org.junit.runners.model.FrameworkMethod.invokeExplosively([FrameworkMethod.java:56]())
 at org.junit.internal.runners.statements.InvokeMethod.evaluate([InvokeMethod.java:17]())
 at org.junit.runners.ParentRunner$3.evaluate([ParentRunner.java:306]())
 at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate([BlockJUnit4ClassRunner.java:100]())
 at org.junit.runners.ParentRunner.runLeaf([ParentRunner.java:366]())
 at org.junit.runners.BlockJUnit4ClassRunner.runChild([BlockJUnit4ClassRunner.java:103]())
 at org.junit.runners.BlockJUnit4ClassRunner.runChild([BlockJUnit4ClassRunner.java:63]())
 at org.junit.runners.ParentRunner$4.run([ParentRunner.java:331]())
 at org.junit.runners.ParentRunner$1.schedule([ParentRunner.java:79]())
 at org.junit.runners.ParentRunner.runChildren([ParentRunner.java:329]())
 at org.junit.runners.ParentRunner.access$100([ParentRunner.java:66]())
 at org.junit.runners.ParentRunner$2.evaluate([ParentRunner.java:293]())
 at org.junit.runners.ParentRunner$3.evaluate([ParentRunner.java:306]())
 at org.junit.runners.ParentRunner.run([ParentRunner.java:413]())
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run([JUnit4TestReference.java:89]())
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run([TestExecution.java:40]())
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests([RemoteTestRunner.java:529]())
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests([RemoteTestRunner.java:756]())
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run([RemoteTestRunner.java:452]())
@JakeForsey
Copy link
Author

JakeForsey commented Apr 19, 2022

Nevermind, bad test :D

Fixed with (incase you want to add the test...):

        Point p1 = new Point(x, board.size - 1);

@JakeForsey
Copy link
Author

Reopened as the problem does exist when wrapping to the south.

    @Test
    public void fleetWrapsAroundTheBoard_southNorth() throws IOException {
        Board board = getStarterBoard();
        
        int x = board.size / 2;
        int playerId = 0;

        Point p1 = new Point(x, 0);
        Fleet f1 = new Fleet("f1", 50, Direction.SOUTH, p1, 100.0, "S", playerId, board);
        final String f1Id = f1.id;
        board.addFleet(f1);

        Board nextBoard = board.next();

        Player player0 = nextBoard.players[playerId];
        Fleet nextF1 = List.of(player0.fleets()).stream().filter(fleet -> fleet.id == f1Id).findFirst().get();

        Assert.assertEquals("position.x should not change when wrapping south -> north", nextF1.position.x, x);
        Assert.assertEquals("position.y should wrap to board.size - 1", nextF1.position.y, board.size - 1);
    }
java.lang.ArrayIndexOutOfBoundsException: Index 976 out of bounds for length 961
 at kore.Board.getCellAtPosition([Board.java:115]())
 at kore.Fleet.cell([Fleet.java:30]())
 at kore.Board.next([Board.java:382]())
 at test.BoardTest.fleetWrapsAroundTheBoard_southNorth([BoardTest.java:594]())
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke([NativeMethodAccessorImpl.java:62]())
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke([DelegatingMethodAccessorImpl.java:43]())
 at java.base/java.lang.reflect.Method.invoke([Method.java:566]())
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall([FrameworkMethod.java:59]())
 at org.junit.internal.runners.model.ReflectiveCallable.run([ReflectiveCallable.java:12]())
 at org.junit.runners.model.FrameworkMethod.invokeExplosively([FrameworkMethod.java:56]())
 at org.junit.internal.runners.statements.InvokeMethod.evaluate([InvokeMethod.java:17]())
 at org.junit.runners.ParentRunner$3.evaluate([ParentRunner.java:306]())
 at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate([BlockJUnit4ClassRunner.java:100]())
 at org.junit.runners.ParentRunner.runLeaf([ParentRunner.java:366]())
 at org.junit.runners.BlockJUnit4ClassRunner.runChild([BlockJUnit4ClassRunner.java:103]())
 at org.junit.runners.BlockJUnit4ClassRunner.runChild([BlockJUnit4ClassRunner.java:63]())
 at org.junit.runners.ParentRunner$4.run([ParentRunner.java:331]())
 at org.junit.runners.ParentRunner$1.schedule([ParentRunner.java:79]())
 at org.junit.runners.ParentRunner.runChildren([ParentRunner.java:329]())
 at org.junit.runners.ParentRunner.access$100([ParentRunner.java:66]())

@JakeForsey JakeForsey reopened this Apr 19, 2022
@JakeForsey
Copy link
Author

I think I fixed this here:

with:

        return new Point(Math.floorMod(this.x, size), Math.floorMod(this.y, size));

Validated with these tests:

    @Test
    public void translate_wrapsNorthToSouth() {
        int size = 31;
        int x = 10;

        Point point = new Point(x, 0);
        Point offset = new Point(0, -1);

        Point nextPoint = point.translate(offset, size);

        Assert.assertEquals("y should wrap to the top", size - 1, nextPoint.y);
        Assert.assertEquals("There should be no change in x", x, nextPoint.x);
    }
    
    @Test
    public void translate_wrapsSouthToNorth() {
        int size = 31;
        int x = 10;

        Point point = new Point(x, size - 1);
        Point offset = new Point(0, 1);

        Point nextPoint = point.translate(offset, size);

        Assert.assertEquals("y should wrap to the bottom", 0, nextPoint.y);
        Assert.assertEquals("There should be no change in x", x, nextPoint.x);
    }

@paradite
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants