Proper distribution of cards in terminal based crazy8's game in bash script
When I run the following script at the bottom it say cards remaining=44...It should be=35.
Can anyone tell me what I'm doing wrong. I've spent hours trying to get this to work and I can't go any further until this part works. thank you in advance
Cogiz
Code:
#!/bin/bash
# Date="November, 2016"
# Author/Constructor="Steve Sullivan"
###############################
# #
# CRAZY8 #
# #
###############################
############# FUNCTIONS ###############
shuffle() #@ USAGE: shuffle
{ #@ TODO: add options for multiple or partial decks
Deck=$(
printf "%s\n" {2,3,4,5,6,7,8,9,10,J,Q,K,A}{H,S,D,C} |
awk '## Seed the random number generator
BEGIN { srand() }
## Put a random number in front of each line
{ printf "%.0f\t%s\n", rand() * 99999, $0 }
' |
sort -n | ## Sort the lines numerically
cut -f2 ## Remove the random numbers
)
}
_deal() #@ USAGE: _deal [N] -- where N is no. of cards; defaults to 1
{ #@ RESULT: stored in $_DEAL
local num=${1:-1}
set -- $Deck
_DEAL=${@:1:$num}
shift "$num"
cards_remaining=$#
Deck=$*
}
deal() #@ USAGE: deal [N]
{
_deal "$@"
echo $_DEAL
}
_computerdeal() #@ USAGE: _deal [N] -- where N is no. of cards; defaults to 1
{ #@ RESULT: stored in $_COMPUTERDEAL
local num=${1:-1}
set -- $Deck
_COMPUTERDEAL=${@:1:$num}
shift "$num"
cards_remaining=$#
Deck=$*
}
computerdeal() #@ USAGE: deal [N]
{
_computerdeal "$@"
echo $_COMPUTERDEAL | tee /home/cogiz/computerhand.txt
}
randomchoicecomputer() {
echo $RANDOM % 2 + 1 | bc > /home/cogiz/choice.txt 2>&1
}
randomchoiceplayer() {
FD=$(</home/cogiz/choice.txt)
echo
echo
echo " $name, the computer has secretly chosen between numbers 1 and 2."
echo
echo " If you choose the same number then you will play first."
echo
echo " If you choose the wrong number then the computer will play first."
echo
echo " The computer has chosen: $FD" | sed -r 's/[12]+/*/g'
echo
read -p " Please choose either 1 or 2: " nbr
echo
if [ "$nbr" -eq "$FD" ]; then
echo " $name has chosen: $nbr"
echo
echo " $name, you guessed right, you will play first."
sleep 3
echo
else
echo " The computer has chosen $FD"
echo
echo " You guessed wrong, the computer will play first."
sleep 3
echo
fi
}
############### MAIN SCRIPT #################
clear
echo
read -p " What is your Name?: " name
clear
echo
echo " Hello $name,"
echo
echo " Let's play some Crazy 8's ..."
sleep 2
clear
randomchoicecomputer
randomchoiceplayer
echo
clear
echo
echo " Shuffling cards ..."
sleep 2
clear
shuffle
echo
echo " Dealing hands ..."
sleep 2
clear
tput cup 2 10
echo " $name's hand: "
echo
tput cup 4 10
deal 8
echo
echo
echo
tput cup 2 40
echo " The computer's hand: "
echo
tput cup 4 40;
computerdeal 8 | sed -r 's/[0123456789JQKAHSDC]+/ * /g'
echo
echo
rm /home/cogiz/choice.txt
echo " The Card in Play is: "$(deal) | tee /home/cogiz/cardinplay.txt
tput cup 24 0
echo "cards remaining="$cards_remaining
Last edited by Scrutinizer; 11-13-2016 at 02:53 AM..
Reason: code tags
How can I get a bash script to wait and read and count $i messages that a running program (drbl clonezilla) sends to the console (terminal) and only then move on to the next line in the script when the count is matched (the next line is the last line of the script and is a reboot)?
The script... (0 Replies)
Hello All,
I was wondering if it would be possible to create a "racing" game in script. The game play would be as follows. Script will read the following input:
Start
|b| | | | |
|r| | | | |
First player (b) will roll a die to see how many spaces to move. This is will continue until the... (0 Replies)
How can I make a bash script that keeps on running after I have closed the terminal?
Or a script that runs without having the terminal window open? (1 Reply)
Okay so Zork sparked my interest in this. I have been learning to program for the last year and a half. I've dabbled in everything from Java to Ruby to PHP & XHTML & SQL, and now I'm on bash. I really like bash scripting. Its easy and fun. I just started two days ago. Pretty much I've been writing... (1 Reply)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
So, I made a script beginning with #!/bin/bash on gedit.
And I double clicked it to run in terminal and I end up with "The child process exited normally with status 127" and "command not found".
If I run the same script from the terminal as "tcsh (script name)" it runs just fine.
If I... (8 Replies)
Hello guys I'm new to shell scripting and I need to make a game using shell script. I want to know if it is possible for me a total noob to shell scripting to make this game.
The game concept is simple:
First thing when you launch the script you get a menu in which you select if you want to... (3 Replies)