Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

Javascript dealing cards

im trying to figure out how can i make my deck deal 6 cards to each player.

I manage to do it with splice method, and i have now my deck and 2 players getting 6 cards in player1Hand and player2Hand.

Right know im getting stucked with how to display vissualy all 6 cards, im stucked with displaying only first card from array. here is the code, and i would be thankfull if someone can help out.

 import Deck from './deck.js'

const CARD_VALUE_MAP = {

    "2" : 2,
    "3" : 3,
    "4" : 4,
    "5" : 5,
    "6" : 6,
    "7" : 7,
    "8" : 8,
    "9" : 9,
    "10" : 10,
    "J" : 11,
    "Q" : 12,
    "K" : 13,
    "A" : 14
}

let player1Hand = [];
let player2Hand = [];


// Selectors
const player1deck = document.querySelector('.player1Hand');
const player2deck = document.querySelector('.player2Hand');
const fulldeck = document.querySelector('.fulldeck');
let deck;




    gameStart();
     function gameStart() {

    deck = new Deck();
    deck.shuffle();
    console.log(deck.cards)

    player1Hand = deck.cards.splice(0,6);
    player2Hand = deck.cards.splice(0,6);
    console.log(player1Hand, player2Hand, deck.cards)

    const i = 2;

        player1Hand.forEach(card => {
            player1deck.appendChild(player1Hand[0].getHTML())
        });
    
        player2Hand.forEach(card => {
            player2deck.appendChild(player2Hand[0].getHTML())
        })

   
}

fulldeck.innerText = deck.cards.length;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...