Added typedef for SudokuStates

Removed unnecessary parameter
This commit is contained in:
BordedDev 2025-01-17 00:50:36 +01:00
parent 367e12fe7b
commit cd18662fed

View File

@ -21,20 +21,24 @@ function randInt(min, max) {
} }
class SudokuPuzzle extends EventTarget { class SudokuPuzzle extends EventTarget {
/**
* @typedef {(number|null|undefined)[]} SudokuState
*/
/** /**
* *
* @type {(number|null)[][]} * @type {SudokuState[]}
*/ */
#state = [] #state = []
/** /**
* *
* @type {(number|null|undefined)[]} * @type {SudokuState}
*/ */
#activeState = new Array(SUDOKU_GRID_SIZE) #activeState = new Array(SUDOKU_GRID_SIZE)
/** /**
* *
* @returns {(number|null|undefined)[]} * @returns {SudokuState}
*/ */
get grid() { get grid() {
const gridValue = [...this.#activeState] const gridValue = [...this.#activeState]
@ -61,7 +65,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param serializedState {string} * @param serializedState {string}
* @returns {((number|null|undefined))[]} * @returns {SudokuState}
*/ */
static #stateFromString(serializedState) { static #stateFromString(serializedState) {
let cellIndex = 0 let cellIndex = 0
@ -86,7 +90,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param stateToSample {(number|null|undefined)[]} * @param stateToSample {SudokuState}
* @returns {undefined|number} * @returns {undefined|number}
*/ */
#sampleRandomEmptyField(stateToSample = this.#activeState) { #sampleRandomEmptyField(stateToSample = this.#activeState) {
@ -98,7 +102,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param states {...(number|null|undefined)[][]} * @param states {...SudokuState[]}
*/ */
static collapseStates(...states) { static collapseStates(...states) {
return Array.from({ length: SUDOKU_GRID_SIZE }, (_, i) => { return Array.from({ length: SUDOKU_GRID_SIZE }, (_, i) => {
@ -113,7 +117,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param slot {number} * @param slot {number}
* @param state {(number|null|undefined)[]} * @param state {SudokuState}
* @return {Set<number>} * @return {Set<number>}
*/ */
#boxGetValues(slot, state = this.#activeState) { #boxGetValues(slot, state = this.#activeState) {
@ -137,7 +141,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param slot {number} * @param slot {number}
* @param state {(number|null|undefined)[]} * @param state {SudokuState}
* @return {Set<number>} * @return {Set<number>}
*/ */
#crossGetValues(slot, state = this.#activeState) { #crossGetValues(slot, state = this.#activeState) {
@ -176,7 +180,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param slot {number} * @param slot {number}
* @param state {(number|null|undefined)[]} * @param state {SudokuState}
* @return {Set<number>} * @return {Set<number>}
*/ */
getValues(slot, state = this.#activeState) { getValues(slot, state = this.#activeState) {
@ -189,7 +193,7 @@ class SudokuPuzzle extends EventTarget {
* *
* @param value {number} * @param value {number}
* @param slot {number} * @param slot {number}
* @param state {(number|null|undefined)[]} * @param state {SudokuState}
* @return boolean * @return boolean
*/ */
isValueValidForSlot(value, slot, state = this.#activeState) { isValueValidForSlot(value, slot, state = this.#activeState) {
@ -200,7 +204,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param newState {(number|null|undefined)[]} * @param newState {SudokuState}
*/ */
fillRandomFieldInState(newState = Array.from({ length: SUDOKU_GRID_SIZE })) { fillRandomFieldInState(newState = Array.from({ length: SUDOKU_GRID_SIZE })) {
const inProgressState = SudokuPuzzle.collapseStates( const inProgressState = SudokuPuzzle.collapseStates(
@ -222,7 +226,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @returns {(number|null|undefined)[]} * @returns {SudokuState}
*/ */
#generateRandomState() { #generateRandomState() {
const newState = Array.from({ length: SUDOKU_GRID_SIZE }) const newState = Array.from({ length: SUDOKU_GRID_SIZE })
@ -235,7 +239,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @returns {(number|null|undefined)[]} * @returns {SudokuState}
*/ */
get baseState() { get baseState() {
return this.#state[0] return this.#state[0]
@ -253,7 +257,7 @@ class SudokuPuzzle extends EventTarget {
/** /**
* *
* @param newState {(number|null|undefined)[]} * @param newState {SudokuState}
*/ */
applyState(newState) { applyState(newState) {
this.#state.push(newState) this.#state.push(newState)
@ -367,7 +371,7 @@ class SudokuHost extends HTMLElement {
}) })
this.#keyProcessors.set("m", () => { this.#keyProcessors.set("m", () => {
for (const [_, cell] of this.#selectedCells) { for (const [, cell] of this.#selectedCells) {
cell.classList.toggle("marked") cell.classList.toggle("marked")
cell.classList.remove("selected") cell.classList.remove("selected")
} }