Skip to content

Instantly share code, notes, and snippets.

@DouweM
Forked from Osagiede/21r.rb
Created April 12, 2014 23:56
Show Gist options
  • Save DouweM/10562742 to your computer and use it in GitHub Desktop.
Save DouweM/10562742 to your computer and use it in GitHub Desktop.
# define global variables here
# define methods here
# for human
user_cards = 0
card_one = rand(1..11)
card_two = rand(1..11)
# for computer
cpu_cards = rand(1..11)
cpu_card = rand(1..11)
# 1 - 11 for computer maybe?
while cpu_cards < 16
cpu_cards += rand(1..11)
end
def help()
begin
puts
puts "21 is a card game. Your opponent will be the computer."
sleep 4
puts
puts "The game is very simple to play, here's how."
sleep 4
puts
puts "Try to get a number card value to as close to 21 as you can."
sleep 5
puts
puts "Anything beyond 21 is an automatic loss."
sleep 4
puts
puts "If you get a number card value of 21, you automatically win."
sleep 5
puts
puts "Your commands: "
puts
sleep 2
puts "Type in h - To add a random number card value 1 - 11 to your existing amount."
puts
sleep 4
puts "Type in s - To stay with your current number card value."
puts
puts "Your card value will be compared to the computer's to determine the winner."
sleep 6
puts
puts "Type in help - To have these rules explained to you again."
puts
sleep 4
puts "Do you now understand the rules?"
puts "Please type in (Y/N): "
puts
user_input = gets.chomp
while user_input != "Y" user_input != "N"
puts "I am sorry, can you please repeat that?"
print "Please enter in (Y/N): "
user_input = gets.chomp
end
if user_input == "Y"
puts
puts "Good."
puts
elsif user_input == "N"
puts
help
end
end
end
# First: Have computer ask if we want to play a game
puts
puts "~Blackjack~"
puts
puts "Welcome to 21, would you like to play?"
print "(Y/N): "
user_input = gets.chomp
if (user_input != "Y") && (user_input != "N")
begin
puts
puts "What was your answer, I didn't catch that."
print "Please enter (Y/N): "
user_input = gets.chomp
end while (user_input != "Y") && (user_input != "N")
end
if user_input == "Y"
puts
puts "Great! Lets get started."
puts
end
if user_input == "N"
puts
puts "Have a nice day!"
abort
end
# Second: Have the computer ask if we know how to play
# If not, have the computer explain the rules
puts "Do you know how to play 21 and do you know the commands?"
print "(Y/N): "
user_input = gets.chomp
if (user_input != "Y") && (user_input != "N")
begin
puts
puts "What was your answer, I didn't catch that."
print "Please enter (Y/N): "
user_input = gets.chomp
end while (user_input != "Y") && (user_input != "N")
end
if user_input == "N"
puts
puts "Your objective is to get more points then your opponent."
sleep 3
puts
puts "Your opponent is the computer."
sleep 2.5
puts
puts "If you get 21 points before cpu then you automatically win."
sleep 3
puts
puts "However, you want to avoid going over 21 or you lose!"
sleep 3
puts
puts "To get any amount of cards, you need to make a selection."
sleep 3
puts
puts "There are two selections that are made in 21"
sleep 2.5
puts "Hit and Stay"
sleep 2
puts
puts
puts
puts "About Hit:"
puts
puts "To select hit type in h"
sleep 2
puts
puts "Hit will add a random number 1 - 11 to your existing amount of points."
sleep 3
puts
puts "WARNING!: If used to often then you might go over 21 and lose the game."
sleep 3
puts
puts "In order to avoid losing the game, you would choose a different selection (Stay)"
sleep 3
puts
puts
puts
puts "About Stay:"
puts
puts "To select Stay type in s"
sleep 2
puts
puts "Stay will not to add any more points to the amount of points you already have."
sleep 3
puts "You would select stay if you are confident with the amount of points you possess"
sleep 3.5
puts
puts "This selection cannot be undone so think before making this decision."
sleep 3
puts
puts "For more elaboration, type in help."
puts
puts "Now that you know the rules, let's get started!"
sleep 5
puts
end
if user_input == "Y"
puts
puts "Great, lets get started!"
puts "*Dealing cards*"
sleep 2
end
# Third: You would receive 2 cards
# Display what cards equal to the user_cards total here?
user_cards = card_one + card_two
puts
puts user_cards
if user_cards >= 22
sleep 2
puts
puts "Bust!"
sleep 3
puts
puts "Computer wins"
puts
puts "You: #{user_cards} Computer: #{cpu_cards}"
abort
end
if user_cards == 21
sleep 2
puts "Blackjack!~"
puts "Congratulations! You won!"
puts "Computer: #{cpu_cards} You: #{user_cards}"
abort
end
# Fourth: The computer would ask for 'hit' or 'stay'
puts
puts "What would you like to do?"
puts
user_input = gets.chomp
if user_input == "help"
help
puts "What would you like to do now?"
puts "Please choose a command"
puts
user_input = gets.chomp
end while user_input == "help"
if (user_input != "h") && (user_input != "s") && (user_input != "help")
begin
puts "What was your answer, I didn't catch that."
puts
puts "Please put a command"
user_input = gets.chomp
if user_input == "help"
help
end
end while (user_input != "h") && (user_input != "s") && (user_input != "help")
end
if user_input == "h"
begin
if user_input == "help"
help
puts "What would you like to do now?"
user_input = gets.chomp
end while user_input == "help"
if user_input == "h"
puts
puts "*Deals a card*"
user_cards = user_cards += rand(1..11) # Use a range rand(1..11)
sleep 2
puts
puts user_cards
end
if user_cards == 21
sleep 2
puts
puts "Blackjack!"
sleep 3
puts
puts "Congratulations! You win!"
puts
puts "You: #{user_cards} Computer: #{cpu_cards}"
abort
elsif user_cards > 21
sleep 2
puts
puts "Bust!"
sleep 3
puts
puts "Computer wins"
puts
puts "You: #{user_cards} Computer: #{cpu_cards}"
abort
elsif user_input == "s"
break
end
puts
puts "What would you like to do?"
puts
user_input = gets.chomp
end while user_cards < 21
end
puts
puts "You have chosen to stay with #{user_cards}."
puts
puts
sleep 4
puts "You: #{user_cards} Computer: #{cpu_cards}"
puts
# Fifth: Computer will compare user_cards and cpu_cards
if cpu_cards == user_cards
puts
puts "It is a tie"
sleep 2
puts
puts "Computer: #{cpu_cards} You: #{user_cards}"
abort
end
if cpu_cards && user_cards > 21 #how computer amount going to go over 21 if it's 1..21?? how to make it add numbers realistically human???
puts
puts "No one is a winner."
sleep 4
puts
puts "Computer: #{cpu_cards} You: #{user_cards}"
abort
end
if cpu_cards < user_cards
puts
puts "Congratulations! You win!"
sleep 2
puts
puts "Computer: #{cpu_cards} You: #{user_cards}"
abort
end
if cpu_cards > user_cards
puts
sleep 3
puts "Computer wins"
puts
puts "Computer: #{cpu_cards} You: #{user_cards}"
abort
end
# Idea
# If this is successful
# Make fake money?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment