Skip to content

Instantly share code, notes, and snippets.

@Harvnlenny
Last active August 29, 2015 14:23
Show Gist options
  • Save Harvnlenny/5b1535ab8a862628ec8c to your computer and use it in GitHub Desktop.
Save Harvnlenny/5b1535ab8a862628ec8c to your computer and use it in GitHub Desktop.
Blackjack where have you been?
#!/usr/bin/evn 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)
@http.post("/games", "game[name]=#{name}")
end
def self.hit(id)
@http.patch("/games/#{id}/hit", '')
end
def self.stay(id)
@http.patch("/games/#{id}/stay", '')
end
end
class Game
def self.hand(game)
puts "Your cards are.."
puts game["player_hand"][0..-1]
puts "The dealer's cards are.."
puts game["dealer_hand"][0..-1]
end
def self.run
puts "What is your name?"
name = gets.chomp
game = JSON.parse(Blackjack.res(name).body)
#puts game
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 = JSON.parse(Blackjack.hit(id).body)
puts "You get a new card."
elsif answer == "stay"
game = JSON.parse(Blackjack.stay(id).body)
puts "You stay."
else
puts "Uh oh, you pressed an incorrect key."
end
end
puts "The winner is #{game["winner"]}"
puts game
#game = JSON.parse(http.patch("/games/#{id}/hit", '').body)
end
end
Game.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment