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.