Question

In: Computer Science

In Java Script how do you code an array with the values and suits of cards...

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.

Solutions

Expert Solution

<!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:


Related Solutions

Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
• In this script, write MATLAB code to create an array A of size 100 ×...
• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of...
[JAVA SCRIPT] Please create an array of student names and another array of student grades. Create...
[JAVA SCRIPT] Please create an array of student names and another array of student grades. Create a function that can put a name and a grade to the arrays. Keep Read student name and grade until student name is “???”. And save the reading by using a function Create another function to show all the grade in that object. Create the third function that can display the maximum grade and the student’s name. Create a sorting function that can sort...
how do you send the values of a method to the main in obj java. im...
how do you send the values of a method to the main in obj java. im having syntax errors and im not sure how to fix them or what the problem is. class Order { private int orderNum; private double orderAmount; private double orderDiscount;    public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public double getOrderAmount() { return orderAmount; } public double getOrderDisc() { return orderDiscount; } public static void...
Unix Create a script that will declare an array and assign four values from the command...
Unix Create a script that will declare an array and assign four values from the command line. 1. Use these four values - Paul, Ringo, George, John, - in that order 2. Display the content of the array, displaying the values in this format and this order The first array value is "John" The second array value is "Paul" The third array value is "George" The fourth array value is "Ringo"
In JAVA Directions: You must trace through the code and determine the status of the array....
In JAVA Directions: You must trace through the code and determine the status of the array. The code can be found on the second page of this packet. Assume Array A = Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Value 33 12 39 6 -2 30 15 11 55 100 40 39 1 How many elements does A have? ______________ What is the start index? _______________ (assume this value is stored in a...
given an array, write code to scan the array for a particular purpose . use java
given an array, write code to scan the array for a particular purpose . use java
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
IN JAVA Create an array and add random values to it, and then find the sum...
IN JAVA Create an array and add random values to it, and then find the sum of the values using recursion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT