How to Split 52 card Into 26 card in javascript

Issue

I Creating A Card game in JavaScript for 2 players. Facing some issue that i unable to split the cards for 2 players, right now all the 52 cards displaying for full page. I want to split 52 cards into 26 cards like playing 2 Players. Need to spilt the 52 card for 2 players. I tried all the way but not working for me. I hereby attached HTML, CSS and JS.

Kindly help regarding the same.

var cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var suits = ["diamonds", "hearts", "spades", "clubs"];
var deck = new Array();

function getDeck()
{
    var deck = new Array();
    
    for(var i = 0; i < suits.length; i++)
    {
        for(var x = 0; x < cards.length; x++)
        {
            var card = {Value: cards[x], Suit: suits[i]};
            deck.push(card);
        }
    }
    return deck;
}

function shuffle()
{
    // for 1000 turns
    // switch the values of two random cards
    for (var i = 0; i < 1000; i++)
    {
        var location1 = Math.floor((Math.random() * deck.length));
        var location2 = Math.floor((Math.random() * deck.length));
        var tmp = deck[location1];

        deck[location1] = deck[location2];
        deck[location2] = tmp;
    }

    renderDeck();
}

function renderDeck()
{
    document.getElementById('deck').innerHTML = '';
    for(var i = 0; i < deck.length; i++)
    {
        var card = document.createElement("div");
        var value = document.createElement("div");
        var suit = document.createElement("div");
        card.className = "card";
        value.className = "value";
        suit.className = "suit " + deck[i].Suit;

        value.innerHTML = deck[i].Value;
        card.appendChild(value);
        card.appendChild(suit);

        document.getElementById("deck").appendChild(card);
    }
}

function load()
{
    deck = getDeck();
    shuffle();
    renderDeck();
}

window.onload = load;
function split()
{
const list = cards()
const half = Math.ceil(list.length / 2);    
const firstHalf = list.slice(0, half)
const secondHalf = list.slice(-half)
}
.deck
{
    width:50%;
    margin: 20px auto;
    background:dodgerblue;
    border:solid 10px black;
    padding: 10px;
    border-radius:10px;
    text-align: center;
    color:white;
}

.card
{
    padding: 10px;
    border: solid 1px #808080;
    background-color: white;
    display: inline-block;
    border-radius: 10px;
    font-size: 14pt;
    text-align: center;
    color:black;
    margin: 3px;
}

.btn
{
    background:white;
    padding: 10px 20px;
    border-radius:30px;
    margin: 10px;
    display:inline-block;
}

.card .value{
    font-size:15pt;
    font-family: sans-serif;
}

.card .suit
{
    background-image: url('card.png');
    height: 100px;
    width: 90px;
}

.card .diamonds
{
    background-position-y: 100px;
}

.card .hearts
{
    background-position-x: 90px;
}

.card .clubs
{
    background-position-x:90px;
    background-position-y:100px;
}
<html>
<head>
    <link rel="stylesheet" href="main.css">
    <script type="text/javascript" src="main.js"></script>
   

</head>

<body>
    <div class="deck">
        <h1>A Deck of Cards</h1>

        <a href="javascript:void(0)" class="btn" onclick="shuffle()">Shuffle</a>
        <div id="deck"></div>
    </div>
</body>
</html>

Solution

It gives each card in turn to 2 players :

const cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
const suits = ["diamonds", "hearts", "spades", "clubs"]
let player1 = player2 = []
const deckOrigin = cards.reduce((res, card) => [...res, ...suits.map(suit => ({Value: card, Suit: suit}))], [])
let deck = [...deckOrigin]
const deckplayer1 = document.getElementById('player1')
const deckplayer2 = document.getElementById('player2')
const shuffle = () => {
  deck = [...deckOrigin]
  countCards()
  player1 = player2 = []
  let currentIndex = deck.length,  randomIndex
  while (currentIndex != 0) {
    randomIndex = Math.floor(Math.random() * currentIndex)
    currentIndex--
    [deck[currentIndex], deck[randomIndex]] = [deck[randomIndex], deck[currentIndex]]
  }
  deckplayer1.innerHTML = ''
  deckplayer2.innerHTML = ''
}

const countCards = () => {
  document.getElementById('countCard').innerHTML = deck.length
}

const renderDeck = (div, ele) => {
    let card = document.createElement("div")
    let value = document.createElement("div")
    let suit = document.createElement("div")
    card.className = "card"
    value.className = "value"
    suit.className = "suit " + ele.Suit

    value.innerHTML = ele.Value
    card.appendChild(value)
    card.appendChild(suit)

    div.appendChild(card)
}
countCards()
const split = () => {
  if (deck.length%2) {
    renderDeck(deckplayer2, deck.pop())
  } else {
    renderDeck(deckplayer1, deck.pop())
  }
  countCards()
}
.deck
{
    width:50%;
    margin: 20px auto;
    background:dodgerblue;
    border:solid 10px black;
    padding: 10px;
    border-radius:10px;
    text-align: center;
    color:white;
}

.player {
  width: 50%;
  display: table-cell;
}

.card
{
    padding: 10px;
    border: solid 1px #808080;
    background-color: white;
    display: inline-block;
    border-radius: 10px;
    font-size: 14pt;
    text-align: center;
    color:black;
    margin: 3px;
}

.btn
{
    background:white;
    padding: 10px 20px;
    border-radius:30px;
    margin: 10px;
    display:inline-block;
}

.card .value{
    font-size:15pt;
    font-family: sans-serif;
}

.player-name {
    width: calc(50% - 30px);
    padding: 10px;
    margin: 3px;
    display: inline-block;
}

.card .suit
{
    background-position-x:90px;
    background-position-y:100px;
    background-color: green;
    opacity: 0.2;
    background-image: url('card.png');
    height: 100px;
    width: 90px;
}

.card .diamonds
{
    background-position-x:90px;
    background-position-y:100px;
    background-color: green;
    opacity: 0.2;
    background-image: url('card.png');
    height: 100px;
    width: 90px;
}

.card .hearts
{
    background-color: red;
    opacity: 0.2;
    background-position-x:90px;
    background-position-y:100px;
    background-image: url('card.png');
    height: 100px;
    width: 90px;
}

.card .clubs
{
    background-color: yellow;
    opacity: 0.2;
    background-position-x:90px;
    background-position-y:100px;
    background-image: url('card.png');
    height: 100px;
    width: 90px;
}
<html>
<head>
    <link rel="stylesheet" href="main.css">
    <script type="text/javascript" src="main.js"></script>
   

</head>

<body>
    <div class="deck">
        <h1>A Deck of Cards</h1>

        <a href="javascript:void(0)" class="btn" onclick="shuffle()">Shuffle</a>
        <a href="javascript:void(0)" class="btn" onclick="split()">split</a>
        <div id="countCard"></div>
        <div class="player-name">Player1</div>
        <div class="player-name">Player2</div>
        <div class="player">
          <div id="player1"></div>
        </div>
        <div class="player">
          <div id="player2"></div>
        </div>
    </div>
</body>
</html>

You can try it :

const cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
const suits = ["diamonds", "hearts", "spades", "clubs"]
let player1 = player2 = []
let deck = cards.reduce((res, card) => [...res, ...suits.map(suit => ({Value: card, Suit: suit}))], [])
const deckEle = document.getElementById('deck')
const deckplayer1 = document.getElementById('player1')
const deckplayer2 = document.getElementById('player2')
const shuffle = () => {
  let currentIndex = deck.length,  randomIndex
  while (currentIndex != 0) {
    randomIndex = Math.floor(Math.random() * currentIndex)
    currentIndex--
    [deck[currentIndex], deck[randomIndex]] = [deck[randomIndex], deck[currentIndex]]
  }
  deckplayer1.innerHTML = ''
  deckplayer2.innerHTML = ''
  renderDeck(deckEle, deck)
}

const renderDeck = (div, arr) => {
    div.innerHTML = ''
    for(let i = 0; i < arr.length; i++)
    {
        let card = document.createElement("div")
        let value = document.createElement("div")
        let suit = document.createElement("div")
        card.className = "card"
        value.className = "value"
        suit.className = "suit " + arr[i].Suit

        value.innerHTML = arr[i].Value
        card.appendChild(value)
        card.appendChild(suit)

        div.appendChild(card)
    }
}
renderDeck(deckEle, deck)
const split = () => {  
  deckEle.innerHTML = ''
  player1 = deck.slice(0, deck.length/2)
  renderDeck(deckplayer1, player1)
  player2 = deck.slice(-deck.length/2)
  renderDeck(deckplayer2, player2)
}
.deck
{
    width:50%;
    margin: 20px auto;
    background:dodgerblue;
    border:solid 10px black;
    padding: 10px;
    border-radius:10px;
    text-align: center;
    color:white;
}

.player {
  width : 50%;
  display: table-cell;
}

.card
{
    padding: 10px;
    border: solid 1px #808080;
    background-color: white;
    display: inline-block;
    border-radius: 10px;
    font-size: 14pt;
    text-align: center;
    color:black;
    margin: 3px;
}

.btn
{
    background:white;
    padding: 10px 20px;
    border-radius:30px;
    margin: 10px;
    display:inline-block;
}

.card .value{
    font-size:15pt;
    font-family: sans-serif;
}

.card .suit
{
    background-image: url('card.png');
    height: 100px;
    width: 90px;
}

.card .diamonds
{
    background-position-y: 100px;
    background-color: green;
    opacity: 0.2;
}

.card .hearts
{
    background-color: red;
    opacity: 0.2;
    background-position-x: 90px;
}

.card .clubs
{
    background-color: yellow;
    opacity: 0.2;
    background-position-x:90px;
    background-position-y:100px;
}
<html>
<head>
    <link rel="stylesheet" href="main.css">
    <script type="text/javascript" src="main.js"></script>
   

</head>

<body>
    <div class="deck">
        <h1>A Deck of Cards</h1>

        <a href="javascript:void(0)" class="btn" onclick="shuffle()">Shuffle</a>
        <a href="javascript:void(0)" class="btn" onclick="split()">split</a>
        <div id="deck"></div>
        <div class="player">
          <p>Player1</p>
          <div id="player1"></div>
        </div>
        <div class="player">
          <p>Player2</p>
          <div id="player2"></div>
        </div>
    </div>
</body>
</html>

Answered By – Xupitan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published