Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

how do i write this in shell script, ubuntu linux For this assignment, we will c

ID: 3918824 • Letter: H

Question

how do i write this in shell script, ubuntu linux

For this assignment, we will create a Blackjack game in which the user will play against the computer. We will use the following game rules:

Aces may be counted as 1 or 11 points. 2 to 9 according to pop value and tens and face cards count as ten points.

The value of a hand is the sum of the point values of the individual cards. Except, a "blackjack" is the highest hand, consisting of an ace and any 10-point card, and it outranks all other 21-point hands.

Game play:

The game will use a file based database to store the player's information which consists of their name and amount of money the user has to play.

One the shell script is started, the system asks the user for a user name. The system then checks to see if the user has played before by looking for a file named userName.txt

If the file exists, the system will read the file contents which only has the user name and balance into two variables.

If the file does not exist, the system will create a new file using the user name as the file name with a txt extension and initialize the user's balance to 100 dollars. After the file is created the user name and balance are loaded into two variables.

After the user information is loaded, the system will start the game as follows.

System will ask for bet. This information will also be saved into a variable.

Once the user places the bet, the system will give the user his first card. The value if the card must be calculated using a number random generator.

After the card is given to the user, the system will ask the user if he or she will stay or to continue with another card. This will be accomplished by printing a message that reads:Hit or Stay [H/S] :

If the user pressed H the system will hand another card and ask the same question again.

If the user hits S, the system will stop. And start it's turn by getting a card. At this point you will need to program how aggressive the computer will play.  

Conservative: The computer stays once it's cards reach 15

Moderate: The computer stays once it's cards reach 18

Aggressive: The computer stays once it's cards reach 20

Once the computer is done getting it's cards it must calculate it's hand and compare it with the user's hand. If the computer has a better hand the user looses and the bet is deducted from the user's balance. Otherwise, the bet is added to the user's balance and the appropriate message is displayed (Depending on the case, You loose or You win!)

Once this game is done, the computer will ask the user if he or she wants another game.

The system will print a menu with two options, play again or Quit [P/Q] If the user hits P, the game will start again and if the user hits Q the game will be over and the user's balance will be saved in the users file.

Explanation / Answer

Hi,
Please find below the script.
In given window of 120 min, i have added as much validations as possible.
Please go through it and run it as bash script on ubuntu machine.
You can change the mode of computer gameplay to aggressive, moderate or conservative in the script.
The username entered will create the txt file for that username in same working directory with username and initial balance of $100 as asked. Balance once exhausted will have to be updated manually.

#!/bin/bash

set -e -o pipefail

Mode=Moderate #Conservative|Moderate|Aggressive

case $Mode in

"Conservate")

limit=15

;;

"Moderate")

limit=18

;;

"Aggressive")

limit=20

;;

*)

echo "Unknown operating mode"

exit

esac

playAgain="P"

while [[ "$playAgain" == "P" ]] || [[ "$playAgain" == "p" ]]

do

echo -n "Enter username: "

read username

if [[ -f $username".txt" ]]

then

balance=`cat $username.txt | grep amount | cut -d "=" -f "2" `

echo -e "user exists! balance: $$balance"

else

echo "name=$username" > $username.txt

echo "amount=100" >> $username.txt

echo "user created!"

fi

echo "Minimum bet amount: 10"

if [[ $balance -lt 10 ]]

then

echo "Insufficient balance, ask administrator to add balance!"

exit

fi

echo -n "Enter the bet amount: "

read bet

re="^[0-9]+$"

while ! [[ "$bet" =~ $re ]] || [[ "$bet" -gt "$balance" ]] || [[ "$bet" -lt 10 ]]

do

echo -n "invalid amount entered or insufficient balance, enter again: "

read bet

done

echo "Let's start with the first card: "

card=$((RANDOM%13 + 1))

case $card in

11)

card2=J

;;

12)

card2=Q

;;

13)

card2=K

;;

*)

card2=$card

esac

echo "card: $card2"

userCount=$card

echo -n "Hit or Stay [H/S] : "

read reply

re="^[h,H,s,S]$"

while ! [[ $reply =~ $re ]]

do

echo -n "invalid response, please enter again: "

read reply

done

while [[ "$reply" == "h" ]] || [[ "$reply" == "H" ]]

do

card=$((RANDOM%13 + 1))

case $card in

11)

card2=J

card=10

face=1

;;

12)

card2=Q

card=10

face=1

;;

13)

card2=K

card=10

face=1

;;

*)

card2=$card

face=0

esac

if [[ $userCount == 1 ]] && [[ $face == 1 ]]

then

userBlackJack=1

face=0

break

fi

face=0

echo "card: $card2"

userCount=$((userCount+card))

echo "Total Count: $userCount"

if [[ $userCount -gt 21 ]]

then

echo "GameOver!"

balance=$((balance-bet))

sed -i "s/amount=.*/amount=$balance/g" $username.txt

reply="over"

break

fi

echo -n "Hit or Stay [H/S] : "

read reply

re="^[h,H,s,S]$"

while ! [[ $reply =~ $re ]]

do

echo -n "invalid response, please enter again: "

read reply

done

done

if [[ "$reply" != "over" ]]

then

echo "Bot's turn now!"

computerCount=0

while [[ $computerCount -lt $limit ]]

do

card=$((RANDOM%13 + 1))

case $card in

11)

card2=J

card=10

face=1

;;

12)

card2=Q

card=10

face=1

;;

13)

card2=K

card=10

face=1

;;

*)

card2=$card

face=0

esac

if [[ $computerCount == 1 ]] && [[ $face == 1 ]]

then

comBlackJack=1

fi

face=0

echo "card: $card2"

computerCount=$((computerCount+card))

echo "Total Computer Count: $computerCount"

if [[ $computerCount -gt 21 ]]

then

echo "GameOver! You won"

balance=$((balance+bet))

sed -i "s/amount=.*/amount=$balance/g" $username.txt

reply="over"

break

fi

if [[ $userBlackJack == 1 ]] && [[ $computerBlackJack != 1 ]]

then

echo "GameOver! You won"

balance=$((balance+bet))

sed -i "s/amount=.*/amount=$balance/g" $username.txt

reply="over"

break

elif [[ $userBlackJack != 1 ]] && [[ $computerBlackJack == 1 ]]

then

echo "GameOver! You loss."

balance=$((balance-bet))

sed -i "s/amount=.*/amount=$balance/g" $username.txt

reply="over"

break

elif [[ $computerCount -eq $userCount ]]

then

echo "GameOver! Match Drawn."

reply="over"

break

fi

if [[ $computerCount -gt $userCount ]]

then

echo "GameOver! You loss."

balance=$((balance-bet))

sed -i "s/amount=.*/amount=$balance/g" $username.txt

reply="over"

break

fi

sleep 1

done

fi

if [[ "$reply" == "over" ]]

then

echo -n "play again or Quit [P/Q]: "

read playAgain

fi

done