module BlackjackJp::Game

Public Class Methods

start() click to toggle source
# File lib/blackjack_jp/game.rb, line 6
def self.start
  init
  p 'ゲームスタート'
  @dealer.draw(2)
  @player.draw(2)
  puts "ディーラーの1枚目の手札は#{@dealer.hand[0]}です。"
  puts "#{@player.name}の手札は#{@player.hand}です。点数は#{get_point(@player.hand)}です。"
  player_turn
  dealer_turn
  sleep(1.5)
  duel
  sleep(1.5)
  replay
end

Private Class Methods

bursted() click to toggle source
# File lib/blackjack_jp/game.rb, line 109
def bursted
  puts "バーストしました。"
  puts @dealer_turn == false ? "#{@player.name}の負けです。" : "ディーラーの負けです。"
  exit
end
convert_face_card_to_int(hands) click to toggle source
# File lib/blackjack_jp/game.rb, line 59
def convert_face_card_to_int(hands)
  hands.each_with_index do |hand, index|
    card_number = hand[0]
    if card_number == 'J' || card_number == 'Q' || card_number == 'K'
      hands[index][0] = '10'
    end
  end
  hands
end
dealer_draw() click to toggle source
# File lib/blackjack_jp/game.rb, line 74
def dealer_draw
  if 17 > get_point(@dealer.hand)
    sleep(1.5)
    puts "ディーラーは#{@dealer.draw.last}を引きました。点数は#{get_point(@dealer.hand)}点です。"
    sleep(1.5)
    judge(get_point(@dealer.hand))
    dealer_draw
  end
end
dealer_turn() click to toggle source
# File lib/blackjack_jp/game.rb, line 41
def dealer_turn
  @dealer_turn = true
  puts 'ディーラ-のターンです。'
  puts "ディーラーの手札は#{@dealer.hand}で#{get_point(@dealer.hand)}点です。"
  judge(get_point(@dealer.hand))
  dealer_draw
  puts "ディーラーの点数は#{get_point(@dealer.hand)}です。"
end
duel() click to toggle source
# File lib/blackjack_jp/game.rb, line 84
def duel
  if get_point(@dealer.hand) > get_point(@player.hand)
    puts 'ディーラーの勝利です。'
  elsif get_point(@dealer.hand) < get_point(@player.hand)
    puts "#{@player.name}の勝利です。"
  else
    puts '引き分けです。'
  end
end
get_point(hands) click to toggle source
# File lib/blackjack_jp/game.rb, line 50
def get_point(hands)
  point = 0
  converted_hands = convert_face_card_to_int(hands)
  converted_hands.each do |hand|
    point += hand[0].to_i
  end
  point
end
init() click to toggle source
# File lib/blackjack_jp/game.rb, line 100
def init
  deck = BlackjackJp::Deck.new.deck
  @dealer = BlackjackJp::Person.new('ディーラー', deck)
  puts "あなたの名前を入力してください。"
  name = gets.chomp
  @player = BlackjackJp::Person.new(name, deck)
  @dealer_turn = false
end
judge(point) click to toggle source
# File lib/blackjack_jp/game.rb, line 69
def judge(point)
  bursted if point > 21
  point
end
player_turn() click to toggle source
# File lib/blackjack_jp/game.rb, line 25
def player_turn
  puts "1: ヒット, 2: スタンド"
  flag = gets.chomp
  if flag == '1'
    puts 'ヒットしました。'
    puts "#{@player.name}は#{@player.draw.last}を引きました。点数は#{get_point(@player.hand)}です。"
    judge(get_point(@player.hand))
    player_turn
  elsif flag == '2'
    puts 'スタンドしました。'
  else
    puts '不正な入力です'
    player_turn
  end
end
replay() click to toggle source
# File lib/blackjack_jp/game.rb, line 94
def replay
  puts 'もう一度遊びますか?'
  puts '1: 遊ぶ, その他: 終わる'
  gets.chomp == '1' ? start : 'また遊んでね!'
end