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

#佐藤隆之助です。
#自分は計算の速度をもっと早くしたいと思いこの学習用プログラム(名称は未定)を思いつきました。
#今の状態だといくつか未定というものがありますが、そこに機能を追加したいと思っています。
#(機能には豆知識クイズやRubyのコマンド(printなど)が出てくるタイピングなどを入れようと思っています。)
#計算は、四則演算をすべてできるようにし、計算で出てくる数の範囲を指定できるようにしたいと思っています。


# # # プログラム # # #
require"curses"
require"csv"

include Curses

def delete
  dy = 0
  while not dy == cols
    dx = 0
    while not dx == lines
      setpos(dy,dx)
      delch
      dx += 0.03125 / 2
    end
    dy += 1
  end
  refresh
end

def loding(ld,lu)
  printf("\e[0;1m")
  delete
  curs_set(0)
  lx = 0
  srand()
  ly = rand(ld..lu)
  while not lx == ly
    setpos(lines - 1,cols - 13)
    addstr"読み込み中"
    refresh
    ldot = 0
    lx += 1
    sleep(0.6)
    while not ldot == 3
      addch"."
      refresh
      ldot += 1
      sleep(0.5)
    end
    delete
    refresh
  end    
end

def cprint(cc,cy,cx,ct)
  setpos(cy,cx)
  printf("\e[m%s",cc)
  addstr(sprintf("%s",ct))
  refresh
end

def choice(cu,cd,cn)
  x = cols / 2 + cn
  y = 4
  sentaku = 1
  while 1
    printf("\e[1;37m")
    setpos(y,x)
    addstr(">")
    refresh
    input = getch
    setpos(y,x)
    addch(" ")
    refresh
    case input
    when Curses::KEY_UP
      if y != cu
        y -= 2
        sentaku -= 1
      end
    when Curses::KEY_DOWN
      if y != cd
        y += 2
        sentaku += 1
      end
    when Curses::KEY_RIGHT
      break
    end
  end
  delete
  return sentaku
end

sentaku = 0
init_screen
begin
  while 1
    delete
    curs_set(0)
    noecho
    stdscr.keypad(true)
    cprint("\e[1;21;33m",1,cols / 2 -5,"学習ゲーム")
    cprint("\e[91m",4,cols / 2 -2,"未定")
    cprint("\e[94m",6,cols / 2 -2,"計算")
    cprint("\e[92m",8,cols / 2 -2,"未定")
    cprint("\e[m",10,cols / 2 -2,"終了")
    sentaku = choice(4,10,-7)
    if sentaku == 4
      break
    end
    loding(1,2)
    if sentaku == 1
      while 1
        delete
        refresh
        cprint("\e[1;4;31m",1,cols / 2 -5,">-=未定=-<")
        cprint("\e[33m",4,cols / 2 -2,"未定")
        cprint("\e[33m",6,cols / 2 -2,"未定")
        cprint("\e[33m",8,cols / 2 -2,"未定")
        cprint("\e[m",10,cols / 2 -2,"戻る")
        sentaku = choice(4,10,-4)
        if sentaku == 4
          break
        end        
        cprint("\e[1;37m",lines / 2,cols / 2 -14,"どれかのキーを押してスタート")
        getch
        delete
        if sentaku == 1
        elsif sentaku == 2
        elsif sentaku == 3
        end
      end
      loding(1,2)
      redo
    elsif sentaku == 2
      while 1
        delete
        cprint("\e[1;4;34m",1,cols / 2 -5,">-=計算=-<")
        cprint("\e[34m",4,cols / 2 -7,"足し算・引き算")
        cprint("\e[34m",6,cols / 2 -7,"掛け算・割り算")
        cprint("\e[34m",8,cols / 2 -2,"設定")
        cprint("\e[m",10,cols / 2 -2,"戻る")
        sentaku = choice(4,10,-9)
        if sentaku == 5
          redo
        elsif sentaku == 6
          break
        end 
        cprint("\e[1;37m",lines / 2,cols / 2 -14,"どれかのキーを押してスタート")
        getch
        delete
        keisan_kaisi = Time.now
        keisan_mondai = 20
        keisan_toketa = 0
        keisan_tokenai = 0
        while keisan_mondai != keisan_toketa
          cprint("\e[94m",1,1,sprintf("当たった問題数 %-2s問",keisan_toketa))
          if sentaku == 1
            srand()
            keisan_1 = rand(1..20)
            keisan_2 = rand(1..20)
            keisan_type = rand(1..2)
            if keisan_type = 1
              keisan_ans = keisan_1 + keisan_2
            elsif keisan_type = 2
              keisan_ans = keisan_1 - keisan_2
            end
            cprint("\e[1m",lines / 2 - 1,cols / 2 - 3.5,sprintf("%2d + %2d",keisan_1,keisan_2))
          elsif sentaku == 2
          end
          setpos(lines / 2 + 1,cols / 2 - 1)
          echo
          if keisan_ans.to_s.length == 2
            player_ans1 = getch
            player_ans2 = getch
            if keisan_ans.to_s[0] == player_ans1 && keisan_ans.to_s[1] == player_ans2
              keisan_toketa += 1
            else
              keisan_tokenai += 1
            end
          else
            player_ans1 == getch
            if keisan_ans.to_s[0] == player_ans1
              keisan_toketa += 1
            else
              keisan_tokenai += 1
            end
          end
          delete
        end
      end
      loding(1,2)
      redo
    elsif sentaku == 3
      while 1
        delete
        cprint("\e[1;4;32m",1,cols / 2 - 5,">-=未定=-<")
        cprint("\e[m",4,cols / 2 -2,"戻る")
        sentaku = choice(4,4,-4)
        if sentaku == 1
          break
        end
      end
      loding(1,2)
    end
  end
  delete
  ensure
  close_screen
end
