This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
26
app/src/main/java/sudoku/Block.java
Normal file
26
app/src/main/java/sudoku/Block.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
package sudoku;
|
||||
|
||||
public class Case {
|
||||
public class Cell {
|
||||
|
||||
private static int NOSYMBOLE = -1;
|
||||
private int symboleIndex;
|
||||
private Bloc bloc = null;
|
||||
private Block block = null;
|
||||
|
||||
public Case(int symboleIndex) {
|
||||
public Cell(int symboleIndex) {
|
||||
this.symboleIndex = symboleIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Default constructor, empty square
|
||||
*/
|
||||
public Case() {
|
||||
public Cell() {
|
||||
this(NOSYMBOLE);
|
||||
}
|
||||
|
||||
@@ -29,18 +29,19 @@ public class Case {
|
||||
setSymboleIndex(NOSYMBOLE);
|
||||
}
|
||||
|
||||
public Bloc getBloc() {
|
||||
return this.bloc;
|
||||
public Block getBlock() {
|
||||
return this.block;
|
||||
}
|
||||
|
||||
void setBloc(Bloc bloc) {
|
||||
this.bloc = bloc;
|
||||
// only SudokuFactory should access this
|
||||
void setBlock(Block block) {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Case autreCase)
|
||||
return autreCase.getSymboleIndex() == this.getSymboleIndex();
|
||||
return super.equals(obj);
|
||||
if (obj instanceof Cell otherCell)
|
||||
return otherCell.getSymboleIndex() == this.getSymboleIndex();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -8,29 +8,22 @@ import java.util.List;
|
||||
*/
|
||||
public class Sudoku {
|
||||
|
||||
private final List<Bloc> blocs;
|
||||
private final List<Case> cases;
|
||||
private final List<Block> blocks;
|
||||
private final List<Cell> cells;
|
||||
|
||||
public Sudoku(List<Case> cases, List<Bloc> blocs) {
|
||||
// assert(symbolCount >= 2);
|
||||
// // initialisation des cases
|
||||
// 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 Sudoku(List<Cell> cells, List<Block> blocks) {
|
||||
this.cells = cells;
|
||||
this.blocks = blocks;
|
||||
}
|
||||
|
||||
public Case getCase(int x, int y) {
|
||||
public Cell getCell(int x, int y) {
|
||||
int index = y * getSize() + x;
|
||||
assert(index < getSize() * getSize());
|
||||
return this.cases.get(index);
|
||||
return this.cells.get(index);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return this.blocs.size();
|
||||
return this.blocks.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,38 +5,38 @@ import java.util.List;
|
||||
|
||||
public class SudokuFactory {
|
||||
|
||||
private static List<Case> initCases(int size) {
|
||||
List<Case> cases = new ArrayList<>(size * size);
|
||||
private static List<Cell> initCells(int size) {
|
||||
List<Cell> cells = new ArrayList<>(size * size);
|
||||
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) {
|
||||
List<Bloc> blocs = new ArrayList<>();
|
||||
private static List<Block> initRectangleBlocs(List<Cell> cells, int width, int height) {
|
||||
List<Block> blocs = new ArrayList<>();
|
||||
int size = width * height;
|
||||
for (int i = 0; i < size; i++) {
|
||||
Bloc newBloc = new Bloc();
|
||||
int blocX = i % height;
|
||||
int blocY = i / height;
|
||||
Block newBlock = new Block();
|
||||
int blockX = i % height;
|
||||
int blockY = i / height;
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
int index = ((y + blocY * height) * size + (x + blocX * width));
|
||||
Case caseBloc = cases.get(index);
|
||||
caseBloc.setBloc(newBloc);
|
||||
newBloc.addCase(caseBloc);
|
||||
int index = ((y + blockY * height) * size + (x + blockX * width));
|
||||
Cell blockCell = cells.get(index);
|
||||
blockCell.setBlock(newBlock);
|
||||
newBlock.addCell(blockCell);
|
||||
}
|
||||
}
|
||||
blocs.add(newBloc);
|
||||
blocs.add(newBlock);
|
||||
}
|
||||
return blocs;
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptyRectangleSudoku(int width, int height) {
|
||||
int symbolCount = width * height;
|
||||
List<Case> cases = initCases(symbolCount);
|
||||
List<Bloc> blocs = initRectangleBlocs(cases, width, height);
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
List<Block> blocs = initRectangleBlocs(cases, width, height);
|
||||
Sudoku s = new Sudoku(cases, blocs);
|
||||
List<Sudoku> ss = new ArrayList<>();
|
||||
ss.add(s);
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package sudoku.constraint;
|
||||
|
||||
import sudoku.Bloc;
|
||||
import sudoku.Case;
|
||||
import sudoku.Block;
|
||||
import sudoku.Cell;
|
||||
import sudoku.Sudoku;
|
||||
|
||||
public class BlockConstraint implements IConstraint{
|
||||
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
Bloc bloc = s.getCase(x, y).getBloc();
|
||||
return !bloc.getCases().contains(new Case(newSymbolIndex));
|
||||
Block bloc = s.getCell(x, y).getBlock();
|
||||
return !bloc.getCells().contains(new Cell(newSymbolIndex));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class ColumnConstraint implements IConstraint {
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
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 true;
|
||||
|
||||
@@ -7,7 +7,7 @@ public class LineConstraint implements IConstraint {
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
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 true;
|
||||
|
||||
Reference in New Issue
Block a user