Skip to content

Instantly share code, notes, and snippets.

@utumno86
Last active August 29, 2015 14:23
Show Gist options
  • Save utumno86/d1887b73639759565e01 to your computer and use it in GitHub Desktop.
Save utumno86/d1887b73639759565e01 to your computer and use it in GitHub Desktop.
Blackjack API Interface
#!/usr/bin/env ruby
require 'net/http'
require 'json'
BLACKJACK_API_HOST = 'pure-forest-blackjack.herokuapp.com'
class Blackjack
@http = Net::HTTP.new(BLACKJACK_API_HOST)
def self.res(name)
JSON.parse(@http.post("/games", "game[name]=#{name}").body)
end
def self.hit(id)
JSON.parse(@http.patch("/games/#{id}/hit", '').body)
end
def self.stay(id)
JSON.parse(@http.patch("/games/#{id}/stay", '').body)
end
end
class Game
def self.hand(game)
puts "#{game["name"]}'s hand:"
game["player_hand"].each do |card|
puts card
end
puts "You can see one of the dealer's cards"
puts game["dealer_hand"][1]
end
def self.run
puts "Name?"
name = gets.chomp
game = Blackjack.res(name)
if game["dealer_score"] == 21
puts "Dealer gets a Blackjack! Sucks to be you."
game["winner"] = "dealer"
elsif game["player_score"] == 21 && game["dealer_score"] != 21
puts "Cool! You got a Blackjack!"
game["winner"] = "player"
end
while game["winner"] == nil
Game.hand(game)
id = game["id"]
puts "Would you like to hit or stay?"
answer = gets.chomp
if answer == "hit"
game = Blackjack.hit(id)
puts "You draw a card"
elsif answer == "stay"
game = Blackjack.stay(id)
puts "You stay"
else
puts "I'm sorry, you've chosen an incorrect option. Choose a coorect one or I will have to sic Cthulu on you."
end
end
if game["player_score"] > 21
puts "You went bust"
end
puts "#{game["name"]}'s hand:"
game["player_hand"].each do |card|
puts card
end
puts "Dealer's Hand:"
game["dealer_hand"].each do |card|
puts card
end
if game["winner"] == "player"
puts "Congratulations!!!! #{game["name"]} wins!"
else
puts "The dealer won. Ha-hah!"
end
end
end
Game.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment