#usr/bin/env ruby
# -*- coding: utf-8 -*-
require `csv`
require 'io/console'
require 'io/console'

puts("虫見分けゲーム!")
puts("上から虫の写真が降ってきます")
puts("矢印キー(↓←→)で写真を移動し、虫が絶滅危惧種か外来種かどうか見分けてください")
puts("正しい箱に入れられたら+5ptです。")
puts("実行するウィンドウはなるべく大きくしておこう")
puts("準備はいい?(Enter)")
puts(">>")
gets
def byouga(field,map,hold)
  
end

def fall(field,movmino,y1,x1)
  if field[y1+1]==nil||field[y2+1]==nil||field[y3+1]==nil||field[y4+1]==nil
    return true
  elsif (field[y1+1][x1]=="0"||xys.include?(xy1+10))&&(field[y2+1][x2]=="0"||xys.include?(xy2+10))&&(field[y3+1][x3]=="0"||xys.include?(xy3+10))&&(field[y4+1][x4]=="0"||xys.include?(xy4+10))
    return false
  else
    return true
  end
end
while true
  pic_=CSV.read()
  while true
    begin
      print
      Timeout.timeout(1){
        key=STDIN.getch
        if key=="\e"&&STDIN.getch=="["
          key=STDIN.getch
          if key=="D" #←(左移動)
            if x1<1||x2<1||x3<1||x4<1
            elsif (field[y1][x1-1]=="0"||xys.include?(xy1-1))&&(field[y2][x2-1]=="0"||xys.include?(xy2-1))&&(field[y3][x3-1]=="0"||xys.include?(xy3-1))&&(field[y4][x4-1]=="0"||xys.include?(xy4-1))&&x1>=0&&x2>=0&&x3>=0&&x4>=0
              field[y1][x1]="0"
              field[y2][x2]="0"
              field[y3][x3]="0"
              field[y4][x4]="0"
              x1-=1
              x2-=1
              x3-=1
              x4-=1
            end
          elsif key=="B" #↓(下移動)
            if field[y1+1]==nil||field[y2+1]==nil||field[y3+1]==nil||field[y4+1]==nil
              
            elsif (field[y1+1][x1]=="0"||xys.include?(xy1+10))&&(field[y2+1][x2]=="0"||xys.include?(xy2+10))&&(field[y3+1][x3]=="0"||xys.include?(xy3+10))&&(field[y4+1][x4]=="0"||xys.include?(xy4+10))
              field[y1][x1]="0"
              field[y2][x2]="0"
              field[y3][x3]="0"
              field[y4][x4]="0"
              y1+=1
              y2+=1
              y3+=1
              y4+=1
            end
          elsif key=="C" #→(右移動)
            if x1==9||x2==9||x3==9||x4==9
            elsif (field[y1][x1+1]=="0"||xys.include?(xy1+1))&&(field[y2][x2+1]=="0"||xys.include?(xy2+1))&&(field[y3][x3+1]=="0"||xys.include?(xy3+1))&&(field[y4][x4+1]=="0"||xys.include?(xy4+1))
              field[y1][x1]="0"
              field[y2][x2]="0"
              field[y3][x3]="0"
              field[y4][x4]="0"
              x1+=1
              x2+=1
              x3+=1
              x4+=1
            end
          end
        end 
      }
    rescue Timeout::Error
      if fall(field,movmino,y1,x1,y2,x2,y3,x3,y4,x4)
        mino.delete(mino_.to_i)
        break #ここのbreakは次のミノに移る
      else        
        field[y1][x1]="0"
        field[y2][x2]="0"
        field[y3][x3]="0"
        field[y4][x4]="0"
        y1+=1
        y2+=1
        y3+=1
        y4+=1
        field[y1][x1]=movmino
        field[y2][x2]=movmino
        field[y3][x3]=movmino
        field[y4][x4]=movmino
      end
    end
  end
  field.length.times do |i|
    field_a=field[i]
    if !(field_a.include?("0"))
      field[i]=["0","0","0","0","0","0","0","0","0","0"]
      byouga(field,map,hold)
      sleep(0.2)
      k=i
      while k>0
        field[k]=field[k-1]
        k-=1
      end
      field[0]=["0","0","0","0","0","0","0","0","0","0"]
    end
  end
  
end
