-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWar-Card-game-deck.js
211 lines (163 loc) · 6.09 KB
/
War-Card-game-deck.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//Code for deck/all cards goes- all caps is universal
// 1. Define the Card class:
// - Properties:
// - `suit`: The suit of the card (e.g., "♠", "♣", "♥", "♦").
// - `value`: The value of the card (e.g., "A", "2", ..., "K").
// - Methods:
// - No additional methods are required for this class.
// 2. Define the Deck class:
// - Properties:
// - `cards`: An array to store the cards in the deck.
// - Methods:
// - `initialize()`: Creates a standard deck of 52 cards with all combinations of suits and values.
// - `shuffle()`: Shuffles the cards in the deck randomly.
// - `dealCard()`: Removes and returns the top card from the deck.
const SUITS = ["♠", "♣", "♥", "♦"]
const VALUES = ["A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K"]
//const deck = new Array(SUITES, VALUES);
function freshDeck() { //This is to create for us a brand new deck of cards w/52 cards one for each suit/value combo
return SUITS.flatMap(suits => { //loop through all the suits and values all in one array, what map function is for
return VALUES.map(value =>{
return new Card(suit, values)
})//This will do is loop through all values and map them into an array condince into one array
})
}
// deck = new Array();
// for (let i = 0; i < SUITS.length; i++) {
// for (let x = 0; x < VALUES.length; x++) {
// //const element = [arr];
// var card = {
// Value: cards [x],
// SUITS: suits[i]
// deck.push(card)
// return deck;
// 2. Define the Deck class:
// - Properties:
// - `cards`: An array to store the cards in the deck.
// - Methods:
// - `initialize()`: Creates a standard deck of 52 cards with all combinations of suits and values.
// - `shuffle()`: Shuffles the cards in the deck randomly.
// - `dealCard()`: Removes and returns the top card from the deck.
class Card{ //do I need this??
constructor(SUIT, VALUE) {
this.SUIT = SUIT;
this.VALUE = VALUE;
}
}
// 4. Create an instance of the Deck class and initialize it.
export default class Deck {
constructor(cards = freshDeck()){
this.cards = cards;
};
//or do I do this code??
///function deck(cards) {
// if (!(this.instanceof deck)) return new deck(cards); The purpose of this code seems to be checking if the Deck object has already been instantiated
// and returning a new instance if it hasn't. However,
// in the context of the Deck class constructor, this check is not needed.
// this.cards = cards;
//
//cards instanceof cards //=> true
// var _player1 = new Person('Player1');
// var _player2 = Person('Player2');
// buildDeck() { I can use this as an example to createDeck????
// this.populate();
// this.shuffle();
// return this.cards
// }
createDeck() {
for (let suit of SUITS) {
for(let value of VALUES) {
const card = new Card(suit, value);
this.cards.push(card);
}
}
}
// populate() { Do I need this, when I have array of suits and values
// const suits = ['Clubs','Diamonds','Hearts', 'Spades'];
// const ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A'];
// const values = [2,3,4,5,6,7,8,9,10,11,12,13,14];
// for (let i = 0; i < suits.length; i++) {
// for (let a = 0; a < ranks.length; a++) {
// this.cards.push(new Card(ranks[a],suits[i],values[a]));
// }
// }
// }
cards = new Card[DECK_SIZE];
// String suits[] = {}"♠", "♣", "♥", "♦"];
// String cards[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Queen", "King"};
// int cardIndex = 0;
// for (String suit : suits) {
// for (String card : cards) {
// cards[cardIndex] = new Card(suit, card);
// cardIndex++;
// }
// }
shuffle(){ //Need to get the } to the end of the class
//for (let i =this.cards.length -1; i >0; i--){ //He uses this.numberOfCards instead of cards
//const j = Math.floor(Math.random() * (i + 1)); //const newIndex = Math.floor(Math.random() * (i + 1))
//const oldValue = this.cards[newIndex]
//this.cards[newIndex] = ths,cards[i]
//this.cards[i] = oldValue
//[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
// function shuffleArray(array) {
// for (let i = array.length - 1; i > 0; i--) {
// const j = Math.floor(Math.random() * (i + 1));
// [array[i], array[j]] = [array[j], array[i]];
// }
// }
}
dealCard() {
return this.cards.pop();
alert(dealCards);
}
// function shuffle(SUITS){ //I may not need this function. Do I need this??
// for (let i = SUITS.length -1; 1 >0; i--) {
// let j = Math.floor(Math.random() * (i + 1));
// [SUITS[j], SUITS[i]] = [SUITS[j], SUITES[i]];
// //let SUITS = ["♠", "♣", "♥", "♦"];
// //SUITS.sort(() => Math.random() -0.5);
// }
}
shuffle(SUITS);
count[SUITS.join(``)]++;
const deck = new Deck();
deck.createDeck();
deck.shuffle();
SUITS = { //counts of appearance for all possible permutations
"♣": 0,
"♠": 0,
"♦": 0,
"♥": 0,
};
for (let i = 0; i < 1000000; i++) {
let SUITS = ["♠", "♣", "♥", "♦"];
shuffle(SUITS);
count[SUITS.join(``)]++;
}
shuffle(SUITS);
alert(SUITS);
//["♠", "♣", "♥", "♦"];
//show cunts of all possible permutation
for (let key in count) {
alert(`${key}: ${count[key]}`);
}
//function shuffleCard(params) {
//}
const id = setInterval(shuffle, 12)
let shuffleCount = 0
for (let i= 0; i < Card.length; index++) { const element = Card[i] + "<br>";
} //end curly from the top code
//function dealt(params) { // put each card in players array
//Put something here to do a push