The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
poker-network 1.6.0 (Default branch) iBot Software Releases - RSS News 0 05-29-2008 10:50 AM
poker-network 1.4.0 (Default branch) iBot Software Releases - RSS News 0 03-24-2008 09:20 PM
Script for Hangman Game immyakram Shell Programming and Scripting 0 11-20-2007 06:46 PM
Linux game programing or just shell scripting Irish Jimmy Linux 3 09-02-2006 04:11 AM
update my game directly from a shell mikey1090 UNIX for Dummies Questions & Answers 0 04-10-2006 01:04 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 10-24-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
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:

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[i]=$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.
 

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0