-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.js
79 lines (69 loc) · 1.68 KB
/
blackjack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function Card(suit, val) {
this.suit = suit;
this.val = val;
}
Card.prototype.toString = function() {
return this.val + this.suit;
}
Card.prototype.plus = function(other) {
return this.val + other.val;
}
function sumCards() {
var result = 0;
for (var i = 0; i < arguments.length; i++) {
result += arguments[i].val;
}
return result;
}
// Generate a single, standard deck
var deck = function() {
var suits = '♠ ♥ ♦ ♣'.split(' ');
var values = 'A 2 3 4 5 6 7 8 9 10 J Q K'.split(' ');
var result = [];
suits.forEach(function(suitElem) {
values.forEach(function(valElem) {
result.push(new Card(suitElem, valElem));
});
});
return result;
}();
function randInt(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
// Shuffle elements of an arbitrary list
function shuffle(arr) {
function swap(arr, a, b) {
var temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
for (var i = 0; i < arr.length; i++) {
var randomIndex = randInt(0, arr.length);
swap(arr, i, randomIndex);
}
}
// Get a random element from an array
function getCard(arr) {
arr = arr || deck;
return arr[randInt(0, deck.length)];
}
function hit() {
alert('hi');
}
// shuffle(deck);
// function consoleGame() {
// var readline = require('readline');
//
// var rl = readline.createInterface({
// input: process.stdin,
// output: process.stdout
// });
//
// function newTurn() {
// var playerCards = [getCard(deck), getCard(deck)];
// }
//
// rl.question("?> ", function(response) {
// rl.close();
// });
// }