Shell Script Poker Game

Original Code Taken from here:

http://www.tldp.org/LDP/abs/html/bashver2.html#EX79

The code in the above link displays 4 unique 13 cards hands. I've modified it to deal a hand unique 2 card hand to 2 different players, then deal 5 unique community cards as in Texas Holdem (3 cards, then 1 card, then 1 card..)

However, I am finding that the DUPE_CHECK in the pick_a_card function and my deal_hole/deal_flop/deal_turn/deal_river is not work, hence, I'm getting non unique cards. I'm sure this is because it was previously going through all 52 cards and now I have it going through only 1-3 at a time, but I'm unsure of how to solve this problem. Any help would be appriciated:
Code:

#!/bin/bash

UNPICKED=0
PICKED=1

DUPE_CARD=99

LOWER_LIMIT=0
UPPER_LIMIT=51
CARDS_IN_SUIT=13
CARDS=52

declare -a Deck
declare -a Suits
declare -a Cards

initialize_Deck ()
{
i=$LOWER_LIMIT
until [ "$i" -gt $UPPER_LIMIT ]
do
  Deck=$UNPICKED   # Set each card of "Deck" as unpicked.
  let "i += 1"
done
echo
}

initialize_Suits ()
{
Suits[0]=C #Clubs
Suits[1]=D #Diamonds
Suits[2]=H #Hearts
Suits[3]=S #Spades
}

initialize_Cards ()
{
Cards=(2 3 4 5 6 7 8 9 10 J Q K A)
}

pick_a_card ()
{
card_number=$RANDOM
let "card_number %= $CARDS"
if [ "${Deck[card_number]}" -eq $UNPICKED ]
then
  Deck[card_number]=$PICKED
  return $card_number
else  
  return $DUPE_CARD
fi
}

parse_card ()
{
number=$1
let "suit_number = number / CARDS_IN_SUIT"
suit=${Suits[suit_number]}
echo -n "$suit-"
let "card_no = number % CARDS_IN_SUIT"
Card=${Cards[card_no]}
printf %-4s $Card
}

seed_random ()  
{               
seed=`eval date +%s`
let "seed %= 32766"
RANDOM=$seed
}

deal_hole ()
{
echo

cards_picked=0
while [ "$cards_picked" -le 1 ]
do
  pick_a_card
  t=$?

  if [ "$t" -ne $DUPE_CARD ]
  then
    parse_card $t

    u=$cards_picked+1
    let "u %= $CARDS_IN_SUIT"
    if [ "$u" -eq 0 ]   
    then
     echo
     echo
    fi
    
    let "cards_picked += 1"
    fi  
done  
}

deal_flop ()
{
echo

cards_picked=0
while [ "$cards_picked" -le 2 ]
do
  pick_a_card
  t=$?

  if [ "$t" -ne $DUPE_CARD ]
  then
    parse_card $t

    u=$cards_picked+1
    let "u %= $CARDS_IN_SUIT"
    if [ "$u" -eq 0 ]   
    then
     echo
     echo
    fi
    
    let "cards_picked += 1"
    fi  
done  
}

deal_turn ()
{
echo

cards_picked=0
while [ "$cards_picked" -le 0 ]
do
  pick_a_card
  t=$?

  if [ "$t" -ne $DUPE_CARD ]
  then
    parse_card $t

    u=$cards_picked+1
    let "u %= $CARDS_IN_SUIT"
    if [ "$u" -eq 0 ]   
    then
     echo
     echo
    fi
    
    let "cards_picked += 1"
    fi  
done  
}

deal_river ()
{
echo

cards_picked=0
while [ "$cards_picked" -le 0 ]
do
  pick_a_card
  t=$?

  if [ "$t" -ne $DUPE_CARD ]
  then
    parse_card $t

    u=$cards_picked+1
    let "u %= $CARDS_IN_SUIT"
    if [ "$u" -eq 0 ]   
    then
     echo
     echo
    fi
    
    let "cards_picked += 1"
    fi  
done  
}
####
seed_random
initialize_Deck
initialize_Suits
initialize_Cards
player1hand=`deal_hole`
player2hand=`deal_hole`
flop=`deal_flop`
turn=`deal_turn`
river=`deal_river`

clear

echo -n "What is player 1's name? "; read player1
echo -n "What is player 2's name? "; read player2

echo "$player1hand"
echo "$player2hand"
echo "$flop"
clear
echo "$flop $turn"
clear
echo "$flop $turn $river"

echo "$player1's hand was $player1hand and $player2's hand was $player2hand"

exit 0

I've omitted some code in the actual "game play" section, but it is inconsequential to the problem of duplicate cards.

I'm sort of lost on how to go about this. Is it possible to randomly assign every "hand" (Card and Suit combination) to a different variable and then draw on those randomly?

I thought it was already doing something similar when it was creating the Cards array.

Anyways, let me know what you think.

This is quite a mess. I don't see an easy way to fix it. I think a complete redesign is in order. The crux of your problem is code like:
player1hand=`deal_hole`

The shell must launch a sub-shell to process the `deal_hole` part. Within this subshell you mark some cards as being delt. Then the subshell exits and the main shell does know which cards were delt. You can't write a function that modifies global data and then invoke it in a subshell.

Good info Perderabo...

What would you suggest to keep things in order? Could I put the entire "play" code (meaning the part that is dealing and whatnot) into a single function?

I am not really strong with arrays, but if you could point me in the right direction I can probably figure it out.

Edit: Putting the play section to a function did not work, but I now see what you are talking about with the $player1hand issue spawning a subshell. What's a better way to save the player's hands so they can be referenced at the end of the script?

Edit2: Not assigning the output of the function (such as deal_hole) to a variable solves the problem, but I would like a way to reference the output later... is it possible?

Edit3: Ok, here's an idea... I'm having trouble finding if this is possible, but could I run the 'deal_hole' function and have the output send to stdout and then also redirected to a file that could be called later?

Have a deal_card function that sets a variable called current_card. Need 3 cards? do:
deal_card
card1=$current_card
deal_card
card2=$current_card
deal_card
card3=$current_card

I like that idea Perderabo... another way I found was to do

### Functions defined as above
deal_hole 2>&1 | tee -a player1hand
...
echo "Player 1's hand was `cat player1hand`"

Obviously your way is cleaner :slight_smile:

I'd simplify the whole thing by shuffling the deck and then dealing from it.

There are 3 functions in this snippet:
shuffle, which shuffles a standard deck of cards into a space-separated string of 52 cards, $Deck

_deal, which takes an optional argument of the number of cards to be dealt; it defaults to 1 if no number is supplied. The result is a space-separated string stored in $_DEAL; nothing is printed

deal, which calls _deal and prints the contents of $_DEAL, one card to a line


shuffle() #@ USAGE: shuffle
{ #@ TODO: add options for multiple or partial decks
 Deck=$(
   printf "%s\n" {1,2,3,4,5,6,7,8,9,J,Q,K,A}_{Hearts,Spades,Diamonds,Clubs} |
    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]
{      #@ RESULT: cards printed one to a line
    _deal "$@"
    printf "%s\n" $_DEAL
}

## Sample run
shuffle
deal 2
echo
deal 3
echo
deal

Sample output from above script:

K_Hearts
A_Clubs

3_Clubs
6_Clubs
2_Hearts

5_Clubs

Very interesting approach! :b:

Agreed... I really enjoy watching problems get tackled from different angles on this forum.

I really like that approach cfa --thanks.

I think I am assured now that these hands are fixed.