In: Computer Science
In Java Script how do you code an array with the values and suits of cards (all 52 cards in a deck) 2 different array one that just has the face values of the card, and the other with the suit value. Then have it randomly give you 1-2 cards from the deck without repeating? (Example: Dealer gives player two cards. Player asks for a hit and the dealer gives a different card, no repeats). Game is Blackjack.
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]> <html class="no-js"> <!--<![endif]-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<script>
var suits = ["Diamonds", "Hearts", "Clubs", "Spades"];
var ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var deck = new Array();
var hand = new Array();
var score = 0;
function initCard(rank, suit) {
// Function to create and return a card using the provided rank and suit
if ((ranks[rank] == "J") || (ranks[rank] == "Q") || (ranks[rank] == "K"))
value = 10; // In blackjack, Face cards has value 10
else if (ranks[rank] == "A")
value = 11; // In blackjack, Ace card has value 11
else
value = parseInt(ranks[rank]);
return { Rank: ranks[rank], Suit: suits[suit], Value: value};
}
function initDeck() {
// Funtion to create a deck of 52 cards.
deck = new Array();
for (var suit = 0; suit < 4; suit++) {
for (var rank = 0; rank < 13; rank++) {
var card = initCard(rank, suit)
deck.push(card);
}
}
}
function shuffle() {
// Function to shuffle the deck.
// Changing position of 2 cards randomly for 100 times.
for (var i = 0; i < 100; i++) {
var pos1 = Math.floor(Math.random() * deck.length);
var pos2 = Math.floor(Math.random() * deck.length);
var temp = deck[pos1];
deck[pos1] = deck[pos2];
deck[pos2] = temp;
}
}
function dealCard() {
// Function to deal a card into the hand.
// A card dealt is removed from the deck so, repeating is not possible.
var card = deck.pop();
hand.push(card);
score += card.Value;
return score;
}
function startGame() {
// Execution starts from here
// Initializing the deck of cards.
initDeck();
// Shuffle
shuffle();
// Dealing 2 cards like in blackjack.
dealCard();
dealCard();
console.log(JSON.parse(JSON.stringify(deck))); // Print the deck of cards after dealing of 2 cards
console.log(JSON.parse(JSON.stringify(hand))); // Showing the cards in hand
console.log(score); // Showing the score,
// JSON.parse(JSON.stringify(obj)) is used to print the value at the time of console.log() is called.
// If not used then console.log() will print the most recent values of the object.
// Hit
while ( score < 21 ) // According to blackjack
{
shuffle();
dealCard();
console.log(JSON.parse(JSON.stringify(hand)));
console.log(score);
}
}
startGame(); // Calling the start game function to start the game.
// Outputs can be seen in Inspect > Console.
</script>
</body>
</html>
OUTPUT: