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.
## Membre du projet
- NOM, prénom, du participant
- Toullec, Romain, Groupe 1
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;
}
}
......@@ -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);
}
}
......@@ -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;
}
}
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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment