ajout logique du sudoku

ludo
ludovic.castglia 8 months ago
parent 6e70e3ccbd
commit e0c256d835

@ -1,14 +1,7 @@
:host {
--cellSize: 50px;
--color1: #f00;
--color2: #0f0;
--color3: #00f;
--color4: #ff0;
--color5: #f0f;
--color6: #0ff;
--color7: #f77;
--color8: #7f7;
--color9: #77f;
--color: #ddd;
--color-faux: rgba(255,0,0,0.4);
}
.grille {
@ -33,44 +26,43 @@
width: var(--cellSize);
text-align: center;
outline: none;
background-color: transparent;
}
.grille {
display: flex;
}
.column:nth-child(-n+9) div:nth-child(-n+9) {
border: solid 1px var(--color1);
.grid, .subgrid {
display: grid;
height: max-content;
width: max-content;
}
.column:nth-child(-n+9) div:nth-child(-n+6) {
border: solid 1px var(--color2);
.sub0, .sub1, .sub2, .y0, .y1, .y2 {
grid-row: 1/2;
}
.column:nth-child(-n+9) div:nth-child(-n+3) {
border: solid 1px var(--color3);
.sub3 , .sub4, .sub5, .y3, .y4, .y5{
grid-row: 2/3;
}
.column:nth-child(-n+6) div:nth-child(-n+9) {
border: solid 1px var(--color4);
.sub6 , .sub7, .sub8, .y6, .y7, .y8 {
grid-row: 3/4;
}
.column:nth-child(-n+6) div:nth-child(-n+6) {
border: solid 1px var(--color5);
.sub0, .sub3, .sub6, .y0, .y3, .y6 {
grid-column: 1/2;
}
.column:nth-child(-n+6) div:nth-child(-n+3) {
border: solid 1px var(--color6);
.sub1, .sub4, .sub7, .y1, .y4, .y7 {
grid-column: 2/3;
}
.column:nth-child(-n+3) div:nth-child(-n+9) {
border: solid 1px var(--color7);
.sub2, .sub5, .sub8, .y2, .y5, .y8 {
grid-column: 3/4;
}
.column:nth-child(-n+3) div:nth-child(-n+6) {
border: solid 1px var(--color8);
.sub1, .sub3, .sub5, .sub7 {
background-color: var(--color);
}
.column:nth-child(-n+3) div:nth-child(-n+3) {
border: solid 1px var(--color9);
.faux {
background-color: var(--color-faux);
}

@ -1,11 +1,10 @@
<p>sudoku works!</p>
<div class="grille">
<div *ngFor="let row of grille" class="column">
<div *ngFor="let cell of row" class="row">
<input *ngIf="cell != 0" type="number" value="{{cell}}" class="cell" max="9" min="0">
<input *ngIf="cell == 0" type="number" value="" class="cell" max="9" min="0">
<div class="grid">
<div *ngFor="let row of grille; let x = index" class="subgrid sub{{x}}">
<div *ngFor="let cell of row; let y = index" class="case x{{x}} y{{y}}">
<input *ngIf="cell != 0" type="number" value="{{cell}}" class="cell" max="9" min="0" id="xy{{x}}{{y}}">
<input *ngIf="cell == 0" type="number" value="" class="cell" max="9" min="0" id="xy{{x}}{{y}}">
</div>
</div>
</div>

@ -35,12 +35,47 @@ export class SudokuComponent {
this.grille = this.sudokuService.getEasy();
break;
}
console.log(this.grille);
}
public publish() {
}
public corriger() {
this.resetError();
if (this.sudokuService.isCorrect(this.getGrid())) {
alert("c'est correct mon ptit");
return;
}
let error: number[][] = this.sudokuService.validationGrille(this.getGrid());
for (let y=0; y<error.length; y++) {
for (let x=0; x<error[y].length; x++) {
if (error[y][x] == -1) {
this.marqueError(x,y);
}
}
}
}
private getGrid():number[][] {
let grid: number[][] = [];
for (let y=0; y<9; y++) {
grid.push([]);
for (let x=0; x<9; x++) {
grid[y].push(Number((document.getElementById("xy"+y.toString()+x.toString()) as HTMLInputElement).value) | 0);
}
}
return grid;
}
private marqueError(x:number,y:number) {
let error = document.getElementById("xy"+y.toString()+x.toString());
if (error == null) return;
error.classList.add("faux");
}
private resetError() {
let errors = document.getElementsByClassName("faux");
for (let i=0; i<errors.length; i++) {
errors[i].classList.remove("faux");
}
}
}

@ -23,10 +23,10 @@ export class SudokuService {
this.hard = jsonResponse["hard"];
});
this.correction = [[3,8,7,1,2,6,5,4,9],[2,1,6,9,4,5,8,7,3],[5,9,4,3,8,7,6,2,1],[4,2,8,5,9,1,7,3,6],[7,5,1,2,6,3,9,8,4],[6,3,9,8,7,4,1,5,2],[1,7,5,6,3,2,4,9,8],[9,4,3,7,1,8,2,6,5],[8,6,2,4,5,9,3,1,7]];
this.easy = [[3,8,0,1,2,0,5,0,9],[2,1,6,9,0,0,8,0,0],[5,9,4,3,8,0,0,0,1],[4,2,8,5,0,0,7,3,6],[7,5,1,2,6,3,9,0,4],[0,3,0,0,0,0,1,5,2],[1,7,5,6,3,0,4,9,0],[9,4,3,0,1,8,0,0,5],[8,6,0,4,5,0,3,1,7]];
this.medium = [[3,0,0,0,0,0,0,4,0],[0,1,6,0,4,5,0,0,3],[0,9,0,3,8,0,0,2,0],[4,2,0,5,0,0,0,3,6],[7,5,0,2,6,0,0,8,4],[0,0,0,0,0,0,0,5,0],[1,0,5,6,3,0,0,9,8],[0,0,0,7,1,8,0,6,5],[0,0,2,4,0,0,0,0,0]];
this.hard = [[0,0,0,1,0,0,0,0,0],[0,1,6,9,0,0,8,0,0],[0,0,0,0,8,0,6,0,0],[0,0,0,5,0,0,0,0,0],[0,0,0,0,6,3,0,0,0],[0,0,0,0,0,0,0,0,0],[0,7,5,0,0,0,0,0,0],[9,0,3,0,0,0,0,0,5],[8,0,0,0,5,0,3,1,0]];
this.correction = this.transformFormat([[3,8,7,1,2,6,5,4,9],[2,1,6,9,4,5,8,7,3],[5,9,4,3,8,7,6,2,1],[4,2,8,5,9,1,7,3,6],[7,5,1,2,6,3,9,8,4],[6,3,9,8,7,4,1,5,2],[1,7,5,6,3,2,4,9,8],[9,4,3,7,1,8,2,6,5],[8,6,2,4,5,9,3,1,7]]);
this.easy = this.transformFormat([[3,8,0,1,2,0,5,0,9],[2,1,6,9,0,0,8,0,0],[5,9,4,3,8,0,0,0,1],[4,2,8,5,0,0,7,3,6],[7,5,1,2,6,3,9,0,4],[0,3,0,0,0,0,1,5,2],[1,7,5,6,3,0,4,9,0],[9,4,3,0,1,8,0,0,5],[8,6,0,4,5,0,3,1,7]]);
this.medium = this.transformFormat([[3,0,0,0,0,0,0,4,0],[0,1,6,0,4,5,0,0,3],[0,9,0,3,8,0,0,2,0],[4,2,0,5,0,0,0,3,6],[7,5,0,2,6,0,0,8,4],[0,0,0,0,0,0,0,5,0],[1,0,5,6,3,0,0,9,8],[0,0,0,7,1,8,0,6,5],[0,0,2,4,0,0,0,0,0]]);
this.hard = this.transformFormat([[0,0,0,1,0,0,0,0,0],[0,1,6,9,0,0,8,0,0],[0,0,0,0,8,0,6,0,0],[0,0,0,5,0,0,0,0,0],[0,0,0,0,6,3,0,0,0],[0,0,0,0,0,0,0,0,0],[0,7,5,0,0,0,0,0,0],[9,0,3,0,0,0,0,0,5],[8,0,0,0,5,0,3,1,0]]);
}
public getCorrection(): number[][]{
@ -47,16 +47,15 @@ export class SudokuService {
public validationGrille(userGrille: number[][]): number[][] {
let validation: number[][] = [];
for (let x:number = 0; x<userGrille.length; x++) {
for (let y:number = 0; y<userGrille.length; y++) {
validation.push([]);
for (let y:number = 0; y<userGrille[x].length; y++) {
if (userGrille[x][y] == 0) {
validation[x].push(0);
}
if (userGrille[x][y] == this.correction[x][y]) {
validation[x].push(1);
for (let x:number = 0; x<userGrille[y].length; x++) {
if (userGrille[y][x] == 0) {
validation[y].push(0);
} else if (userGrille[y][x] == this.correction[y][x]) {
validation[y].push(1);
} else {
validation[x].push(-1);
validation[y].push(-1);
}
}
}
@ -66,10 +65,34 @@ export class SudokuService {
public isCorrect(userGrille: number[][]): boolean {
for (let x:number = 0; x<userGrille.length; x++) {
for (let y:number = 0; y<userGrille[x].length; y++) {
if (userGrille[x][y] == 0 || userGrille[x][y] != this.correction[x][y]) return true;
if (userGrille[x][y] == 0 || userGrille[x][y] != this.correction[x][y]) return false;
}
}
return true;
}
private transformFormat(grill: number[][]): number[][] { // convertie le format de la grille pour faciliter l'affichage après.
let newgrid: number[][] = [];
for (let y=0; y<9; y++) {
newgrid.push([]);
for (let x=0; x<9; x++) {
newgrid[y].push(0);
}
}
for (let y=0; y<grill.length; y++) {
for (let x=0; x<grill[y].length; x++) {
let a = this.getXYfromOtherXY(x,y);
newgrid[a[1]][a[0]] =grill[y][x];
}
}
return newgrid;
}
private getXYfromOtherXY(x:number, y:number):number[] {
let a = Math.floor(x/3);
let b = Math.floor(y/3);
let newy:number = b*3+a;
let newx:number = (y%3)*3+(x%3);
return [newx,newy];
}
}
Loading…
Cancel
Save