1
0
Fork 0
spacetac/src/core/NameGenerator.ts

29 lines
740 B
TypeScript
Raw Normal View History

2019-11-21 22:14:27 +00:00
import { RandomGenerator } from "../common/RandomGenerator";
import { acopy } from "../common/Tools";
2015-03-25 00:00:00 +00:00
2019-11-21 22:14:27 +00:00
// A unique name generator
export class NameGenerator {
// List of available choices
private choices: string[];
2015-03-25 00:00:00 +00:00
2019-11-21 22:14:27 +00:00
// Random generator to use
private random: RandomGenerator;
2015-03-25 00:00:00 +00:00
2019-11-21 22:14:27 +00:00
constructor(choices: string[], random: RandomGenerator = new RandomGenerator()) {
this.choices = acopy(choices);
this.random = random;
}
2015-03-25 00:00:00 +00:00
2019-11-21 22:14:27 +00:00
// Get a new unique name from available choices
getName(): string | null {
if (this.choices.length === 0) {
return null;
2015-03-25 00:00:00 +00:00
}
2019-11-21 22:14:27 +00:00
var index = this.random.randInt(0, this.choices.length - 1);
var result = this.choices[index];
this.choices.splice(index, 1);
return result;
}
2015-03-25 00:00:00 +00:00
}