Open Side Menu Go to the Top
Register
Linux, BASH script for a shufle and dealing simulation Linux, BASH script for a shufle and dealing simulation

07-23-2016 , 02:05 AM
Not sure if this is the correct forum but I thought I'd share a very simple BASH script (Linux) to simulate a 52 card deck shuffle and dealing to a number of players in a Texas hold 'em game. The purpose is to replace manual shuffling and dealing for running simulations.

A similar Online tool
(I was specifically looking for a UNIX terminal tool hence the hobby project)
- https://www.random.org/playing-cards/

Disclaimer: I am not a professional programmer so if there are any bugs or features missing, you are mostly on your own.

Currently the output format is written for a simulation of shuffle and dealing to 1, 2, 9 and 10 players only. I haven't written the output for 3-9 players situation and the script will throw an error for this condition.

Dependencies
- Linux OS (Haven't tested on UNIX)
- /usr/bin/bash (BASH)
- shuf program (part of GNU coreutils)

Almost all Linux distributions have the above dependencies installed.

Instructions.

1. Copy and paste bellow output into a new file. Save the file as "Cards.txt". Name and case matters.
Code:
Ac
As
Ah
Ad
Kc
Ks
Kh
Kd
Qc
Qs
Qh
Qd
Jc
Js
Jh
Jd
Tc
Ts
Th
Td
9c
9s
9h
9d
8c
8s
8h
8d
7c
7s
7h
7d
6c
6s
6h
6d
5c
5s
5h
5d
4c
4s
4h
4d
3c
3s
3h
3d
2c
2s
2h
2d
2. Copy and paste bellow output into a new file. Save the file as shuffle_up_and_deal.sh Name doesn't matter as long as you can find it.
Code:
#!/usr/bin/bash
SHUFFLED_DECK_ARRAY=($(shuf Cards.txt))
clear
echo ""
echo "Welcome to Shuffle Up and Deal simulation."
echo ""
echo -n "Enter how many players you would like to deal in [9]: "
read NUMBER_OF_PLAYERS
NUMBER_OF_PLAYERS=${NUMBER_OF_PLAYERS:-9}
if [[ "$NUMBER_OF_PLAYERS" -gt 10 || "$NUMBER_OF_PLAYERS" -lt 1 ]]; then
	echo ""
	echo "Error. You must set number of players between 1 and 10."
	echo "Press the ENTER key to exit and try again ..."	
	read PAUSE
	exit
fi
echo ""
echo -n "Enter how many shuffles you would like to do [10]: "
read NUMBER_OF_SHUFFLES
NUMBER_OF_SHUFFLES=${NUMBER_OF_SHUFFLES:-10}
if [[ "$NUMBER_OF_SHUFFLES" -lt 1 ]]; then
	echo ""
	echo "Error. You must do at least 1 shuffle."
	echo "Press the ENTER key to exit and try again ..."	
	read PAUSE
	exit
fi
echo ""
echo "OK, now shuffling and dealing players in... "
echo "Press the ENTER key to continue"
read PAUSE
SHUFFLE=1	
if [[ "$NUMBER_OF_PLAYERS" -eq 10 ]]; then 
while [[ "$NUMBER_OF_SHUFFLES" -gt 0 ]]
do
	clear
	echo "Hand #"$SHUFFLE""
	echo ""
	echo "My hand		Flop		Turn	River"					
	echo -n "${SHUFFLED_DECK_ARRAY[0]} "				
	echo -n "${SHUFFLED_DECK_ARRAY[1]}"		
	echo "		${SHUFFLED_DECK_ARRAY[@]0:49}	${SHUFFLED_DECK_ARRAY[48]}	${SHUFFLED_DECK_ARRAY[47]}"
	echo ""
	echo ""	
	echo -n "${SHUFFLED_DECK_ARRAY[2]} "		
	echo  "${SHUFFLED_DECK_ARRAY[3]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[4]} "		
	echo  "${SHUFFLED_DECK_ARRAY[5]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[6]} "		
	echo  "${SHUFFLED_DECK_ARRAY[7]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[8]} "		
	echo  "${SHUFFLED_DECK_ARRAY[9]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[10]} "		
	echo  "${SHUFFLED_DECK_ARRAY[11]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[12]} "		
	echo  "${SHUFFLED_DECK_ARRAY[13]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[14]} "		
	echo  "${SHUFFLED_DECK_ARRAY[15]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[16]} "		
	echo  "${SHUFFLED_DECK_ARRAY[17]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[18]} "		
	echo  "${SHUFFLED_DECK_ARRAY[19]}"		
	echo ""
	echo ""
	let NUMBER_OF_SHUFFLES=$NUMBER_OF_SHUFFLES-1		
		if [[ "$NUMBER_OF_SHUFFLES" -eq 0 ]]; then 
			echo ""	
			echo "No more shuffles to do"
			echo "Press the ENTER key to quit the game"
			read PAUSE
			echo
			exit 
		fi	
	let SHUFFLE=$SHUFFLE+1 					
	echo -n "Press the ENTER key to continue to next shuffle..."
	read PAUSE
	SHUFFLED_DECK_ARRAY=($(shuf Cards.txt))		
done
fi
if [[ "$NUMBER_OF_PLAYERS" -eq 9 ]]; then 
while [[ "$NUMBER_OF_SHUFFLES" -gt 0 ]]
do
	clear
	echo "Hand #"$SHUFFLE""
	echo ""
	echo "My hand		Flop		Turn	River"					
	echo -n "${SHUFFLED_DECK_ARRAY[0]} "				
	echo -n "${SHUFFLED_DECK_ARRAY[1]}"		
	echo "		${SHUFFLED_DECK_ARRAY[@]0:49}	${SHUFFLED_DECK_ARRAY[48]}	${SHUFFLED_DECK_ARRAY[47]}"
	echo ""
	echo ""	
	echo -n "${SHUFFLED_DECK_ARRAY[2]} "		
	echo  "${SHUFFLED_DECK_ARRAY[3]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[4]} "		
	echo  "${SHUFFLED_DECK_ARRAY[5]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[6]} "		
	echo  "${SHUFFLED_DECK_ARRAY[7]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[8]} "		
	echo  "${SHUFFLED_DECK_ARRAY[9]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[10]} "		
	echo  "${SHUFFLED_DECK_ARRAY[11]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[12]} "		
	echo  "${SHUFFLED_DECK_ARRAY[13]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[14]} "		
	echo  "${SHUFFLED_DECK_ARRAY[15]}"		
	echo -n "${SHUFFLED_DECK_ARRAY[16]} "		
	echo  "${SHUFFLED_DECK_ARRAY[17]}"		
	echo ""
	echo ""
	let NUMBER_OF_SHUFFLES=$NUMBER_OF_SHUFFLES-1		
		if [[ "$NUMBER_OF_SHUFFLES" -eq 0 ]]; then 
			echo ""	
			echo "No more shuffles to do"
			echo "Press the ENTER key to quit the game"
			read PAUSE
			echo
			exit 
		fi	
	let SHUFFLE=$SHUFFLE+1 					
	echo -n "Press the ENTER key to continue to next shuffle..."
	read PAUSE
	SHUFFLED_DECK_ARRAY=($(shuf Cards.txt))		
done
fi
if [[ "$NUMBER_OF_PLAYERS" -eq 2 ]]; then 
while [[ "$NUMBER_OF_SHUFFLES" -gt 0 ]]
do
	clear
	echo "Hand #"$SHUFFLE""
	echo ""
	echo "My hand		Flop		Turn	River"					
	echo -n "${SHUFFLED_DECK_ARRAY[0]} "				
	echo -n "${SHUFFLED_DECK_ARRAY[1]}"		
	echo "		${SHUFFLED_DECK_ARRAY[@]0:49}	${SHUFFLED_DECK_ARRAY[48]}	${SHUFFLED_DECK_ARRAY[47]}"
	echo ""
	echo ""	
	echo -n "${SHUFFLED_DECK_ARRAY[2]} "		
	echo  "${SHUFFLED_DECK_ARRAY[3]}"		
	echo ""
	echo ""
	let NUMBER_OF_SHUFFLES=$NUMBER_OF_SHUFFLES-1		
		if [[ "$NUMBER_OF_SHUFFLES" -eq 0 ]]; then 
			echo ""	
			echo "No more shuffles to do"
			echo "Press the ENTER key to quit the game"
			read PAUSE
			echo
			exit 
		fi	
	let SHUFFLE=$SHUFFLE+1 					
	echo -n "Press the ENTER key to continue to next shuffle..."
	read PAUSE
	SHUFFLED_DECK_ARRAY=($(shuf Cards.txt))		
done
fi
if [[ "$NUMBER_OF_PLAYERS" -eq 1 ]]; then 
while [[ "$NUMBER_OF_SHUFFLES" -gt 0 ]]
do
	clear
	echo "Hand #"$SHUFFLE""
	echo ""
	echo "My hand		Flop		Turn	River"					
	echo -n "${SHUFFLED_DECK_ARRAY[0]} "				
	echo -n "${SHUFFLED_DECK_ARRAY[1]}"		
	echo "		${SHUFFLED_DECK_ARRAY[@]0:49}	${SHUFFLED_DECK_ARRAY[48]}	${SHUFFLED_DECK_ARRAY[47]}"
	echo ""
	echo ""
	let NUMBER_OF_SHUFFLES=$NUMBER_OF_SHUFFLES-1		
		if [[ "$NUMBER_OF_SHUFFLES" -eq 0 ]]; then 
			echo ""	
			echo "No more shuffles to do"
			echo "Press the ENTER key to quit the game"
			read PAUSE
			echo
			exit 
		fi	
	let SHUFFLE=$SHUFFLE+1 					
	echo -n "Press the ENTER key to continue to next shuffle..."
	read PAUSE
	SHUFFLED_DECK_ARRAY=($(shuf Cards.txt))		
done
fi
if [[ "$NUMBER_OF_PLAYERS" -gt 2 ]] && [[ "$NUMBER_OF_PLAYERS" -lt 9 ]] ; then
	clear
	echo ""
	echo "Modify script to display entries for 3 - 9 players."
	echo "Press the ENTER key to exit..."
	read PAUSE
	echo ""
	exit
fi
exit
3. Make sure that "Cards.txt" file and "shuffle_up_and_deal.sh" file are both in the same directory.

4. To run simply execute $ sh shuffle_up_and_deal.sh in a Linux (virtual) terminal.

Hope you enjoy it and find it useful.

Last edited by _dave_; 07-24-2016 at 03:13 AM. Reason: code tags ffs
Linux, BASH script for a shufle and dealing simulation Quote
07-23-2016 , 10:59 AM
This is nice and all, but why on earth use bash?
Linux, BASH script for a shufle and dealing simulation Quote
07-23-2016 , 11:47 AM
Quote:
Originally Posted by RustyBrooks
This is nice and all, but why on earth use bash?
Thanks. It's just what I am somewhat familiar with
Linux, BASH script for a shufle and dealing simulation Quote
07-23-2016 , 01:31 PM
I see. I mean, knowing how to use bash is great, it's extremely handy, but it's also a fairly blunt tool. Almost any programming language would be easier to program in, probably faster, easier to debug, more extensible, etc. For speed people usually use C/C++ or Java (or anything that runs well in a JVM). For ease/flexibility maybe a language that is a bit looser, such as python, ruby, or javascript. (You can also do the crucial calculations in C/C++ and load them as modules in most scripting languages, that's the approach I take)
Linux, BASH script for a shufle and dealing simulation Quote
07-25-2016 , 10:23 AM
I wrote an AI Poker bot in LISP. Below are the three functions to make a deck, shuffle the cards, and deal the cards. I actually don't think I wrote this shuffle function--I found it somewhere. But it's pretty cool that you can do this with such short code.

Your Bash script is cool--one thing I'd suggest is breaking it up into small functions.

Code:
(defparameter *suits* '(spades diamonds hearts clubs))
(defparameter *values* '(14 13 12 11 10 9 8 7 6 5 4 3 2))

(defun make-deck ()
  (mapcan #'(lambda (suit)
              (mapcar #'(lambda (value) (make-card :suit suit :value value))
                *values*))
    *suits*))

;;shuffles the cards. 
(defun shuffle (deck)
  (if (null (cdr deck))
      nil
    (let* ((this-card (car deck))
           (cdr-size (length (cdr deck)))
           (loc-to-swap (random cdr-size))
           (card-to-swap (nth loc-to-swap (cdr deck))))
      (setf (car deck) card-to-swap)
      (setf (nth loc-to-swap (cdr deck)) this-card)
      (shuffle (cdr deck)))))
	 
;;; Make it, then shuffle it and return it.
(defun make-shuffled-deck ()
  (let ((deck (make-deck)))
    (shuffle deck)
    deck))

Last edited by _dave_; 07-27-2016 at 12:07 AM. Reason: omg code tags
Linux, BASH script for a shufle and dealing simulation Quote

      
m