Blackjack Ruby Code
- This fast-paced casino card game is easy to learn and fun to play online. Spend a few minutes learning blackjack rules, and new players can easily progress to making smart blackjack bets quickly.Practice using one of our 50 free blackjack games now before playing blackjack for Blackjack Ruby Code.
- Below is the code for a simulation of a simple blackjack game. The player and dealer (house) draw two cards. The player then hits until he reaches 17. The dealer (house) then hits until he draws with the player or goes bust. Considering the player plays first the odds of him winning should be less than 50% for any strategy.
Learning Goals
- Utilize conditional logic and looping
- Gain an introduction to the command line interface
Wagering for the match bonus is set at 30 times the deposit and bonus amounts. No withdrawal limits for the match! Use the code VFGKQXGHF and receive $25 free chip. Make a deposit of $30 with the code TA6TMF and get 250% match bonus.
Background
In Blackjack, the goal is to have ahand that is closer to 21 than the dealer's hand without ever exceeding a cardtotal of 21.
However, in this simplified version of Blackjack, we'll cut out that 'comparewith the dealer's hand' part and pretend that the goal of the game is to have acard total of, or very close to, but never exceeding, 21.
To start, a player gets dealt two cards, each of which has values between 1-11.Then, the player is asked if they want to 'hit' (get another card dealt tothem), or 'stay'.
If they hit, they get dealt another card. If the sum of their three cardsexceeds 21, they've lost. If it does not, they can decide to hit or stay againFOREVER.
If you're thinking, 'But now there's no way to win!', then you'd be right. Inthis simple, simple version of Blackjack, there is no way to win. Once you'vecompleted this lab, feel free to make a second version where there IS a way towin. Maybe even include the real rules and compare the user's hand to thedealer's hand.
The Command Line Interface
A Brief Note: This is a brief introduction to command line apps. It's okayif you don't understand everything we discuss here. We're going to take a morein-depth look in your next command line application.
The CLI, or command line interface, is the interaction between a user and theircomputer or a program via the command line. You've already become comfortableinteracting with the command line to navigate files and connect with GitHub andtest your programs. In a command line application, the user will respond to aprompt that your program will output to the terminal. The user's response, orinput, will be received by the application and the application will then carryout the programmed response based on that input.
How Does puts
Output Text to the Terminal?
How do the puts
and print
methods actually output text to your console? Theyuse the $stdout
global variable provided to us by Ruby. You don't need to worryabout global variables right now. For the purposes of understanding how puts andprint work, we just need to understand the following:
Your computer has a stdout
file that communicates with your operating system.So, puts
and print
actually send output to the $stdout
variable. The $stdout
variable sends that information to the stdout
file on your computer which inturn communicates with your operating system which in turn outputs thatinformation to the console.
You can absolutely employ puts
and print
without understanding everything thatwas just described. But now, you have a basic sense of what is happening underthe hood of these methods.
Running Our Command Line App
We already know that we can run, or execute the code in a Ruby file from thecommand line by typing ruby <file name>
. In a command line app, it isconventional to create a special file that has one responsibility: executing thecode that constitutes our program. You can think about this in terms of theseparation of concerns principle. The separation of concerns principle is aprogramming design principle for separating the responsibilities andfunctionalities into discrete sections. For our command line app, that meansthat we have one file that defines the methods we will use to play our blackjackgame and a separate file that calls those methods. Then, we will play our gameby executing the that 'runner' file via ruby runner.rb
in the command line.
Testing Our Command Line App
You already know that your Blackjack command line app will rely on the user'sinput to run. In order to test our program using RSpec, we need a way for ourtest suite to fake the user's input, i.e. fake the implementation of the puts
and gets
methods. This is called stubbing.
What is Stubbing?
Stubbing refers to the fake implementation of a method. In this case, we willstub the puts
method to trick our test suite into thinking the stdout
file hasreceived the puts
method and to trick our test suite into recognizing thatthe gets
method has been used.
First:
The above line means that the test suite is expecting the execution of a certainmethod to use the puts
method to output 'Type 'h' to hit or 's' to stay'.
Second:
The above line means that the test suite it_self_ is expecting the execution ofa certain method, :get_user_input
, to use the gets
method to store the user'sinput and return that input (which in this particular test happens to be 's'
).
Enacting a Ruby Program Via a 'Runner' Method
It is common practice to break down the constituent parts of a larger programinto smaller methods. Each method should be responsible for one job. Thesemethods can be called in succession inside a larger method to enact the runningof the program.
For example, if we were writing a simple app to greet a user and ask them theirname, we might have a short method to output a welcome message, another methodto ask them their name, a third method to capture the user's input, a fourthmethod to output a new, personalized greeting that uses that input and a lastmethod that calls on each of the smaller methods to make the whole thing run.Our shorter methods might look like this:
To run our program, we need to call all of these methods. Instead of callingeach of them in turn, we might place them inside a single method. Then, toenact our program, we only have to invoke our one wrapper or runner method.
The run_program
method can then be invoked inside of a runner file, discussedabove. Such a file would only need to contain one line!
This is the basic pattern that we will be using to code our simple blackjackgame. In the first part of this lab, we'll be defining our smaller methods, eachof which is responsible for one discrete unit of the game. These methods arecalled helper methods. Once we have all of our helper method tests passing,we'll define the runner method that calls on each of the helper methods in turnto make the program run.
Instructions
All your code will go in lib/blackjack.rb
.
Once every test is working, run ruby lib/runner.rb
from the root directory toplay!
Using TDD
This lab is test driven, so run learn
to guide you through the program.
Read the test output very carefully! Pay attention to whether or not thetest is telling you that the method should be defined to take in an argument.Pay attention to what the test expects the return value of the method to be.
We're going to take a look at one example together and for the rest of themethods, you'll be required to let the tests guide you.
In the test suite, we have the following test:
This test is telling us the following things about the method calleddisplay_card_total
:
- The method should take in an argument of a number that is the card total.
- The method should use
puts
to output that card total as part of the phrase'Your cards add up to #{card total}'
.
Defining Our Methods
The welcome
Method
This method uses puts
to output the message: 'Welcome to the Blackjack Table'.
The deal_card
Method
This method generates and returns a random number between 1 and 11.
The display_card_total
Method
This method accepts an argument of a number and puts out the phrase 'Your cardsadd up to #{card_total}'. The number that this method takes in as an argument isthe sum of a players cards. This method will be called inside another method,at which point the real sum of a player's cards will be passed in as anargument. This is not important right now. Just define the method to take in anumber and puts out the appropriate phrase using that number.
The prompt_user
Method
This method asks the user for input by outputting the phrase 'Type 'h' to hit or's' to stay'.
The get_user_input
Method
This method is very basic. It only needs to use the gets
method to capture theuser's input. Eventually, when we take all of these helper methods and assemblethem into the larger method that enacts the gameplay, this method will be usedafter we prompt the user for input to actually capture and store their input.
The end_game
Method
This method takes in an argument of a number, which will be a player's cardtotal, and outputs the message 'Sorry, you hit #{card_total}. Thanks forplaying!'
The initial_round
Method
This method represents the first round of the game for a given player. It shouldcall on the deal_card
method twice, use the display_card_total
method toputs
out the sum and then return the sum. This method will, therefore, call ontwo other helper methods, deal_card
and display_card_total
, which takes inan argument of the sum of both invocations of deal_card
.
The hit?
Method
This method is a bit more complex. It should take in an argument of the player'scurrent card total. So, set up your hit?
method to take in an argument of anumber.
Then, the method should use the prompt_user
method to prompt the user forinput and the get_user_input
method to get and store the user's input. Now weneed to implement some logic. If the player's input is 's'
, we don't deal anew card. If the player's input is 'h'
, we do need to deal a new card. In thiscase, use the deal_card
method to deal a new card and increment the player'scard total by whatever number is returned by deal_card
.
If the player's input is neither'h'
nor's'
, call on theinvalid_command
method to output the phrase 'Please enter a valid command'.Then, call on the prompt_user
method again to remind your user what a validcommand is.
In either case, our method should then return the player's current card total.
The Runner Method: runner
Once you get all of the tests in the first part of the test suite passing, youhave built the building blocks of our blackjack game. Now, we need to put themall together in the runner
method. The runner
method is responsible forenacting the gameplay until the user loses. Remember that a player loses ifthe sum of their cards exceeds 21.
Here's how we want our game to run:
- Welcome the user
- Deal them their first two cards, i.e. their
initial_round
- Ask them if they want to hit or stay
- If they want to stay, ask them again!
- If they want to hit, deal another card and display the new total
- If their card total exceeds 21, the game ends.
Use a loop constructor (I'd recommend until
, but that is by no means your onlyoption) to enact the above gameplay in the runner
method. Then, check out thelib/runner.rb
file. Notice that it is simply calling the runner
method. Therunner file will call the runner
method which should, in turn, utilize all theother methods you built!
Resources
- Wikipedia - Blackjack
View Blackjack CLI on Learn.co and start learning to code for free.
Skip to main contentPremium Rubberized Fibered Coating
Black JackĀ® Rubr-Coat #57 is a premium rubberized coating for waterproofing roofs & foundations. This liquid applied asphalt coating has a blend of rubber and reinforcing fibers to provide a strong, flexible, waterproof seal. Rubr-Coat has exceptional adhesion to existing roofing, masonry surfaces, wood, foam board insulation and metal surfaces. The addition of rubber enhances performance by improving crack resistance, life expectancy and adds increased waterproofing protection. Product is black in color. As a Roof Coating, this product seals worn out roof surfaces and provides a tough flexible waterproof seal. As a Foundation Coating, this product seals masonry, foam board insulation, wood and metal surfaces to protect, preserve and damp-proof, below grade surfaces. This multi-use product forms a tough rubberized seal for lasting protection on a variety of surfaces. Rubr-Coat may be used to adhere foam board insulation to masonry surfaces. When used to coat the exterior surface this product will act as a protective coating for the foam board.
>Find Nearest storeresources & useful info
- SAFETY DATA SHEET
- technical data SHEET
Coverage Calculator
why you will love it
Blackjack Ruby
Flexes to resist cracking
Rubberized for superior waterproofing and adhesion
Lasts twice as long as standard coatings
Low odor, water cleanup