Proper distribution of cards in terminal based crazy8's game in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Proper distribution of cards in terminal based crazy8's game in bash script
# 1  
Old 11-13-2016
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
# 2  
Old 11-13-2016
The problem is in the two following lines:
Code:
computerdeal 8 | sed -r 's/[0123456789JQKAHSDC]+/ * /g'

and
Code:
echo "          The Card in Play is: "$(deal) | tee /home/cogiz/cardinplay.txt

In both cases the deal and computerdeal function are executed in a subshell, in the first case as the left hand side of a pipe, in the second case as part of a command substitution. The global variables that are set inside those functions are local to that subshell. When the subshell finishes, those variables and their values will be lost...

By contrast the first time the deal function is called:
Code:
deal 8

it is done in the current shell itself and the value of global variable cards_remaining that gets set inside that function is the value that gets printed at the end..

So instead you could run those functions in the current shell and then use the global variables that are being set inside them...

Last edited by Scrutinizer; 11-13-2016 at 03:45 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-13-2016
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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-14-2016 at 03:05 AM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating a text based game using shell script.

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)
Discussion started by: Othmane
3 Replies

2. Shell Programming and Scripting

Tetris Game -- based on a shell script (new algorithm)

GitHub - deepgrace/tetris: Tetris implementation in all kinds of Programming Languages Usage: bash Tetris_Game ] ] ] ] Range: #!/bin/bash # Tetris Game // The Art Of Shell Programming box0=(4 30) box1=(4 30 4 32) box2=(4 30 5 32) box3=(4 28 4 30 4 32) box4=(4 28 4 30... (69 Replies)
Discussion started by: complex.invoke
69 Replies

3. Shell Programming and Scripting

Bash tcsh Script runs in terminal but not folder

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)
Discussion started by: OntorEska
8 Replies

4. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

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)
Discussion started by: LaurenRose
3 Replies

5. Shell Programming and Scripting

Making a Text based game. Need help.

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)
Discussion started by: lemonoid
1 Replies

6. UNIX for Dummies Questions & Answers

Is there picture based game under linux terminal?

Is there picture based game under linux terminal? Just like Supermario under DOS. (18 Replies)
Discussion started by: vistastar
18 Replies

7. Shell Programming and Scripting

Run bash script without terminal

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)
Discussion started by: locoroco
1 Replies

8. Shell Programming and Scripting

Bash Script for "simple" racing game.

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)
Discussion started by: jl487
0 Replies

9. Shell Programming and Scripting

Reading output from terminal back into bash script

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)
Discussion started by: dp123
0 Replies

10. UNIX for Dummies Questions & Answers

text based football game?

Is there a textbased football game (American) that I can download through ubuntu server edition? (1 Reply)
Discussion started by: dadoprso
1 Replies
Login or Register to Ask a Question