Skip to content

Instantly share code, notes, and snippets.

@timkellogg
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timkellogg/e41a88ede0c90231f506 to your computer and use it in GitHub Desktop.
Save timkellogg/e41a88ede0c90231f506 to your computer and use it in GitHub Desktop.
Game Suite in Bash/Ruby Scripting
#!/usr/bin/env ruby
# Draws the first cards for the game
def first_draw
@player_hand = @deck.to_a.sample(2).to_h
@computer_hand = @deck.to_a.sample(2).to_h
puts "Your Hand: #{@player_hand.keys.first} #{@player_hand.keys.last}"
puts "Computer's Hand: #{@computer_hand.keys.first}"
check_ace
computer_hit
check_win
end
# Allows user to pick between 1 and 11 for ace value
def check_ace
if @player_hand.has_value?("option")
puts "What do you want the ace to be worth? 1 or 11"
ace_value = Integer(gets.chomp)
if [1, 11].include?(ace_value)
@player_hand.each_key { |k| @player_hand[k] = ace_value if @player_hand[k] == "option" }
else
check_ace
end
else
end
end
# Initialize function to start the game; sets up deck and data structures for hands, scores
def start_game
@deck = { HA: "option", HK: 10, HQ: 10, HJ: 10, H10: 10, H9: 9, H8: 8, H7: 7, H6: 6, H5: 5, H4: 4, H3: 3, H2: 2,
SA: "option", SK: 10, SQ: 10, SJ: 10, S10: 10, S9: 9, S8: 8, S7: 7, S6: 6, S5: 5, S4: 4, S3: 3, S2: 2,
CA: "option", CK: 10, KQ: 10, CJ: 10, C10: 10, C9: 9, C8: 8, C7: 7, C6: 6, C5: 5, C4: 4, C3: 3, C2: 2,
DA: "option", DK: 10, DQ: 10, DJ: 10, D10: 10, D9: 9, D8: 8, D7: 7, D6: 6, D5: 5, D4: 4, D3: 3, D2: 2
}
@player_score = 0
@computer_score = 0
@player_hand = Hash.new
@computer_hand = Hash.new
first_draw
end
# Hit/Stay loop
def main_loop
get_score("player")
puts "Your hand is: #{@player_hand} which is worth #{@player_score}."
puts "Hit (H) or Stay (S)?"
@user_hit = gets.chomp!
computer_hit
while @user_hit.downcase == 'h'
@player_hand.merge!(@deck.to_a.sample(1).to_h)
puts "Your hand is now #{@player_hand}"
check_ace
check_win
end
computer_hit
check_win
end
# Calculates computer score; computer allow has aces worth 11
def computer_hit
if @computer_hand.has_value?("option")
ace_value = 11
@computer_hand.each_key { |k| @computer_hand[k] = ace_value if @computer_hand[k] == "option" }
else
end
get_score("computer")
while @computer_score < 17
@computer_hand.merge!(@deck.to_a.sample(1).to_h)
computer_hit
end
end
# Calculates scores for object passed in (either computer or player)
def get_score(type)
if type == "computer"
@computer_score = @computer_hand.values.inject(:+)
elsif type == "player"
@player_score = @player_hand.values.inject(:+)
end
end
# Checks win conditions
def check_win
get_score("player")
get_score("computer")
# Check if won
if @player_score == 21 && @computer_score != 21
end_game("player")
elsif @player_score == 21 && @computer_score == 21
end_game("computer")
elsif @player_score > 21
end_game("computer")
elsif @computer_score > 21
end_game("player")
elsif @user_hit == 's'
if @computer_score < 21
if @computer_score > @player_score
end_game("computer")
else
end_game("player")
end
end
else
main_loop
end
end
# Handles winning/losing dialog and re
def end_game(who_won)
if who_won == "player"
puts "+===============+"
puts "| Congrats! |"
puts "+===============+"
else
puts "+========================+"
puts "| Sorry, you lost! |"
puts "+========================+"
end
get_score("player")
get_score("computer")
puts "Your final hand: #{@player_hand.keys} worth #{@player_score}"
puts "Computer's final hand: #{@computer_hand.keys} worth #{@computer_score}"
puts "Play again? n or y"
play_again = gets.chomp!
if play_again.downcase! == "y"
start_game
else
exit
end
end
start_game
#!/bin/bash
# Game Loop
playAgain='y'
while [ $playAgain == 'y' ]
do
# Game Functions
function RollDie() {
diOne="$((RANDOM%6 + 1))"
diTwo="$((RANDOM%6 + 1))"
sum=$(($diOne+$diTwo))
Rolls[$diRoll]=$sum
echo "You rolled a $diOne and a $diTwo for a total of $sum."
echo "Your rolls are now: ${Rolls[*]}"
}
function winGame() {
echo "That means you win!"
if [ $diRoll -eq 0 ]
then
echo "You won on your first roll, nice!"
else
echo "It took you an extra $diRoll roll(s) to win."
fi
echo "Play again? Y or N"
read playAgain
}
function loseGame() {
echo "That means you lose!!"
echo "Play again? Y or N"
read playAgain
}
function startGame() {
echo `clear`
echo "WELCOME TO THE CRAPS TABLE"
diRoll=0
winGame=0
declare -a Rolls=()
}
startGame
RollDie
# Player rolls 0,7,11 on the first roll, they win
if [[ $diRoll -eq 0 ]] && [[ $sum -eq 7 ]] || [[ $sum -eq 11 ]]
then
winGame
#
elif [[ $diRoll -eq 0 ]] && [[ $sum -eq 2 ]] || [[ $sum -eq 3 ]] || [[ $sum -eq 12 ]]
then
loseGame
else
echo "You aren't done yet!"
echo "You rolled a $sum"
echo "Press enter to try for $sum again?"
read enter
winGame=$sum
diRoll=$((diRoll + 1))
RollDie
while [[ diRoll -ne 0 ]] && [[ $winGame -ne $sum ]]
do
diRoll=$((diRoll + 1))
if [[ $sum -eq 7 ]]
then
loseGame
else
echo "Press enter to try for $sum again?"
read enter
RollDie
fi
done
fi
done
#!/bin/bash
option=0
while [ $option=0 ]
do
echo "=================="
echo
echo "Welcome to Game Me"
echo "Please choose an option below"
echo
echo "1. CRAPS"
echo "2. GUESSING GAME"
echo "3. BLACKJACK"
echo "4. EXIT"
echo
echo "================="
read option
case $option in
1) `echo bash craps.sh`
;;
2) `echo bash guessinggame.sh`
;;
3) `echo ruby blackjack.rb`
;;
4) exit 1
;;
*) echo "Sorry, that isn't a choice"
option=0
;;
esac
done
#!/bin/bash
# Game Loop
playAgain='y'
while [ $playAgain == 'y' ]
do
# Map the prize choice to random cost of price
function createPrize() {
if [ $prize == "TV" ]
then
prize_cost="$((RANDOM%300 + 200))"
startGame
elif [ $prize == "AUTO" ]
then
prize_cost="$((RANDOM%10000 + 5000))"
startGame
elif [ $prize == "REFRIG"]
then
prize_cost="$((RANDOM%600 + 300))"
startGame
else
echo "Uh oh, something went wrong!"
fi
}
# Return a win or loss
function decideGame() {
if [[ $guess = $prize_cost ]]
then
echo "YOU WON a $prize WORTH $prize_cost DOLLARS!"
echo "PLAY AGAIN?"
read playAgain
else
echo "TIME RAN OUT!"
echo "THE CORRECT PRICE OF THE $prize WAS $prize_cost."
echo "PLAY AGAIN?"
read playAgain
fi
}
# Main Game/Timing Loop
function startGame() {
echo "YOU HAVE ONE MINUTE TO GUESS THE PRICE OF THE PRIZE"
echo "WHAT IS YOUR FIRST GUESS?"
read guess
# Set timing in seconds
secs=60
SECONDS=0
while [[ $guess -ne $prize_cost ]] && [[ $secs -gt $SECONDS ]]
do
if [[ $guess -lt $prize_cost ]]
then
echo "GUESS HIGHER?"
read guess
elif [[ $guess -gt $prize_cost ]]
then
echo "GUESS LOWER?"
read guess
else
echo "OOPS, SOMETHING WENT WRONG. MAKE SURE TO ENTER A NUMBER!"
startGame
fi
done
decideGame
}
# Menu to decide which option to select
function menu() {
option=0
while [ $option=0 ]
do
echo `clear`
echo '=========================='
echo 'WELCOME TO GUESSING GAME!'
echo '1 COLOR TV'
echo '2 REFRIGERATOR'
echo '3 AUTO'
echo '=========================='
echo 'ENTER YOUR CHOICE (1,2,3)'
read option
case $option in
1) echo "Time to win that TV!"
prize="TV"
createPrize
;;
2) echo "Time to win that REFRIG!"
prize="REFRIG"
createPrize
;;
3) echo "Time to win that AUTO!"
prize="AUTO"
createPrize
;;
*) echo "SORRY, THAT ISN'T A CHOICE."
option=0
;;
esac
done
}
menu
done
echo `bash gameme.sh`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment