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

28 lines
800 B
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac {
2015-03-25 00:00:00 +00:00
// A unique name generator
export class NameGenerator {
// List of available choices
private choices: string[];
// Random generator to use
private random: RandomGenerator;
constructor(choices: string[], random: RandomGenerator = new RandomGenerator()) {
2017-07-06 22:55:29 +00:00
this.choices = acopy(choices);
2015-03-25 00:00:00 +00:00
this.random = random;
}
// Get a new unique name from available choices
2017-03-09 17:11:00 +00:00
getName(): string | null {
2015-03-25 00:00:00 +00:00
if (this.choices.length === 0) {
return null;
}
2017-02-26 17:44:15 +00:00
var index = this.random.randInt(0, this.choices.length - 1);
2015-03-25 00:00:00 +00:00
var result = this.choices[index];
this.choices.splice(index, 1);
return result;
}
}
}