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
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
--About the class to prepare --Whether to use instance method or class method --Where should the data be retained (deck, hand, etc.)
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.
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 # . # . # .
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.
I will write a detailed code explanation in another article. This time I wrote down my impressions and hardships.