mirror of
https://github.com/MrEidam/Black-Jack.git
synced 2025-12-29 17:06:11 +00:00
Generation of cards and score
This commit is contained in:
180
main.js
180
main.js
@@ -1,94 +1,146 @@
|
|||||||
|
let playerCards = [];
|
||||||
|
let score = 0;
|
||||||
|
|
||||||
const Deck = {
|
const Deck = {
|
||||||
Clubs:[
|
Clubs:[
|
||||||
'2.png',
|
'2',
|
||||||
'3.png',
|
'3',
|
||||||
'4.png',
|
'4',
|
||||||
'5.png',
|
'5',
|
||||||
'6.png',
|
'6',
|
||||||
'7.png',
|
'7',
|
||||||
'8.png',
|
'8',
|
||||||
'9.png',
|
'9',
|
||||||
'10.png',
|
'10',
|
||||||
'A.png',
|
'A',
|
||||||
'J.png',
|
'J',
|
||||||
'K.png',
|
'K',
|
||||||
'Q.png',
|
'Q',
|
||||||
],
|
],
|
||||||
Diamonds:[
|
Diamonds:[
|
||||||
'2.png',
|
'2',
|
||||||
'3.png',
|
'3',
|
||||||
'4.png',
|
'4',
|
||||||
'5.png',
|
'5',
|
||||||
'6.png',
|
'6',
|
||||||
'7.png',
|
'7',
|
||||||
'8.png',
|
'8',
|
||||||
'9.png',
|
'9',
|
||||||
'10.png',
|
'10',
|
||||||
'A.png',
|
'A',
|
||||||
'J.png',
|
'J',
|
||||||
'K.png',
|
'K',
|
||||||
'Q.png',
|
'Q',
|
||||||
],
|
],
|
||||||
Hearts:[
|
Hearts:[
|
||||||
'2.png',
|
'2',
|
||||||
'3.png',
|
'3',
|
||||||
'4.png',
|
'4',
|
||||||
'5.png',
|
'5',
|
||||||
'6.png',
|
'6',
|
||||||
'7.png',
|
'7',
|
||||||
'8.png',
|
'8',
|
||||||
'9.png',
|
'9',
|
||||||
'10.png',
|
'10',
|
||||||
'A.png',
|
'A',
|
||||||
'J.png',
|
'J',
|
||||||
'K.png',
|
'K',
|
||||||
'Q.png',
|
'Q',
|
||||||
],
|
],
|
||||||
Spades:[
|
Spades:[
|
||||||
'2.png',
|
'2',
|
||||||
'3.png',
|
'3',
|
||||||
'4.png',
|
'4',
|
||||||
'5.png',
|
'5',
|
||||||
'6.png',
|
'6',
|
||||||
'7.png',
|
'7',
|
||||||
'8.png',
|
'8',
|
||||||
'9.png',
|
'9',
|
||||||
'10.png',
|
'10',
|
||||||
'A.png',
|
'A',
|
||||||
'J.png',
|
'J',
|
||||||
'K.png',
|
'K',
|
||||||
'Q.png',
|
'Q',
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const emptyDeck = {
|
||||||
|
Clubs: [],
|
||||||
|
Diamonds: [],
|
||||||
|
Hearts: [],
|
||||||
|
Spades: []
|
||||||
|
}
|
||||||
|
|
||||||
let currentDeck = Deck;
|
let currentDeck = Deck;
|
||||||
|
|
||||||
function cardsleft(){
|
function cardsleft(){
|
||||||
return currentDeck.Clubs.length// + currentDeck.Diamonds.length + currentDeck.Hearts.length + currentDeck.Spades.length;
|
return [
|
||||||
|
currentDeck.Clubs.length,
|
||||||
|
currentDeck.Diamonds.length,
|
||||||
|
currentDeck.Hearts.length,
|
||||||
|
currentDeck.Spades.length
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
function addScore(num){
|
||||||
|
playerCards.push(num);
|
||||||
|
|
||||||
|
score = 0;
|
||||||
|
playerCards.forEach(e => {
|
||||||
|
if(e === 'A'){
|
||||||
|
score += (score < 11) ? 10 : 1;
|
||||||
|
}else{
|
||||||
|
score += isNaN(e) ? 10 : parseInt(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function cardAvail(num){
|
function cardAvail(num){
|
||||||
console.log(num);
|
// Filter available suits that still have cards left
|
||||||
console.warn(currentDeck.Clubs);
|
const availableCards = ['Clubs', 'Diamonds', 'Hearts', 'Spades'].filter((suit, idx) => num[idx] > 0);
|
||||||
|
|
||||||
let card = Math.floor(Math.random() * num);
|
// Check if there are any available cards
|
||||||
|
if(availableCards.length === 0){
|
||||||
document.getElementById('card').src = `./Clubs/${currentDeck.Clubs[card]}`;
|
console.log('No cards available. Deck is empty.');
|
||||||
|
return; // Prevent further execution if no cards are left
|
||||||
const index = currentDeck.Clubs.indexOf(currentDeck.Clubs[card]);
|
|
||||||
if(index > -1){ // only splice array when item is found
|
|
||||||
currentDeck.Clubs.splice(index, 1); // Remove one item at the found index
|
|
||||||
console.log('Card removed');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return card;
|
// Select a random suit from available suits
|
||||||
|
const randomCard = availableCards[Math.floor(Math.random() * availableCards.length)];
|
||||||
|
|
||||||
|
// Ensure that currentDeck[randomCard] exists and has cards
|
||||||
|
if(!currentDeck[randomCard] || currentDeck[randomCard].length === 0){
|
||||||
|
console.log(`Error: ${randomCard} has no cards left.`);
|
||||||
|
return; // Exit if there's an issue with the selected suit
|
||||||
|
}
|
||||||
|
|
||||||
|
const cardIndex = Math.floor(Math.random() * currentDeck[randomCard].length);
|
||||||
|
const card = currentDeck[randomCard][cardIndex];
|
||||||
|
|
||||||
|
addScore(card);
|
||||||
|
|
||||||
|
document.getElementById('card').src = `./${randomCard}/${card}.png`;
|
||||||
|
|
||||||
|
currentDeck[randomCard].splice(cardIndex, 1);
|
||||||
|
|
||||||
|
console.log(`Card removed from ${randomCard}: ${card}`);
|
||||||
|
console.log(currentDeck);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDeckEmpty() {
|
||||||
|
return ['Clubs', 'Diamonds', 'Hearts', 'Spades'].every(suit => currentDeck[suit].length === 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addCard(){
|
function addCard(){
|
||||||
|
if (isDeckEmpty()) {
|
||||||
|
currentDeck = {...Deck}; // Reset to the original deck if empty
|
||||||
|
}
|
||||||
cardAvail(cardsleft());
|
cardAvail(cardsleft());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function hit(){
|
function hit(){
|
||||||
addCard();
|
addCard();
|
||||||
|
document.querySelector('h2').innerHTML = score;
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
font-family: 'Ubuntu', sans-serif;
|
font-family: 'Ubuntu', sans-serif;
|
||||||
}
|
}
|
||||||
body{
|
body{
|
||||||
background-color: #333;
|
background-color: #262a2b;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
|
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
@@ -34,4 +34,6 @@ button{
|
|||||||
height: 30px;
|
height: 30px;
|
||||||
width: 60px;
|
width: 60px;
|
||||||
margin: .5rem;
|
margin: .5rem;
|
||||||
|
color: #fff;
|
||||||
|
background: #6b6b6b;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user