Register
It is currently Wed Apr 23, 2014 11:32 am

Bridge (the card game) script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Mar 31, 2010 12:26 am   

Joined: Wed Mar 31, 2010 12:17 am
Posts: 4
Just finished a 12 hour bash class and started writing this program to get some practice. I would like to continue but not sure that Bash is capable of much more. I may take a "C" class soon but my instructor suggested Python as the next step.

Any suggestions are appreciated.


Top
 Profile  
 PostPosted: Wed Mar 31, 2010 12:27 am   

Joined: Wed Mar 31, 2010 12:17 am
Posts: 4
#!/bin/bash
# NOTE this worked as a simple display of one hand and dealing the deck

# Sets array1 to the numbers 0 to 51
deck_numbers=( $( seq 0 51 ) )

# Bash's command substitution syntax to store the results of the tput command
term_clear=$(tput clear)

# Stores the number of lines or rows on the terminal display
max_lines=$(tput lines)

# Stores the number of columns on the terminal display
max_cols=$(tput cols)

# Clear the screen
echo $term_clear

declare -a deck_cards
declare -a Hand1
declare -a Hand1n
declare -a Hand1nn
declare -a Hand1f
declare -a Hand2
declare -a Hand3
declare -a Hand4
declare -a Hand2f
declare -a Hand3f
declare -a Hand4f
declare -a Hand2n
declare -a Hand3n
declare -a Hand4n

A=52
x=13
while ((x>=0 && A>0))
do
g=$(($RANDOM%$A))
Hand1[${#Hand1[*]}]=${deck_numbers[g]}
deck_numbers=(${deck_numbers[@]:0:g} ${deck_numbers[@]:g+1})
let A--
g=$(($RANDOM%$A))
Hand2[${#Hand2[*]}]=${deck_numbers[g]}
deck_numbers=(${deck_numbers[@]:0:g} ${deck_numbers[@]:g+1})
let A--
g=$(($RANDOM%$A))
Hand3[${#Hand3[*]}]=${deck_numbers[g]}
deck_numbers=(${deck_numbers[@]:0:g} ${deck_numbers[@]:g+1})
let A--
g=$(($RANDOM%$A))
Hand4[${#Hand4[*]}]=${deck_numbers[g]}
deck_numbers=(${deck_numbers[@]:0:g} ${deck_numbers[@]:g+1})
let A--
let x--
done

# Put hands in order idea use deal_numbers and then order numbers in greater to smaller
declare -i k
declare -i l
for ((i = 0; i <= 12; ++i))
do
for ((j = 0; j <= 12; j++));do
if [[ ${Hand1[i]} -ge ${Hand1[j]} ]]
then
let k++
fi
done
let k--
Hand1n[k]=${Hand1[i]}
k=0
done
declare -i k
declare -i l
for ((i = 0; i <= 12; ++i))
do
for ((j = 0; j <= 12; j++));do
if [[ ${Hand2[i]} -ge ${Hand2[j]} ]]
then
let k++
fi
done
let k--
Hand2n[k]=${Hand2[i]}
k=0
done
declare -i k
declare -i l
for ((i = 0; i <= 12; ++i))
do
for ((j = 0; j <= 12; j++));do
if [[ ${Hand3[i]} -ge ${Hand3[j]} ]]
then
let k++
fi
done
let k--
Hand3n[k]=${Hand3[i]}
k=0
done
declare -i k
declare -i l
for ((i = 0; i <= 12; ++i))
do
for ((j = 0; j <= 12; j++));do
if [[ ${Hand4[i]} -ge ${Hand4[j]} ]]
then
let k++
fi
done
let k--
Hand4n[k]=${Hand4[i]}
k=0
done

# then look up for cards
deck_cards=(As Ks Qs Js 10s 9s 8s 7s 6s 5s 4s 3s 2s Ah Kh Qh Jh 10h 9h 8h 7h 6h 5h 4h 3h 2h Ac Kc Qc Jc 10c 9c 8c 7c 6c 5c 4c 3c 2c Ad Kd Qd Jd 10d 9d 8d 7d 6d 5d 4d 3d 2d)
for ((i = 0; i <= 12; ++i))
do
p=${Hand1n[i]}
Hand1f[i]=${deck_cards[p]}
done
for ((i = 0; i <= 12; ++i))
do
p=${Hand2n[i]}
Hand2f[i]=${deck_cards[p]}
done
for ((i = 0; i <= 12; ++i))
do
p=${Hand3n[i]}
Hand3f[i]=${deck_cards[p]}
done
for ((i = 0; i <= 12; ++i))
do
p=${Hand4n[i]}
Hand4f[i]=${deck_cards[p]}
done

# print hand to screen
s=0
h=0
c=0
d=0
for ((j = 0; j <= 12; ++j))
do
card=${Hand1f[j]}
if [[ $card == *s ]]
then
let s++ # count spades in this hand
elif [[ $card == *h ]]
then
let h++ # count hearts in this hand
elif [[ $card == *c ]]
then
let c++ # count clubs in this hand
elif [[ $card == *d ]]
then
let d++ # count diamonds in this hand
fi
done
# prints to screen one hand and other players
indent=$((2 * max_cols / 5))
line_num=$((max_lines / 3))
q=$line_num
tput cup $q $(( indent ))
echo "North"
indent1=$((max_cols / 5))
line_num2=$((max_lines / 2))
q=$line_num2
tput cup $q $(( indent1 ))
echo -n "West"
indent2=$((2 * max_cols / 3))
line_num3=$((max_lines / 2))
q=$line_num3
tput cup $q $(( indent2 ))
echo "East"


# Finds the vertical center of the terminal.
line_num4=$(( 2 * max_lines / 3 ))
i=0
p=0
q=$line_num4
tput cup $q $(( indent ))
echo "South"
((q++))
i=0
for suit in $s $h $c $d
do
while (($suit > 0));do
tput cup $q $(( indent + p ))
echo -n "${Hand1f[i]} "
((p+=${#Hand1f[i]}))
((p++))
((suit--))
((i++))
done
p=0
((q++))
done

# Place the cursor at the bottom of the screen --where is usually at.
tput cup $max_lines 0

exit 0


Top
 Profile  
 PostPosted: Wed Mar 31, 2010 12:28 am   

Joined: Wed Mar 31, 2010 12:17 am
Posts: 4
I tried uploading the script but that didn't happen for some reason. Sorry that the indenting was lost on the copy paste.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: deb and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP