LoginSignup
0

More than 1 year has passed since last update.

【Ruby勉強中】チンチロをRubyで作ってみた。(ソシー)

Last updated at Posted at 2023-01-15

チンチロ作ってみた

Ruby初心者です。
練習がてら、霜降り粗品さんがYouTubeでよくやっている’チンチロ’というゲームを作ってみました。(単位はソシー)
※ションベンという役はなし。

めっちゃ時間かかっちゃったな。。。

tintiro.rb

tintiro.rb
# チンチロ

# ●ルール
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ゲームは2人以上で行われ、まずは親を決める。親以外のメンバーは子になる
# 子が賭け金を決めたら、親がサイコロを振って勝負する役(数)を決める。3回まで振ることができるが、役が出たらその役で勝負
# 子が一人ずつサイコロを振る。親より強い役が出たら子の勝ち。同じなら親の勝ち。親より弱いのが出たら子の負け。親に勝ったら子が賭けた金額を親からもらい、負けたら親に払う。
# 子全員との勝負が終わったら親の交代で、左隣の人に親の権利が移る。
# これを繰り返して参加者全員に親が回ったら、一回のゲームが終了。
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# ● 役(強い順)
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# 1.ピンゾロ : 111                        賭け金の5倍貰える
# 2.アラシ.  :  222,333,444,555,666                 賭け金の3倍貰える
# 3.シゴロ.  :  456                         賭け金の2倍貰える
# 4.出目    :  同じ数字2つ+別の数字 ex) 116 → 出目6 551 → 出目1  賭け金の1倍貰える
# 5.目ナシ.  :  他の役以外                     賭け金の1倍払う
# 6.ヒフミ.  :  123                         賭け金の2倍払う
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
require "./user.rb"

DELIMITER = '----------------------------------------'
DELIMITER2 = '★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★'

role = ['親', '子']
role.shuffle!
tintiro = User.new(role)

puts DELIMITER2
puts '★             チンチロ                  ★'
puts DELIMITER2
sleep(3)

#所持金表示
tintiro.info_possession_meney

play_count = 1
while play_count <= 2
  #親子決め、掛け金決め
  tintiro.play_game
  
  # '★☆★☆★☆ 子のターン ★☆★☆★☆'
  tintiro.play_child
  
  # '★☆★☆★☆ 親のターン ★☆★☆★☆'
  tintiro.play_parent
  
  #勝敗判定
  tintiro.determine_victory_or_defeat
  
  # #賞金計算
  tintiro.calc_award
  
  #所持金表示
  tintiro.info_possession_meney
  
  if play_count == 1
    puts '親子を交換します。'
    puts DELIMITER
    sleep(2)
  else
    puts 'ゲームを終了します。'
    puts DELIMITER
    break
  end
  #親子入れ替え、掛け金初期化
  tintiro.reset_user
  
  play_count += 1
end

user.rb

本当にこのクラス定義で良いものか分からない。。

user.rb
require "./role.rb"

DEFAULT_MONEY = 50000

class User
  
  def initialize(role)
    @player = Role.new(role[0], DEFAULT_MONEY, 0, 'あなた')
    @com = Role.new(role[1], DEFAULT_MONEY, 0, '相手')
    @users = [@player, @com]
  end
  
    #所持金を表示する
  def info_possession_meney
    puts DELIMITER
    puts '<所持金>'
    puts "#{@com.name}  : #{@com.possession_money}ソシー"
    puts "#{@player.name}#{@player.possession_money}ソシー"
    puts DELIMITER
  end
  
  #親子決め、掛け金決め
  def play_game
    puts "#{@player.name}は「#{@player.role}」です。"
    if @player.role == '子'
      puts '賭け金を決めてください(ソシー)'
      @child_bet_money = gets.to_i
      puts "#{@player.name}#{@child_bet_money}ソシー賭けました"
    else
      @child_bet_money = [3000, 5000, 8000, 10000, 20000].sample
      puts "#{@com.name}は、#{@child_bet_money}ソシー賭けました"
    end
    puts DELIMITER
  end
  
  # 子ターン
  def play_child
    @users.each do |user|
      if user.role == '子'
        user.bet_money = @child_bet_money
        user.roll_dice
        user.output_result
        @child = user
      end
    end
  end
  
  # 親ターン
  def play_parent
    @users.each do |user|
      if user.role == '親'
        user.bet_money = @child_bet_money #負けた際の計算に使用する
        user.roll_dice
        user.output_result
        @parent = user
      end
    end
  end
  
  #勝敗判定
  def determine_victory_or_defeat
    if @parent.hand[1] == 4 && @child.hand[1] == 4
      if @parent.hand[2] >= @child.hand[2]
        @winner = @parent
        @loser = @child
      else
        @winner = @child
        @loser = @parent
      end
    elsif @parent.hand[1] <= @child.hand[1] 
      @winner = @parent
      @loser = @child
    else
      @winner = @child
      @loser = @parent
    end
  end
  
  #賞金計算
  def calc_award
    puts DELIMITER
    puts "#{@winner.role}(#{@winner.name})の勝ち!"
      
    case @winner.hand[1]
      when 1 #ピンゾロ
        award_money = @loser.bet_money * 5
      when 2 #アラシ
        award_money = @loser.bet_money * 3
      when 3 #シゴロ
        award_money = @loser.bet_money * 2
      else #目アリ/目ナシ
        award_money = @loser.bet_money * 1
    end
    
    award_money *= 2 if @loser.hand[1] == 6 #負けた方がヒフミの場合、賞金2倍
    puts "#{@winner.role}(#{@winner.name})が、#{award_money}ソシー獲得しました!"
    @winner.possession_money += award_money
    @loser.possession_money -= award_money
  end
  
  #親子入れ替え、賭け金/役を初期化
  def reset_user
    @player.reset
    @com.reset
    @player.role, @com.role = @com.role, @player.role #親子入れ替え
  end
  
end

role.rb

うまく?できてる?

role.rb
class Role
  attr_accessor :role , :possession_money , :bet_money
  attr_reader :hand , :name
  
  def initialize(role, possession_money, bet_money, name)
    @role = role
    @possession_money = possession_money
    @bet_money = bet_money
    @name = name
    @hand = ['', 5, 0]
  end
  
  #サイコロを振る
  def roll_dice
    sleep(1)
    puts "★☆★☆★☆ #{@role}(#{@name})のターン ★☆★☆★☆"

    play_count = 1 

    while (play_count <= 3) && @hand[1] == 5
      dices = Array.new(3).map{rand(1..6)}
      puts "● #{play_count}投目"
      dices.each do |dice|
        print "#{dice} "
        sleep(1)
      end
      print "\n"
      @hand = get_hand(dices)
      play_count += 1
    end
  end
  
  #役判定
  def get_hand(dices)
    if (dices.sum >= 15) && (dices.include?(5)) 
      @hand = ['シゴロ', 3]
    elsif (dices[0] == dices[1]) && (dices[1] == dices[2])
      @hand = ['アラシ', 2]
      if dices.sum == 3 
        @hand = ['ピンゾロ', 1]
      end
    elsif dices.sum == 6
      @hand = ['ヒフミ', 6]
    else
      @hand = ['目ナシ', 5]
    end
  
      #出目判定
    1.upto(6){ |n|
      if dices.select{ |m| m == n}.size == 2
        deme_number = dices.reject{ |k| k == n}
        @hand = ['出目', 4, deme_number[0]]
        break    
      end
    } 
  
    if @hand[1] == 5  
      puts '目ナシ...'
    end
    @hand
  end

  #役を出力する
  def output_result
    if @hand[1] == 4
      puts "#{@role}の結果 : #{@hand[0]}(#{@hand[2]})"
    else
      puts "#{@role}の結果 : #{@hand[0]}"
    end
  end
  
  def reset
    @bet_money = 0
    @hand = ['', 5, 0]
  end
end

感想

初心者ながら、なにかひとつを一から形でできた経験は貴重だと思ってる!!(ポジティブ)
この調子でまたなにか作りたい。

チンチロを教えてくれた粗品様に感謝。

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