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
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...
(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...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I...
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I want to be able to scan and output which number appears the most and the least. int x =1000 int[] array = new array[x] for(int i = 0 ; i < x; i++){ array[i] = random.nextInt(101); }
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT