Sha256: 7954617da855de073e280ed6b88ace7099ac2f5dab3f0cdcdfb579bfb63925fc

Contents?: true

Size: 840 Bytes

Versions: 188

Compression:

Stored size: 840 Bytes

Contents

class SpiralMatrixBuilder {

    int[][] buildMatrixOfSize(int size) {
        if (size == 0) {
            return new int[][]{};
        }

        int[][] result = new int[size][size];
        int entryCount = (int) Math.pow(size, 2.0);

        Coordinate coord = new Coordinate(0, 0);
        Direction direction = Direction.RIGHT;

        for (int i = 0; i < entryCount; i++) {
            result[coord.getY()][coord.getX()] = i + 1;

            Coordinate maybeNextCoord = coord.step(direction);
            if (maybeNextCoord.isWithinGridOfSize(size) && result[maybeNextCoord.getY()][maybeNextCoord.getX()] == 0) {
                coord = maybeNextCoord;
            } else {
                direction = direction.turnRight();
                coord = coord.step(direction);
            }
        }

        return result;
    }

}

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.138 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.137 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.136 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.135 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.134 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.133 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.132 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.131 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.130 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.129 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.128 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.127 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.126 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.125 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.124 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.123 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.122 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.121 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java
trackler-2.2.1.120 tracks/java/exercises/spiral-matrix/.meta/src/reference/java/SpiralMatrixBuilder.java