ludo
ludovic.castglia 8 months ago
parent e0c256d835
commit 5404192301

@ -65,4 +65,8 @@
.faux {
background-color: var(--color-faux);
}
input:disabled {
color: #000;
}

@ -3,16 +3,22 @@
<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="{{cell}}" class="cell" max="9" min="0" id="xy{{x}}{{y}}" disabled>
<input *ngIf="cell == 0" type="number" value="" class="cell" max="9" min="0" id="xy{{x}}{{y}}">
</div>
</div>
</div>
<button (click)="publish()">
valider la grille
soumettre
</button>
<button (click)="corriger()">
corriger la grille
verrifier
</button>
<p>nombre d'indice: {{nbIndice}}</p>
<button (click)="a()">
verrifier
</button>

@ -19,22 +19,12 @@ export class SudokuComponent {
constructor(protected sudokuService: SudokuService, private activatedRoute: ActivatedRoute) {
}
public a() {
console.log(this.sudokuService.getEasy());
}
ngOnInit() {
const difficulty = this.activatedRoute.snapshot.params["difficulty"];
switch (difficulty) {
case 'easy':
this.grille = this.sudokuService.getEasy();
break;
case 'medium':
this.grille = this.sudokuService.getMedium();
break;
case 'hard':
this.grille = this.sudokuService.getHard();
break;
default:
this.grille = this.sudokuService.getEasy();
break;
}
this.Request(difficulty);
}
public publish() {
@ -45,6 +35,7 @@ export class SudokuComponent {
alert("c'est correct mon ptit");
return;
}
this.nbIndice += 1;
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++) {
@ -78,4 +69,24 @@ export class SudokuComponent {
errors[i].classList.remove("faux");
}
}
private Request(difficulty: string) {
switch (difficulty) {
case 'easy':
this.grille = this.sudokuService.getEasy();
break;
case 'medium':
this.grille = this.sudokuService.getMedium();
break;
case 'hard':
this.grille = this.sudokuService.getHard();
break;
default:
this.grille = this.sudokuService.getEasy();
break;
}
if (this.grille.length == 0) {
setTimeout(()=> {this.Request(difficulty);console.log("a")},100);
} // lancer un await ici si possible
}
}

@ -1,13 +1,20 @@
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from "@angular/core";
interface apiResponseInterface {
data: number[][],
easy: number[][],
medium: number[][],
hard: number[][]
}
@Injectable()
export class SudokuService {
private correction: number[][];
private easy: number[][];
private medium: number[][];
private hard: number[][];
private readonly userApiUrl = 'https://sudoku-game-and-api.netlify.app/api/sudoku';
private readonly userApiUrl = 'https://664ba07f35bbda10987d9f99.mockapi.io/api/sudoku/1';
public constructor(private http: HttpClient){
this.correction = [];
@ -15,18 +22,12 @@ export class SudokuService {
this.medium = [];
this.hard = [];
this.http.get<string>(this.userApiUrl).subscribe(apiResponse => {
let jsonResponse = JSON.parse(apiResponse);
this.correction = jsonResponse["data"];
this.easy = jsonResponse["easy"];
this.medium = jsonResponse["medium"];
this.hard = jsonResponse["hard"];
this.http.get<apiResponseInterface>(this.userApiUrl).subscribe(apiResponse => {
this.correction = this.transformFormat(apiResponse["data"]);
this.easy = this.transformFormat(apiResponse["easy"]);
this.medium = this.transformFormat(apiResponse["medium"]);
this.hard = this.transformFormat(apiResponse["hard"]);
});
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[][]{
@ -63,6 +64,7 @@ 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 false;
@ -71,7 +73,8 @@ export class SudokuService {
return true;
}
private transformFormat(grill: number[][]): number[][] { // convertie le format de la grille pour faciliter l'affichage après.
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([]);
@ -89,6 +92,7 @@ export class SudokuService {
}
private getXYfromOtherXY(x:number, y:number):number[] {
// transforme les coordonées d'un point du systhème de représentation de l'API au systhème de représentation interne à l'application
let a = Math.floor(x/3);
let b = Math.floor(y/3);
let newy:number = b*3+a;

Loading…
Cancel
Save