|
|
|
@ -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;
|
|
|
|
|