ANGLICHE
All checks were successful
Linux arm64 / Build (push) Successful in 34s

This commit is contained in:
2025-01-10 16:14:52 +01:00
parent 77c0b0d41b
commit 8cd0f6fa12
8 changed files with 68 additions and 74 deletions

View File

@@ -1,26 +0,0 @@
package sudoku;
import java.util.ArrayList;
import java.util.List;
public class Bloc {
private final List<Case> cases;
public Bloc(List<Case> cases) {
this.cases = cases;
}
public Bloc() {
this.cases = new ArrayList<>();
}
public List<Case> getCases() {
return cases;
}
void addCase(Case newCase) {
this.cases.add(newCase);
}
}

View File

@@ -0,0 +1,26 @@
package sudoku;
import java.util.ArrayList;
import java.util.List;
public class Block {
private final List<Cell> cells;
public Block(List<Cell> cells) {
this.cells = cells;
}
public Block() {
this.cells = new ArrayList<>();
}
public List<Cell> getCells() {
return cells;
}
void addCell(Cell newCell) {
this.cells.add(newCell);
}
}

View File

@@ -1,19 +1,19 @@
package sudoku; package sudoku;
public class Case { public class Cell {
private static int NOSYMBOLE = -1; private static int NOSYMBOLE = -1;
private int symboleIndex; private int symboleIndex;
private Bloc bloc = null; private Block block = null;
public Case(int symboleIndex) { public Cell(int symboleIndex) {
this.symboleIndex = symboleIndex; this.symboleIndex = symboleIndex;
} }
/** /**
* @brief Default constructor, empty square * @brief Default constructor, empty square
*/ */
public Case() { public Cell() {
this(NOSYMBOLE); this(NOSYMBOLE);
} }
@@ -29,18 +29,19 @@ public class Case {
setSymboleIndex(NOSYMBOLE); setSymboleIndex(NOSYMBOLE);
} }
public Bloc getBloc() { public Block getBlock() {
return this.bloc; return this.block;
} }
void setBloc(Bloc bloc) { // only SudokuFactory should access this
this.bloc = bloc; void setBlock(Block block) {
this.block = block;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof Case autreCase) if (obj instanceof Cell otherCell)
return autreCase.getSymboleIndex() == this.getSymboleIndex(); return otherCell.getSymboleIndex() == this.getSymboleIndex();
return super.equals(obj); return false;
} }
} }

View File

@@ -8,29 +8,22 @@ import java.util.List;
*/ */
public class Sudoku { public class Sudoku {
private final List<Bloc> blocs; private final List<Block> blocks;
private final List<Case> cases; private final List<Cell> cells;
public Sudoku(List<Case> cases, List<Bloc> blocs) { public Sudoku(List<Cell> cells, List<Block> blocks) {
// assert(symbolCount >= 2); this.cells = cells;
// // initialisation des cases this.blocks = blocks;
// this.cases = new ArrayList<>(symbolCount * symbolCount);
// Collections.fill(this.cases, new Case());
// // initialisation des blocs
// this.blocs = new ArrayList<>(symbolCount);
// Collections.fill(this.blocs, new Bloc(null));
this.cases = cases;
this.blocs = blocs;
} }
public Case getCase(int x, int y) { public Cell getCell(int x, int y) {
int index = y * getSize() + x; int index = y * getSize() + x;
assert(index < getSize() * getSize()); assert(index < getSize() * getSize());
return this.cases.get(index); return this.cells.get(index);
} }
public int getSize() { public int getSize() {
return this.blocs.size(); return this.blocks.size();
} }
} }

View File

@@ -5,38 +5,38 @@ import java.util.List;
public class SudokuFactory { public class SudokuFactory {
private static List<Case> initCases(int size) { private static List<Cell> initCells(int size) {
List<Case> cases = new ArrayList<>(size * size); List<Cell> cells = new ArrayList<>(size * size);
for (int i = 0; i < size * size; i++) { for (int i = 0; i < size * size; i++) {
cases.add(new Case()); cells.add(new Cell());
} }
return cases; return cells;
} }
private static List<Bloc> initRectangleBlocs(List<Case> cases, int width, int height) { private static List<Block> initRectangleBlocs(List<Cell> cells, int width, int height) {
List<Bloc> blocs = new ArrayList<>(); List<Block> blocs = new ArrayList<>();
int size = width * height; int size = width * height;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Bloc newBloc = new Bloc(); Block newBlock = new Block();
int blocX = i % height; int blockX = i % height;
int blocY = i / height; int blockY = i / height;
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
int index = ((y + blocY * height) * size + (x + blocX * width)); int index = ((y + blockY * height) * size + (x + blockX * width));
Case caseBloc = cases.get(index); Cell blockCell = cells.get(index);
caseBloc.setBloc(newBloc); blockCell.setBlock(newBlock);
newBloc.addCase(caseBloc); newBlock.addCell(blockCell);
} }
} }
blocs.add(newBloc); blocs.add(newBlock);
} }
return blocs; return blocs;
} }
public static MultiDoku createBasicEmptyRectangleSudoku(int width, int height) { public static MultiDoku createBasicEmptyRectangleSudoku(int width, int height) {
int symbolCount = width * height; int symbolCount = width * height;
List<Case> cases = initCases(symbolCount); List<Cell> cases = initCells(symbolCount);
List<Bloc> blocs = initRectangleBlocs(cases, width, height); List<Block> blocs = initRectangleBlocs(cases, width, height);
Sudoku s = new Sudoku(cases, blocs); Sudoku s = new Sudoku(cases, blocs);
List<Sudoku> ss = new ArrayList<>(); List<Sudoku> ss = new ArrayList<>();
ss.add(s); ss.add(s);

View File

@@ -1,15 +1,15 @@
package sudoku.constraint; package sudoku.constraint;
import sudoku.Bloc; import sudoku.Block;
import sudoku.Case; import sudoku.Cell;
import sudoku.Sudoku; import sudoku.Sudoku;
public class BlockConstraint implements IConstraint{ public class BlockConstraint implements IConstraint{
@Override @Override
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) { public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
Bloc bloc = s.getCase(x, y).getBloc(); Block bloc = s.getCell(x, y).getBlock();
return !bloc.getCases().contains(new Case(newSymbolIndex)); return !bloc.getCells().contains(new Cell(newSymbolIndex));
} }
} }

View File

@@ -7,7 +7,7 @@ public class ColumnConstraint implements IConstraint {
@Override @Override
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) { public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
for (int i = 0; i < s.getSize(); i++) { for (int i = 0; i < s.getSize(); i++) {
if (s.getCase(x, newSymbolIndex).getSymboleIndex() == newSymbolIndex) if (s.getCell(x, newSymbolIndex).getSymboleIndex() == newSymbolIndex)
return false; return false;
} }
return true; return true;

View File

@@ -7,7 +7,7 @@ public class LineConstraint implements IConstraint {
@Override @Override
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) { public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
for (int i = 0; i < s.getSize(); i++) { for (int i = 0; i < s.getSize(); i++) {
if (s.getCase(newSymbolIndex, y).getSymboleIndex() == newSymbolIndex) if (s.getCell(newSymbolIndex, y).getSymboleIndex() == newSymbolIndex)
return false; return false;
} }
return true; return true;