-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardGame.js
160 lines (147 loc) · 3.97 KB
/
CardGame.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
$(document).ready(function(){
var newDeck = new Deck();
var Dealer = new Player('Dealer');
var Player1 = new Player('Player1');
$('#start').click(function() {
var newDeck = new Deck();
newDeck.shuffle();
Dealer.hand.push(newDeck.deal());
Dealer.hand.push(newDeck.deal());
Player1.hand.push(newDeck.deal());
Player1.hand.push(newDeck.deal());
var dealer = Dealer.winLoss();
console.log(dealer);
var player = Player1.winLoss();
console.log(player);
if (!player){
$('#player').append('<h1> YOU LOSE :( </h1>');
}
else if (!dealer){
$('#player').append('<h1> YOU WIN!!!!!!!!!!!!!!!!! </h1>');
$('#back').remove();
$('#dealer').append('<img src="./images/' + Dealer.hand[1].name() + '.png" alt="img">');
}
else if (player === true) {
$('#player').append('<h1> YOU WIN!!!!!!!!!!!!!!!!! </h1>');
}
for (var i = 0; i < 2; i ++){
$('#player').append('<img src="./images/' + Player1.hand[i].name() + '.png" alt="img">');
}
$('#dealer').append('<img src="./images/' + Dealer.hand[0].name() + '.png" alt="img">');
$('#dealer').append('<img src="./images/b1fv.png" alt="img" id="back">');
});
$('#hit').click(function() {
var card = newDeck.deal();
$('#player').append('<img src="./images/' + card.name() + '.png" alt="img">');
Player1.hand.push(card);
var player = Player1.winLoss();
console.log(player);
if (player === true) {
$('#player').append('<h1> YOU WIN!!!!!!!!!!!!!!!! </h1>');
}
else if (!player){
$('#player').append('<h1> YOU LOSE :( </h1>');
}
});
$('#stay').click(function() {
while (Dealer.sum < 18){
var card = newDeck.deal();
$('#dealer').append('<img src="./images/' + card.name() + '.png" alt="img">');
Dealer.hand.push(card);
var dealer = Dealer.winLoss();
if (dealer === true){
$('#player').append('<h1> YOU LOSE :( </h1>');
return;
}
else if (!dealer) {
$('#player').append('<h1> YOU WIN!!!!!!!!!!!!!!!!! </h1>');
return;
}
console.log(card.name());
}
var outcome = Player1.sumCompare(Dealer);
if (!outcome){
$('#player').append('<h1> YOU LOSE :( </h1>');
}
else
{
$('#player').append('<h1> YOU WIN!!!!!!!!!!!!!!!!! </h1>');
}
});
});
function Card(suit, value){
this.suit = suit;
this.value = value;
this.name = function(){
var suits = ['s', 'h', 'c', 'd'];
return suits[this.suit] + this.value;
};
};
function Deck() {
this.newDeck = function(){
cards = [];
for (var i = 0; i < 4; i++){
for (var j = 2; j < 10; j++){
cards.push(new Card(i, j));
}
}
cards.push(new Card(1, 1));
cards.push(new Card (0, 10));
};
var cards = [];
this.newDeck();
this.shuffle = function(){
for (var i = 0; i < cards.length; i++){
var randomNum = Math.floor(Math.random() * (cards.length));
var temp = cards[i];
cards[i] = cards[randomNum];
cards[randomNum] = temp;
}
};
this.deal = function() {
return cards.pop();
};
}
function Player(name){
this.name = name;
this.hand = [];
this.sum = 0;
this.deckDeal = function(deck){
this.hand.push(deck.deal());
};
this.discard = function(cardIndex){
this.hand.splice(cardIndex, 1);
};
this.winLoss = function(){
var newSum = 0;
var ten = false;
var ace = false;
for (var i = 0; i < this.hand.length; i++){
console.log(this.hand[i].value);
newSum += this.hand[i].value;
if (this.hand[i].value === 10){
ten = true;
}
else if (this.hand[i].value === 1){
ace = true;
}
}
this.sum = newSum;
if (newSum === 10 || newSum > 21 || ten){
return false;
}
else if (newSum === 21 || ace){
return true;
}
return newSum;
};
this.sumCompare = function(Dealer){
if (Dealer.sum > this.sum){
return false;
}
else {
return true;
}
};
}