Impressions of making BlackJack-cli with Ruby

I found such an article on Qiita, so I made BlackJack with Ruby. Graduation exam from programming beginners should develop "Blackjack" | Qiita


My impression is that I just made a simple game, but it was a lot of fun. After trying to make BlackJack, I felt like I could make various things other than games, so I'm thinking of playing while changing the code of BlackJack a little more and then thinking about what to make next.

All the code is posted on Github so you can check it here BlackJack Ruby | Ryutaro - Github

Blackjack rules

The rules for blackjack were created based on the rules described in the article here.

--The initial card is 52. Make sure there are no duplicate cards when drawing --A two-player match between the player and the dealer. Players run, dealers run automatically --At the start of execution, the player and the dealer each draw two cards. The drawn card is displayed on the screen. However, do not know the dealer's second card --After that, the player draws the card first. Burst if the number of players exceeds 21, the game ends at that point --Players can choose to draw the next card each time they draw a card --Once the player has finished drawing, the dealer will continue to draw until his or her hand is 17 or higher. --A game when the player and the dealer finish drawing. The one closer to 21 wins --J, Q and K are treated as 10 --A is treated only as "1" for the time being. Do not set to "11" --No double down, no split, no surrender, no other special rules

Where I was worried

--About the class to prepare --Whether to use instance method or class method --Where should the data be retained (deck, hand, etc.)

Prepared class

I have created the following 6 classes. The role of each class is as follows.

#A class that prepares card numbers and marks such as diamonds and hearts in an array
class Card

#It inherits the Card class, a class that creates a deck from the prepared cards and shuffles it.
class Deck < Card

#A class that performs common processing between the player and dealer superclasses
class PlayerBase

#It inherits PlayerBase, which creates player-specific processing.
class Player < PlayerBase

#It inherits PlayerBase, which creates a dealer-specific process.
class Dealer < PlayerBase

#A class that holds the processing required for the progress of the game and data such as hands and decks
class BlackJack

Since there were many common processes between the player and the dealer, creating a superclass called PlayerBase made it possible to reuse the methods, and I feel that it was easy to proceed.

Should it be an instrument method or a class method?

I was worried about this too and googled things like "ruby instance method class method difference" many times.

As a result, I decided with a slightly abstract judgment that "basically create with an instance method, and if you feel that it is a little difficult to use with an instance method, let's make it a class method".

** Class method ** Example

class Card
  class << self
    def numbers
      no = [1, 2, 3, 4, 5, 6, 7 ,8 ,9 ,10, 11, 12, 13]

    def suit
      suit = ["Diamond", "heart", "club", "spade"]

** Instance method ** Example

class PlayerBase

  def create_hand(deck)
# .
# .
# .

Which class should hold data such as hand and deck

The data storage location is the most troublesome place. At first, I thought, "Because it's a player's hand, let's manage it in the Player class, and because it's a dealer's hand, let's manage it in the Dealer class. Since the deck is common, it's the PlayerBase class." Since there were many troublesome things such as two decks being created at that time, it was decided to keep all the data in the BlackJack class.

Detailed code description

I will write a detailed code explanation in another article. This time I wrote down my impressions and hardships.

Recommended Posts

Impressions of making BlackJack-cli with Ruby
Extract a part of a string with Ruby
Basics of Ruby
[Ruby] Arguments with keywords and default values of arguments
Manage the version of Ruby itself with rbenv
[Ruby / Rails] Inactivate Lint of update_all with Rubocop
I checked the number of taxis with Ruby
The story of making a reverse proxy with ProxyServlet
Getting Started with Ruby
definition of ruby method
Evolve Eevee with Ruby
The story of making dto, dao-like with java, sqlite
Notify Slack of AWS bills daily with Lambda for Ruby
Story of making a task management application with swing, java
Impressions and memories of openssl, curl, ruby, homebrew, rbenv installation
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
How to deal with different versions of rbenv and Ruby
Ruby version switching with rbenv
I tried DI with Ruby
GraphQL Client starting with Ruby
Ruby: Send email with Starttls
Format Ruby with VS Code
Integer check method with ruby
[Ruby] List of basic commands
[Ruby] problem with if statement
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
Getting Started with Ruby Modules
[ruby] Method call with argument
I tried to solve the problem of "multi-stage selection" with Ruby
Making a Cee-lo game with Ruby Final improvement after receiving a review
[Illustration] Finding the sum of coins with a recursive function [Ruby]