Ruby Blackjack (Part 1)

unnamed.png
Last week we were assigned a lab that required us to create the game blackjack in Ruby.

This has been the bane of my existence ever since. I am still not done (and it was due today). I have struggled and written, deleted and rewritten code over and over.

But I have been able to work out a few kinks, so as I continue to work on this, I plan on sharing. I, myself have been trolling the internet for help, but there did seem to be that much out there to help me out.

So here is my tiny contribution to the ruby world.
st_gemcutter_f.jpg

My first problem was creating the cards for the deck. So that is what I will show in this post. Disclaimer: There are many ways to do this, this is mine (so far) and I posted it cause it works (finally).

I couldn’t quite figure out what to do…so I ended up creating each card…individually. It ain’t pretty, but it works..

Here it goes:

create a class for the card, and initialize it (properly). In this case I used attr_accessor

class Card

attr_accessor :suit

attr_accessor :rank

attr_accessor :value

def initialize(suit,rank,value)

@suit = suit

@rank = rank

@value = value

end

end

create another class for deck (not sure if you need this) and an empty
array to put your cards in

class Deck

deck = []

and here come the cards…

Diamonds
#

deck << Card.new(“Diamonds”, “Ace”, 11)

deck << Card.new(“Diamonds”, “King”, 10)

deck << Card.new(“Diamonds”, “Queen”, 10)

deck << Card.new(“Diamonds”, “Jack”, 10)

deck << Card.new(“Diamonds”, “Ten”, 10)

deck << Card.new(“Diamonds”, “Nine”, 9)

deck << Card.new(“Diamonds”, “Eight”, 8)

deck << Card.new(“Diamonds”, “Seven”, 7)

deck << Card.new(“Diamonds”, “Six”, 6)

deck << Card.new(“Diamonds”, “Five”, 5)

deck << Card.new(“Diamonds”, “Four”, 4)

deck << Card.new(“Diamonds”, “Three”, 3)

deck << Card.new(“Diamonds”, “Two”, 2)

Hearts
#

deck << Card.new(“Hearts”, “Ace”, 11)

deck << Card.new(“Hearts”, “King”, 10)

deck << Card.new(“Hearts”, “Queen”, 10)

deck << Card.new(“Hearts”, “Jack”, 10)

deck << Card.new(“Hearts”, “Ten”, 10)

deck << Card.new(“Hearts”, “Nine”, 9)

deck << Card.new(“Hearts”, “Eight”, 8)

deck << Card.new(“Hearts”, “Seven”, 7)

deck << Card.new(“Hearts”, “Six”, 6)

deck << Card.new(“Hearts”, “Five”, 5)

deck << Card.new(“Hearts”, “Four”, 4)

deck << Card.new(“Hearts”, “Three”, 3)

deck << Card.new(“Hearts”, “Two”, 2)

Spades
#

deck << Card.new(“Spades”, “Ace”, 11)

deck << Card.new(“Spades”, “King”, 10)

deck << Card.new(“Spades”, “Queen”, 10)

deck << Card.new(“Spades”, “Jack”, 10)

deck << Card.new(“Spades”, “Ten”, 10)

deck << Card.new(“Spades”, “Nine”, 9)

deck << Card.new(“Spades”, “Eight”, 8)

deck << Card.new(“Spades”, “Seven”, 7)

deck << Card.new(“Spades”, “Six”, 6)

deck << Card.new(“Spades”, “Five”, 5)

deck << Card.new(“Spades”, “Four”, 4)

deck << Card.new(“Spades”, “Three”, 3)

deck << Card.new(“Spades”, “Two”, 2)

Clubs
#

deck << Card.new(“Clubs”, “Ace”, 11)

deck << Card.new(“Clubs”, “King”, 10)

deck << Card.new(“Clubs”, “Queen”, 10)

deck << Card.new(“Clubs”, “Jack”, 10)

deck << Card.new(“Clubs”, “Ten”, 10)

deck << Card.new(“Clubs”, “Nine”, 9)

deck << Card.new(“Clubs”, “Eight”, 8)

deck << Card.new(“Clubs”, “Seven”, 7)

deck << Card.new(“Clubs”, “Six”, 6)

deck << Card.new(“Clubs”, “Five”, 5)

deck << Card.new(“Clubs”, “Four”, 4)

deck << Card.new(“Clubs”, “Three”, 3)

deck << Card.new(“Clubs”, “Two”, 2)

end

Now you have a deck populated with cards. You can see that if you type

puts deck

I will have to refactor this code later, but I plan on continuing to make something that works til I get to the end.

In the meantime, I will be continuing to work on it, and posting updates.

I am working on adding the player and dealer next, so if anyone out there can help write code…holla!

 
31
Kudos
 
31
Kudos

Now read this

Back 2 The Start…

These last few years have been a constant whirlwind. From leaving my job, to learning how to code, to becoming a freelancer, and moving beyond, I have been constantly learning. And while I have learned to sustain drinking from the fire... Continue →