Question
In Bash(Shell Scripting) Fill in the empty functions and show output Bash BlackJack project !/bin/bash function rules { cat <
In Bash(Shell Scripting) Fill in the empty functions and show output
Bash BlackJack project
!/bin/bash
function rules { cat <<'EOF'
A "deck" containing some number of standard decks of (52) cards is shuffled. Cards are dealt one at a time from the deck. When the deck is nearly (or completely) empty, a new deck is used. (Actually the old deck is re-stocked with cards and is shuffled.) The dealer and player are each dealt two cards. The player can only see their two cards and the first card of the dealer's hand. Each hand has a value obtained by summing the value of each card held, with picture cards worth 10 points. An ace is worth either one or 11 points. The purpose of the game is to build a hand worth more than the dealer's hand, without going over 21 points. The player can draw as many cards as desired, called taking "hits". When taking a hit, if the hand value exceeds 21 the player loses. When satified the player "stands pat" and it is the dealer's turn. The "house rules" require the dealer to draw a card with a hand value of 16 or less and to stand pat with 17 or more. If the dealer's hand value exceeds 21 the player wins. Otherwise the player or dealer with the highest value hand wins, a tie going to the dealer. EOF }
# Create an empty "deck" (array), to hold a deck of 52 cards: deck=()
# This function shuffles the (global) array "deck": function shuffle { local i j temp for (( i=${#deck[@]}; i ; --i )) do j=$(( $RANDOM % i )) temp=${deck[i]} deck[i]=${deck[j]} deck[j]=$temp done }
# This function deals and returns (in "$?") one card, then # removes it from the deck. When the deck is empty, a new # deck is created and shuffled: function deal { if [ ${#deck[@]} -eq 0 ] then deck=(`seq 0 51`) shuffle fi
local card=${deck[0]} # The next two lines remove the first card from the deck: unset deck[0] deck=(${deck[@]}) return $card }
# this function nicely displays a player's hand, which # is really an array of ints 0..51 representing cards: function show_hand { # FILL IN THIS FUNCTION }
# This function returns the value of a player's hand. # Note picture cards count as 10, an Ace counts as 1 # unless the total is under 11, in which case an Ace # counts as 11: function value { local value=0 has_ace=0 for card do let 'card = card % 13 + 1' [ $card = 1 ] && has_ace=1 [ $card -gt 10 ] && card=10 let 'value += card' done if [ $value -lt 12 -a $has_ace -eq 1 ] then let 'value += 10' fi return $value }
# This function displays the dealer's and player's hands: function print_hands { #FILL IN THIS FUNCTION }
# Main program starts here:
echo -n "Welcome to Black-Jack! Would you like to see the rules? (y/n) " read REPLY if [ "$REPLY" = "y" ] then rules fi
# Deal two cards to the dealer: deal dealers_hand=($?) deal dealers_hand=(${dealers_hand[@]} $?)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started