|
function RandomBag(queueSize) { |
|
|
|
this.available = []; |
|
this.queue = []; |
|
|
|
|
|
while (this.queue.length < queueSize) { |
|
this.queue.push(this.nextAvailable()); |
|
} |
|
} |
|
|
|
RandomBag.initialList = ['i', 'o', 'j', 'l', 'z', 's', 't']; |
|
|
|
|
|
|
|
|
|
|
|
RandomBag.prototype.getQueue = function () { |
|
return this.queue; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
RandomBag.prototype.popQueue = function () { |
|
var res = this.queue.shift(); |
|
this.queue.push(this.nextAvailable()); |
|
return res; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
RandomBag.prototype.nextAvailable = function() { |
|
var index, res; |
|
|
|
|
|
if (this.available.length === 0) { |
|
this.available = RandomBag.initialList.slice(0); |
|
} |
|
|
|
index = Math.floor(Math.random()*this.available.length); |
|
res = this.available.splice(index, 1)[0]; |
|
|
|
return res; |
|
}; |