diff --git a/README.md b/README.md index d8169c8447c883f8a41b7dc74fe9e607ad2ea3d7..60e388e0d3081f8d16c7eb2f7a7881f6ded3efa8 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,5 @@ jeu de la vie. ## Membre du projet -- NOM, prénom, du participant +- Toullec, Romain, Groupe 1 diff --git a/src/main/java/matrix/ConstantMatrixInitializer.java b/src/main/java/matrix/ConstantMatrixInitializer.java index 88d798b4b1a0e79292168f2a8d6f47024457fdae..94813378c681183b13c43f182c7a341c882d5db6 100644 --- a/src/main/java/matrix/ConstantMatrixInitializer.java +++ b/src/main/java/matrix/ConstantMatrixInitializer.java @@ -1,16 +1,15 @@ package matrix; public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> { - - // TODO: add instance variables + private final T constant; public ConstantMatrixInitializer(T constant) { - // TODO + this.constant = constant; } - @Override - public T initialValueAt(Coordinate coordinate) { - // TODO - return null; - } + @Override + public T initialValueAt(Coordinate coordinate) { + return this.constant; + } } + diff --git a/src/main/java/matrix/Coordinate.java b/src/main/java/matrix/Coordinate.java index f3e5ac009ee837b1c3998c6b1f033e42019669b6..545b494c315751e099cc3d09ccf3cc2a6a2d0c0c 100644 --- a/src/main/java/matrix/Coordinate.java +++ b/src/main/java/matrix/Coordinate.java @@ -15,8 +15,7 @@ public record Coordinate(int x, int y) { * @return A new {@link Coordinate} instance. */ public static Coordinate of(int x, int y) { - // TODO: compléter ce fabriquant - return null; + return new Coordinate(x, y); } /** @@ -25,8 +24,7 @@ public record Coordinate(int x, int y) { * @return The left adjacent {@link Coordinate}. */ public Coordinate left() { - // TODO: à compléter - return null; + return new Coordinate(x - 1, y); } /** @@ -35,8 +33,7 @@ public record Coordinate(int x, int y) { * @return The right adjacent {@link Coordinate}. */ public Coordinate right() { - // TODO: à compléter - return null; + return new Coordinate(x + 1, y); } /** @@ -45,8 +42,7 @@ public record Coordinate(int x, int y) { * @return The above adjacent {@link Coordinate}. */ public Coordinate above() { - // TODO: à compléter - return null; + return new Coordinate(x , y-1); } /** @@ -55,8 +51,7 @@ public record Coordinate(int x, int y) { * @return The below adjacent {@link Coordinate}. */ public Coordinate below() { - // TODO: à compléter - return null; + return new Coordinate(x , y+1); } /** @@ -73,8 +68,7 @@ public record Coordinate(int x, int y) { * @return A list of orthogonal neighboring {@link Coordinate}s. */ public List<Coordinate> orthogonalNeighbours() { - // TODO: à compléter - return List.of(); + return List.of(left(), right(), above(), below()); } /** @@ -92,8 +86,11 @@ public record Coordinate(int x, int y) { * @return A list of diagonal neighboring {@link Coordinate}s. */ public List<Coordinate> diagonalNeighbours() { - // TODO: à compléter - return List.of(); + return List.of( + new Coordinate(x - 1, y - 1), + new Coordinate(x + 1, y - 1), + new Coordinate(x - 1, y + 1), + new Coordinate(x + 1, y + 1)); } /** @@ -111,8 +108,9 @@ public record Coordinate(int x, int y) { * @return A list of all neighboring {@link Coordinate}s. */ public List<Coordinate> orthodiagonalNeighbours() { - // TODO: à compléter - return List.of(); + List<Coordinate> neighbours = orthogonalNeighbours(); + neighbours.addAll(diagonalNeighbours()); + return neighbours; } @Override @@ -120,11 +118,11 @@ public record Coordinate(int x, int y) { return "(" + this.x + "," + this.y + ")"; } - public Coordinate minus(Coordinate corner) { - return new Coordinate(this.x - corner.x, this.y - corner.y); - } + public Coordinate minus(Coordinate corner) { + return new Coordinate(this.x - corner.x, this.y - corner.y); + } - public Coordinate plus(Coordinate corner) { - return new Coordinate(this.x + corner.x, this.y + corner.y); - } -} \ No newline at end of file + public Coordinate plus(Coordinate corner) { + return new Coordinate(this.x + corner.x, this.y + corner.y); + } +} diff --git a/src/main/java/matrix/CoordinateIterator.java b/src/main/java/matrix/CoordinateIterator.java index 810b71287e52821943a45e52eb7959ace9df7f06..769d8a0829249c1d3d0b455c1f89f7b26c4c7bb3 100644 --- a/src/main/java/matrix/CoordinateIterator.java +++ b/src/main/java/matrix/CoordinateIterator.java @@ -3,42 +3,33 @@ package matrix; import java.util.Iterator; import java.util.NoSuchElementException; -/** - * An {@link Iterator} for generating 2D {@link Coordinate}s within a specified width and - * height range. - */ class CoordinateIterator implements Iterator<Coordinate> { + private final int width; + private final int height; + private int currentX = 0; + private int currentY = 0; - /** - * Creates a new {@link CoordinateIterator} with the specified width and height. - * - * @param width The width of the coordinate range. - * @param height The height of the coordinate range. - */ public CoordinateIterator(int width, int height) { - // TODO: à compléter + this.width = width; + this.height = height; } - /** - * Checks if there are more {@link Coordinate}s to iterate over. - * - * @return true if there are more {@link Coordinate}s; otherwise, false. - */ @Override public boolean hasNext() { - // TODO: à compléter - return false; + return currentY < height; } - /** - * Returns the next {@link Coordinate} in the iteration. - * - * @return The next {@link Coordinate} in the iteration. - * @throws NoSuchElementException if there are no more {@link Coordinate}s to iterate over. - */ @Override public Coordinate next() { - // TODO: à compléter - return null; + if (!hasNext()) { + throw new NoSuchElementException(); + } + Coordinate coord = new Coordinate(currentX, currentY); + currentX++; + if (currentX >= width) { + currentX = 0; + currentY++; + } + return coord; } } diff --git a/src/main/java/matrix/ListMatrix.java b/src/main/java/matrix/ListMatrix.java index a3a4e7d562c43dded22737a89919f85f5eca6040..ff7e0311d3caea608f47e268e6da4669e9553d10 100644 --- a/src/main/java/matrix/ListMatrix.java +++ b/src/main/java/matrix/ListMatrix.java @@ -1,63 +1,62 @@ package matrix; +import java.util.ArrayList; import java.util.List; - -/** - * Represents a matrix, a rectangular array, with generic values in each cell. - * - * @param <T> The type of values stored in the matrix cells. - */ public class ListMatrix<T> implements Matrix<T> { - private final List<List<T>> matrix; private final int width; private final int height; - /** - * Creates a new {@link ListMatrix} with the specified width, height, and an initializer to set - * values. - * - * @param width The width of the {@link ListMatrix}. - * @param height The height of the {@link ListMatrix}. - * @param initializer A matrix initializer to set values in the {@link ListMatrix}. - */ public ListMatrix(int width, int height, MatrixInitializer<T> initializer) { - // TODO - this.width = 0; - this.height = 0; - this.matrix = null; - this.initializeWith(initializer); // fills the matrix using initializer + this.width = width; + this.height = height; + matrix = new ArrayList<>(height); + for (int y = 0; y < height; y++) { + List<T> row = new ArrayList<>(width); + for (int x = 0; x < width; x++) { + row.add(initializer.initialValueAt(new Coordinate(x, y))); + } + matrix.add(row); + } } public ListMatrix(int width, int height, T constant) { this(width, height, new ConstantMatrixInitializer<>(constant)); } - private void initializeWith(MatrixInitializer<T> initializer) { - // TODO initialize each cell of the matrix, with a value determined by initializer - } - + @Override public int width() { - // TODO - return 0; + return width; } + @Override public int height() { - // TODO - return 0; + return height; } @Override public T get(int x, int y) { - // TODO - return null; + return matrix.get(y).get(x); } - @Override public void set(int x, int y, T newValue) { - // TODO + matrix.get(y).set(x, newValue); } + @Override + public T get(Coordinate coordinate) { + return get(coordinate.x(), coordinate.y()); + } + + @Override + public void set(Coordinate coordinate, T newValue) { + set(coordinate.x(), coordinate.y(), newValue); + } + + @Override + public Matrix<T> subMatrix(Coordinate corner, int subWidth, int subHeight) { + return new ListMatrix<>(subWidth, subHeight, new SubMatrixInitializer<>(this, corner)); + } }