#!/usr/bin/env ruby
# -*- coding: utf-8 -*-



class RANDOM

  def initialize
    @@choices = {"初期設定" => ["役職分けスタート","GM決め","昼夜スタート","詳細設定","リセット","プログラムを終了"],
                 "役職追加" => ["役職を追加","参加者を追加","ホームに戻る"],
                 "リセット" => ["すべて","役職","参加者","戻る"]}
    #"役職を削除","役職を確認","参加者を確認","参加者を削除",
    @position = []
    @name = []
    @gazo = spawn "display -geometry +0+0 タイトル.jpg"
  end
  
  def question(choices,mold = "moji")
    x = 0
    @select = -1
    if mold == "hyozi"
      while x < choices.length
        printf("%s ",choices[x])
        print("\e[m")
        x += 1
      end
    else
      while x < choices.length
        printf("%s(%d) ",choices[x],x)
        print("\e[m")
        x += 1
      end
    end
    print("\n")
    while @select < 0 || @select >= choices.length   #回答の仕分け
      print("選択:")
      @select = gets.chomp
      if @select == "" || check(@select) == false
        redo
      end
      @select = @select.to_i
    end
    if mold == "moji"
      @select = choices[@select]
      return @select
    elsif mold == "kazu"
      return @select
    else
      puts("Error")
    end
  end

  def check(h)
    number = ["1","2","3","4","5","6","6","7","8","9","0"]
    x = h.split("")
    n = 0
    for i in x
      if  false == number.include?(x[n])
        return false
      end
      n += 1
    end
    return true
  end
  
  def tuika
    while true
      print("設定:")
      a = gets.chomp
      if not a == ""
        return a
      end
    end
  end

  def ninzu
    while true
      print("設定:")
      number = ["1","2","3","4","5","6","7","8","9","0"]
      n = gets.chomp
      x = n.split("")
      for i in x
        if number.include?(x)
          redo
        end
      end
      return n.to_i
    end
  end
  
  def program
    while true
      puts("ZinroGameRandomProgrm")
      question(@@choices["初期設定"])
      print("\n")
      if @select == "詳細設定"
        while true
          question(@@choices["役職追加"])
          if @select == "役職を追加"
            x = []
            puts("役職名を教えてください")
            x << tuika
            puts("人数はどうしますか?")
            x << ninzu
            @position << x
          elsif @select == "役職を削除"
            p "製作途中"
            redo
            z = @position
            a = []
            while not z == []
              a << z.shift
              b = a[-1]
              z.delete(b)
            end
            p a
            p b
            p z
          elsif @select == "参加者を追加"
            puts("参加者の名前を教えてください")
            @name << tuika
          elsif @select == "参加者を削除"
            y = @name+["戻る"]
            question(@name+["戻る"])
            @name.delete(@select)
          elsif @select == "役職を確認"
            p @position
          elsif @select == "参加者を確認"
            p @name
          elsif @select == "ホームに戻る"
            break
          end
        end
      elsif @select == "役職分けスタート"
        p @position
        p @name
        if @position == []
          puts("役職が設定されていません")
          redo
        end
        y = []
        for i in @position
          y << [i[0]]*i[1].to_i
        end
        y.flatten!
        n = @name
        n.shuffle!
        x = 0
        if  y.length > n.length
          puts("役職数が参加者人数より多いです")
          redo
        end
        
        while not y.length == @name.length
          y << "村人"
        end
        while not x >= n.length
          printf("%s:%s\n",y[x],n[x])
          x += 1
        end
      elsif @select == "リセット"
        question(@@choices["リセット"])
        if @select == "すべて"
          @position.clear
          @name.clear
        elsif @select == "役職"
          @position.clear
        elsif @select == "参加者"
          @name.clear
        end
      elsif @select == "昼夜スタート"
        puts("昼は何分?")
        hiru = ninzu
        puts("夜は何分?")
        yoru = ninzu
        puts("入力するとき ENDと入力すると終了 OKと入力すると続行 人狼と入力すると人狼の勝利 村人と入力すると村人の勝利 です")
        if "END" == gets.chomp
          redo
        end
        while true
          gazo = spawn "display -geometry +0+0 昼のターン.jpg"
          puts("昼のターンスタート")
          x = Time.new+(hiru*60)
          while Time.new < x
          end
          Process.kill(:INT,gazo)
          puts("昼のターンが終了しました")
          puts("入力してください")
          while true
            x = gets.chomp
            y = nil
            if x == "END"
              y = "終わる"
              break
            elsif x == "人狼"
              y = "人狼"
            elsif x == "村人"
              y = "村人"
            elsif x == "OK"
              break
            else
              redo
            end
          end
          if y == "終わる" || y == "人狼" || y == "村人"
            break
          end
          puts("投票と退場が完了したら入力してください")
          gazo = spawn "display -geometry +0+0 投票画面.jpg"
          while true
            x = gets.chomp
            y = nil
            if x == "END"
              y = "終わる"
              break
            elsif x == "人狼"
              y = "人狼"
            elsif x == "村人"
              y = "村人"
            elsif x == "OK"
              break
            else
              redo
            end
          end
          if y == "終わる" || y == "人狼" || y == "村人"
            break
          end
          Process.kill(:INT,gazo)
          gazo = spawn "display -geometry +0+0 夜のターン.jpg"
          puts("夜のターンスタート")
          y = Time.new+(yoru*60)
          while Time.new < y
          end
          Process.kill(:INT,gazo)
          puts("夜のターンが終了しました")
          puts("入力してください")
          while true
            x = gets.chomp
            y = nil
            if x == "END"
              y = "終わる"
              break
            elsif x == "人狼"
              y = "人狼"
            elsif x == "村人"
              y = "村人"
            elsif x == "OK"
              break
            else
              redo
            end
          end
          if y == "終わる" || y == "人狼" || y == "村人"
            break
          end
          gazo = spawn "display -geometry +0+0 襲撃.jpg"
          puts("襲撃を行ってください")
          puts("入力してください")
          while true
            x = gets.chomp
            y = nil
            if x == "END"
              y = "終わる"
              break
            elsif x == "人狼"
              y = "人狼"
            elsif x == "村人"
              y = "村人"
            elsif x == "OK"
              break
            else
              redo
            end
          end
          if y == "終わる" || y == "人狼" || y == "村人"
            break
          end
          Process.kill(:INT,gazo)
        end
        if y == "人狼"
          gazo = spawn "display -geometry +0+0 人狼陣営の勝利.jpg"
          Process.kill(:INT,gazo)
        elsif y == "村人"
          gazo = spawn "display -geometry +0+0 村人陣営の勝利.jpg"
          Process.kill(:INT,gazo)
        end
      elsif @select == "GM決め"
        if @name == []
          puts("参加者が設定されていません")
          redo
        end
        puts("今回のGMは何人ですか?(GMに選ばれた人は参加者から消去されます)")
        x = ninzu
        if x > @name.length
          puts("参加者の人数より多いです")
          redo
        end
        z = 0
        while not x == z
          y = @name.shuffle!.shift
          printf("GM:%s\n",y)
          z += 1
        end
      elsif @select == "プログラムを終了"
        Process.kill(:INT,@gazo)
        return
      end
    end
  end
end

x = RANDOM.new

x.program
