#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'curses'
include Curses
init_screen
cbreak
noecho

waza = ["孫悟空","かめはめ波","元気玉","ベジータ","ギャリック砲","天津飯","どどん波","クリリン","気円斬","ヤムチャ","狼牙風風拳","ピッコロ","魔貫光殺砲","餃子","さよなら天さん","亀仙人","魔封波","如意棒","ウーロン","ギャルのパンティおくれ"]
rwaza = ["sonngokuu","kamehameha","gennkidama","bezi-ta","gyarikkuhou","tennsinnhann","dodonnpa","kuririnn","kiennzann","yamutya","rougahuuhuukenn","pikkoro","makannkousappou","tyaozu","sayonaratennsann","kamesennninn","mahuuba","nyoibou","u-ron","gyarunopannthiokure"]
vs = 0
ok = 0
bad = 0
point = 0

init_screen
start_color
init_pair(0, COLOR_BLACK, COLOR_WHITE)
init_pair(1, COLOR_RED, COLOR_BLACK)
init_pair(2, COLOR_RED, COLOR_WHITE)

def typ(mi,mvs,mwaza,mrwaza)
  w = stdscr.subwin(10, 40, 8, 12)
  w.attron(color_pair(2)) do
    w.box("|"[0], "+"[0])
  end
  w.setpos(2, 2)
  w.addstr(sprintf("VS %d",mvs+1))
  w.attron(A_BOLD) do
    w.setpos(2, 10)
    w.addstr(sprintf("%s",mwaza[mi]))
    w.setpos(3, 10)
    w.addstr(sprintf("%s",mrwaza[mi]))
  end
  w.setpos(5, 10)
  mat = w.getstr
  w.clear
  return mat
end

def title
  setpos(1,24)
  attron(color_pair(1)) do
    attron(A_BOLD) do
      addstr("ドラゴンタイピング！！")
    end
  end
end

title

begin
  setpos(3,18)
  addstr("これから単語が１つずつ出てくっぞ！")
  setpos(4,14)
  addstr("入力は全部”ローマ字”だから気をつけろよ！※ん→nn")
  setpos(5,3)
  addstr("しっかり打ち込めたら＜エンターキー＞っちゅうのを押して攻撃してくれ！")
  setpos(6,13)
  addstr("＜やりてーモード番号＞を押してスタートだ！")
  while true
    setpos(8,10)
    addstr("１：10問必勝！採点モード　２：限界を超えろ！無限モード -> ")
    start = getch
    if start == "1" || start == "2"
      break
    else
      setpos(9,2)
      addstr("１か２を入力してくれ！")
    end
  end
  refresh
  clear

  nocbreak
  echo
  title
  if start == "1"
    setpos(3,22)
    addstr("~10問必勝！採点モード~")
    refresh
    while vs < 10
      i = rand(19)
      at = typ(i,vs,waza,rwaza)
      vs += 1
      if at == rwaza[i]
        ok += 1
        setpos(10, 55)
        addstr(" すげーぞ！")
        point += 10
      else
        bad += 1
        setpos(10,55)
        addstr("ヤベーぞ...")
      end
      setpos(11,57)
      addstr(sprintf("%3d勝　%3d敗",ok,bad))
      refresh
    end

    clear

    attron(A_BOLD) do
      if ok >= 9
        setpos(3,37)
        addstr(sprintf("%d点！！！",point))
        setpos(6,16)
        addstr("オメーすげーやつだな！おら驚れーたぞ！")
      elsif ok >= 5
        setpos(3,37)
        addstr(sprintf("%d点！！",point))
        setpos(6,8)
        addstr("惜しかったなー、まだまだ上手くなれると思うとおらワクワクすっぞ！")
      elsif ok >= 2
        setpos(3,37)
        addstr(sprintf("%d点！",point))
        setpos(6,16)
        addstr("おらと一緒に修行、いっちょやってみっか！")
      else
        setpos(3,37)
        addstr(sprintf("%d点",point))
        setpos(6,16)
        addstr("ふんっ！口ほどにもない奴め")
      end
    end

  elsif start == "2"
    setpos(3,19)
    addstr("~限界を超えろ！無限モード~")
    setpos(4,20)
    addstr("※owariと入力すると終了")
    refresh
    while true
      i = rand(19)
      at = typ(i,vs,waza,rwaza)
      vs += 1
      if at == rwaza[i]
        ok += 1
        setpos(10, 55)
        addstr(" すげーぞ！")
      elsif at == "owari"
        break
      else
        bad += 1
        setpos(10,55)
        addstr("ヤベーぞ...")
      end
      setpos(11,57)
      addstr(sprintf("%3d勝　%3d敗",ok,bad))
      refresh
    end

    clear

    setpos(3,20)
    addstr("今回の結果が出たぞ！")
    setpos(5,25)
    addstr(sprintf("倒した数：%3d / %3d",ok,vs-1))
    rate = ok.to_f / vs.to_f * 100
    setpos(7,25)
    addstr(sprintf("正答率：%5.2f %",rate))
  end
  setpos(9,16)
  addstr("＜オメーの好きなキー＞を押して終わってくれ！")
  en = getch
  refresh
ensure
  close_screen
end
