0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Blackjackgame

Last updated at Posted at 2023-04-22
blackjack.rb
   class Card
  attr_reader :rank, :suit

  def initialize(rank, suit)
    @rank = rank
    @suit = suit
  end

  def score
    case rank
    when 2..10
      rank
    when 'J', 'Q', 'K'
      10
    else
      11
    end
  end

  def to_s
    "#{suit}#{rank}"
  end
end

class Deck
  SUITS = ['ハート', 'ダイヤ', 'スペード', 'クラブ']
  RANKS = (1..13).to_a

  def initialize
    @cards = SUITS.product(RANKS).map { |suit, rank| Card.new(rank, suit) }
    shuffle
  end

  def shuffle
    @cards.shuffle!
  end

  def draw
    @cards.pop
  end
end

class Hand
  def initialize
    @cards = []
  end

  def cards
    @cards
  end

  def add_card(card)
    @cards << card
  end

  def score
    total = @cards.sum(&:score)
    aces = @cards.select { |card| card.rank == 'A' }.count
    aces.times { total -= 10 if total > 21 }
    total
  end

  def to_s
    @cards.map(&:to_s).join(', ')
  end
end

class Player
  attr_reader :hand

  def initialize
    @hand = Hand.new
  end

  def hit(card)
    hand.add_card(card)
  end
end

class Dealer < Player
  def play_turn(deck)
    hit(deck.draw) while hand.score < 17
  end
end

class Game
  def initialize
    @deck = Deck.new
    @player = Player.new
    @dealer = Dealer.new
  end

  def start
    puts "ブラックジャックを開始します。"
    2.times { @player.hit(@deck.draw) }
    2.times { @dealer.hit(@deck.draw) }

   
    show_initial_cards

    player_turn
    return if busted?(@player.hand)

    dealer_turn
    determine_winner
  end

  private

  def show_initial_cards
    puts "あなたの引いたカードは#{@player.hand.cards[0]}です。"
    puts "あなたの引いたカードは#{@player.hand.cards[1]}です。"
    puts "ディーラーの引いたカードは#{@dealer.hand.cards[0]}です。"
    puts "ディーラーの引いた2枚目のカードはわかりません。"
  end

  def player_turn
    loop do
      puts "あなたの現在の得点は#{@player.hand.score}です。カードを引きますか?(Y/N)"
      answer = gets.chomp.upcase

      if answer == 'Y'
        card = @deck.draw
        puts "あなたの引いたカードは#{card}です。"
        @player.hit(card)

        if busted?(@player.hand)
          puts "あなたの現在の得点は#{@player.hand.score}です。"
          puts "あなたの負けです。"
          break
        end
      else
        break
      end
    end
  end

  def dealer_turn
    @dealer.play_turn(@deck)
  end

  def determine_winner
    puts "ディーラーの引いた2枚目のカードは#{@dealer.hand.cards[1]}でした。"
    puts "ディーラーの現在の得点は#{@dealer.hand.score}です。"
    puts "あなたの得点は#{@player.hand.score}です。"

    if busted?(@dealer.hand) || @player.hand.score > @dealer.hand.score
      puts "あなたの勝ちです!"
    elsif @player.hand.score == @dealer.hand.score
      puts "引き分けです。"
    else
      puts "あなたの負けです。"
    end

    puts "ブラックジャックを終了します。"
  end

  def busted?(hand)
    hand.score > 21
  end
end

game = Game.new
game.start
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?