Generation of cards and score

This commit is contained in:
2024-10-02 09:40:23 +02:00
parent 974a3d49b5
commit 1381706440
2 changed files with 119 additions and 65 deletions

180
main.js
View File

@@ -1,94 +1,146 @@
let playerCards = [];
let score = 0;
const Deck = {
Clubs:[
'2.png',
'3.png',
'4.png',
'5.png',
'6.png',
'7.png',
'8.png',
'9.png',
'10.png',
'A.png',
'J.png',
'K.png',
'Q.png',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'A',
'J',
'K',
'Q',
],
Diamonds:[
'2.png',
'3.png',
'4.png',
'5.png',
'6.png',
'7.png',
'8.png',
'9.png',
'10.png',
'A.png',
'J.png',
'K.png',
'Q.png',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'A',
'J',
'K',
'Q',
],
Hearts:[
'2.png',
'3.png',
'4.png',
'5.png',
'6.png',
'7.png',
'8.png',
'9.png',
'10.png',
'A.png',
'J.png',
'K.png',
'Q.png',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'A',
'J',
'K',
'Q',
],
Spades:[
'2.png',
'3.png',
'4.png',
'5.png',
'6.png',
'7.png',
'8.png',
'9.png',
'10.png',
'A.png',
'J.png',
'K.png',
'Q.png',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'A',
'J',
'K',
'Q',
]
}
const emptyDeck = {
Clubs: [],
Diamonds: [],
Hearts: [],
Spades: []
}
let currentDeck = Deck;
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){
console.log(num);
console.warn(currentDeck.Clubs);
// Filter available suits that still have cards left
const availableCards = ['Clubs', 'Diamonds', 'Hearts', 'Spades'].filter((suit, idx) => num[idx] > 0);
let card = Math.floor(Math.random() * num);
document.getElementById('card').src = `./Clubs/${currentDeck.Clubs[card]}`;
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');
// Check if there are any available cards
if(availableCards.length === 0){
console.log('No cards available. Deck is empty.');
return; // Prevent further execution if no cards are left
}
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(){
if (isDeckEmpty()) {
currentDeck = {...Deck}; // Reset to the original deck if empty
}
cardAvail(cardsleft());
}
function hit(){
addCard();
document.querySelector('h2').innerHTML = score;
}

View File

@@ -6,7 +6,7 @@
font-family: 'Ubuntu', sans-serif;
}
body{
background-color: #333;
background-color: #262a2b;
color: #fff;
min-height: 100vh;
@@ -34,4 +34,6 @@ button{
height: 30px;
width: 60px;
margin: .5rem;
color: #fff;
background: #6b6b6b;
}