minesweeper/src/main/js/Difficulty.ts

68 lines
2.1 KiB
TypeScript

/**
* A difficulty setting for a `Field`.
*/
export class Difficulty {
readonly name: string;
readonly width: number;
readonly height: number;
readonly mineCount: number;
readonly solvable: boolean;
/**
* Constructs a new difficulty.
*
* @param name the name of the difficulty
* @param width the width of the field in this difficulty
* @param height the height of the field in this difficulty
* @param mineCount the number of mines in this difficulty
* @param solvable whether the field is guaranteed to be solvable in this difficulty
*/
constructor(name: string, width: number, height: number, mineCount: number, solvable: boolean) {
this.name = name;
this.width = width;
this.height = height;
this.mineCount = mineCount;
this.solvable = solvable;
}
}
/**
* The default difficulty levels.
*/
export const difficulties: Difficulty[] = [
new Difficulty("Beginner (9x9, 10 mines)", 9, 9, 10, true),
new Difficulty("Intermediate (16x16, 40 mines)", 16, 16, 40, true),
new Difficulty("Expert (30x16, 99 mines)", 30, 16, 99, true),
new Difficulty("Custom", 0, 0, 0, false)
];
/**
* The default difficulty level.
*/
export const defaultDifficulty = difficulties[0];
/**
* The custom difficulty level, which is not a real difficulty level.
*/
export const customDifficulty = difficulties[difficulties.length - 1];
/**
* Returns the difficulty in `difficulties` that matches the given parameters.
*
* @param width the width to match a difficulty with
* @param height the height to match a difficulty with
* @param mineCount the number of mines to match a difficulty with
*/
export const findDifficulty = function(width: number, height: number, mineCount: number): Difficulty {
for (let i = 0; i < difficulties.length; i++) {
const difficulty = difficulties[i];
if (difficulty === customDifficulty) continue;
if (width === difficulty.width && height === difficulty.height && mineCount === difficulty.mineCount)
return difficulty;
}
return customDifficulty;
}