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

blackjack_ruby_gif.gif

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]
    end

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

** Instance method ** Example

class PlayerBase

  def create_hand(deck)
    deck.pop(2)
  end
# .
# .
# .

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 / Rails] Inactivate Lint of update_all with Rubocop
I checked the number of taxis with Ruby
Install Ruby 3.0.0 with asdf
Build ruby debug environment with VS Code of Windows 10
Getting Started with Ruby
definition of ruby method
11th, Classification with Ruby
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
Basic methods of Ruby hashes
Ruby version switching with rbenv
Basic methods of Ruby arrays
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
[Ruby] Various types of each
Ruby: Send email with Starttls
Leap year judgment with Ruby
Format Ruby with VS Code
Integer check method with ruby
Ruby Learning # 8 Working With String
[Ruby] List of basic commands
[Ruby] problem with if statement
Judgment of fractions in Ruby
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
[Ruby] REPL-driven development with pry
Getting Started with Ruby Modules
[ruby] Method call with argument
Review of Ruby basic grammar
The basics of the process of making a call with an Android app
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]