proper distribution of cards in BASH terminal based crazy8's game SOLVED!
Thank you very much for your very helpful suggestion. I have solved that particular problem by adjusting my script as follows:
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
}
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 #################
# This section decides which player will play first
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
# This is the actual dealing of hands
tput cup 2 9
echo " $name's hand: "
echo
tput cup 4 9
deal 8
echo
echo
echo
tput cup 2 40
echo " The computer's hand: "
echo
deal 8 > /home/cogiz/computerhand.txt
CH=()
computerhand=$(</home/cogiz/computerhand.txt)
for i in computerhand; do
CH+=${computerhand[*]}
done
tput cup 4 40
echo $CH | sed -r 's/[0123456789JQKAHSDC]+/ * /g'
rm /home/cogiz/choice.txt
deal 1 > /home/cogiz/topcardinplay.txt
TCIP=()
card=$(</home/cogiz/topcardinplay.txt)
for i in card; do
TCIP+=${card[*]}
done
tput cup 8 36
echo $TCIP
tput cup 10 22
echo " The Top Card in Play"
# Running total of cards remaining in the Deck
tput cup 24 0
echo "cards remaining="$cards_remaining
Moderator's Comments:
Please use CODE tags as required by forum rules!
Last edited by RudiC; 11-14-2016 at 03:05 AM..
Reason: Added 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)