require "csv"

data = CSV.read("bun.csv")   # ← headers は使わない（Array前提）

puts "1.ランダムに答える 2.問題を作る"
u = gets.chomp.to_i

if u == 1
  row = data.sample          # row は Array
  mondai = row[0]            # 問題
  kotae  = row[1]            # 答え

  puts mondai
  print "答え: "
  user = gets.chomp

  if user == kotae
    puts "正解!"
  else
    puts "不正解"
    puts "正解は#{kotae}"
  end

elsif u == 2
  print "問題: "
  mondai = gets.chomp
  print "答え: "
  kotae = gets.chomp

  CSV.open("bun.csv", "a") do |csv|
    csv << [mondai, kotae]
  end

  puts "追加した"
end
