Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var sha256 = require('js-sha256').sha256;
- var Deck = function(get_seed) {
- var used_cards = {};
- var n_used_cards = 0;
- var get_bits = function(seed) {
- var arr = sha256.array(seed);
- var ret = [];
- for (const i of arr) {
- var k = 1;
- for (var p = 0; p < 8; ++p) {
- ret.push(i & k);
- k *= 2;
- }
- }
- return ret;
- };
- var cur_sha = get_bits(get_seed());
- var cur_idx = 0;
- var get_bit = function() {
- if (cur_idx === 256) {
- this.cur_sha = get_bits(get_seed());
- this.cur_idx = 0;
- }
- return this.cur_sha[this.cur_idx];
- }
- this.get_card = function() {
- var rem = 52 - n_used_cards;
- var cur = get_bit();
- var pow = 2;
- while (pow < rem) {
- cur += get_bit() * pow;
- pow *= 2;
- }
- if (cur >= rem) {
- return this.get_card();
- }
- var idx = 0;
- for (var i = 0; i < 52; ++i) {
- while (idx in used_cards) {
- ++idx;
- }
- ++idx;
- }
- while (idx in used_cards) {
- ++idx;
- }
- used_cards[idx] = true;
- ++n_used_cards;
- return to_card(idx);
- }
- var to_card = function(idx) {
- var num = idx % 13;
- var suit = Math.floor(idx / 4);
- return {
- num: num,
- suit: ['diamonds', 'clubs', 'hearts', 'spades'][suit]
- };
- }
- };
- module.exports = Deck;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement