Skip to content
Snippets Groups Projects
Commit 78c69d96 authored by TOULLEC Romain's avatar TOULLEC Romain
Browse files

classe matrix

parent e2517c6c
No related branches found
No related tags found
No related merge requests found
Pipeline #38375 failed
...@@ -19,5 +19,5 @@ jeu de la vie. ...@@ -19,5 +19,5 @@ jeu de la vie.
## Membre du projet ## Membre du projet
- NOM, prénom, du participant - Toullec, Romain, Groupe 1
package matrix; package matrix;
public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> { public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> {
private final T constant;
// TODO: add instance variables
public ConstantMatrixInitializer(T constant) { public ConstantMatrixInitializer(T constant) {
// TODO this.constant = constant;
} }
@Override @Override
public T initialValueAt(Coordinate coordinate) { public T initialValueAt(Coordinate coordinate) {
// TODO return this.constant;
return null;
} }
} }
...@@ -15,8 +15,7 @@ public record Coordinate(int x, int y) { ...@@ -15,8 +15,7 @@ public record Coordinate(int x, int y) {
* @return A new {@link Coordinate} instance. * @return A new {@link Coordinate} instance.
*/ */
public static Coordinate of(int x, int y) { public static Coordinate of(int x, int y) {
// TODO: compléter ce fabriquant return new Coordinate(x, y);
return null;
} }
/** /**
...@@ -25,8 +24,7 @@ public record Coordinate(int x, int y) { ...@@ -25,8 +24,7 @@ public record Coordinate(int x, int y) {
* @return The left adjacent {@link Coordinate}. * @return The left adjacent {@link Coordinate}.
*/ */
public Coordinate left() { public Coordinate left() {
// TODO: à compléter return new Coordinate(x - 1, y);
return null;
} }
/** /**
...@@ -35,8 +33,7 @@ public record Coordinate(int x, int y) { ...@@ -35,8 +33,7 @@ public record Coordinate(int x, int y) {
* @return The right adjacent {@link Coordinate}. * @return The right adjacent {@link Coordinate}.
*/ */
public Coordinate right() { public Coordinate right() {
// TODO: à compléter return new Coordinate(x + 1, y);
return null;
} }
/** /**
...@@ -45,8 +42,7 @@ public record Coordinate(int x, int y) { ...@@ -45,8 +42,7 @@ public record Coordinate(int x, int y) {
* @return The above adjacent {@link Coordinate}. * @return The above adjacent {@link Coordinate}.
*/ */
public Coordinate above() { public Coordinate above() {
// TODO: à compléter return new Coordinate(x , y-1);
return null;
} }
/** /**
...@@ -55,8 +51,7 @@ public record Coordinate(int x, int y) { ...@@ -55,8 +51,7 @@ public record Coordinate(int x, int y) {
* @return The below adjacent {@link Coordinate}. * @return The below adjacent {@link Coordinate}.
*/ */
public Coordinate below() { public Coordinate below() {
// TODO: à compléter return new Coordinate(x , y+1);
return null;
} }
/** /**
...@@ -73,8 +68,7 @@ public record Coordinate(int x, int y) { ...@@ -73,8 +68,7 @@ public record Coordinate(int x, int y) {
* @return A list of orthogonal neighboring {@link Coordinate}s. * @return A list of orthogonal neighboring {@link Coordinate}s.
*/ */
public List<Coordinate> orthogonalNeighbours() { public List<Coordinate> orthogonalNeighbours() {
// TODO: à compléter return List.of(left(), right(), above(), below());
return List.of();
} }
/** /**
...@@ -92,8 +86,11 @@ public record Coordinate(int x, int y) { ...@@ -92,8 +86,11 @@ public record Coordinate(int x, int y) {
* @return A list of diagonal neighboring {@link Coordinate}s. * @return A list of diagonal neighboring {@link Coordinate}s.
*/ */
public List<Coordinate> diagonalNeighbours() { 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) { ...@@ -111,8 +108,9 @@ public record Coordinate(int x, int y) {
* @return A list of all neighboring {@link Coordinate}s. * @return A list of all neighboring {@link Coordinate}s.
*/ */
public List<Coordinate> orthodiagonalNeighbours() { public List<Coordinate> orthodiagonalNeighbours() {
// TODO: à compléter List<Coordinate> neighbours = orthogonalNeighbours();
return List.of(); neighbours.addAll(diagonalNeighbours());
return neighbours;
} }
@Override @Override
......
...@@ -3,42 +3,33 @@ package matrix; ...@@ -3,42 +3,33 @@ package matrix;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; 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> { 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) { 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 @Override
public boolean hasNext() { public boolean hasNext() {
// TODO: à compléter return currentY < height;
return false;
} }
/**
* 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 @Override
public Coordinate next() { public Coordinate next() {
// TODO: à compléter if (!hasNext()) {
return null; throw new NoSuchElementException();
}
Coordinate coord = new Coordinate(currentX, currentY);
currentX++;
if (currentX >= width) {
currentX = 0;
currentY++;
}
return coord;
} }
} }
package matrix; package matrix;
import java.util.ArrayList;
import java.util.List; 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> { public class ListMatrix<T> implements Matrix<T> {
private final List<List<T>> matrix; private final List<List<T>> matrix;
private final int width; private final int width;
private final int height; 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) { public ListMatrix(int width, int height, MatrixInitializer<T> initializer) {
// TODO this.width = width;
this.width = 0; this.height = height;
this.height = 0; matrix = new ArrayList<>(height);
this.matrix = null; for (int y = 0; y < height; y++) {
this.initializeWith(initializer); // fills the matrix using initializer 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) { public ListMatrix(int width, int height, T constant) {
this(width, height, new ConstantMatrixInitializer<>(constant)); this(width, height, new ConstantMatrixInitializer<>(constant));
} }
private void initializeWith(MatrixInitializer<T> initializer) { @Override
// TODO initialize each cell of the matrix, with a value determined by initializer
}
public int width() { public int width() {
// TODO return width;
return 0;
} }
@Override
public int height() { public int height() {
// TODO return height;
return 0;
} }
@Override @Override
public T get(int x, int y) { public T get(int x, int y) {
// TODO return matrix.get(y).get(x);
return null;
} }
@Override @Override
public void set(int x, int y, T newValue) { 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));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment