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

def pacman
  def move(firld,a,b,c,d,y,x,e,f,g,key)
    if key==a #↑
      
      if firld[y-1][x]=="1"
        return true
      elsif firld[y-1][x]=="0"
        firld[y][x]=e
        y-=1
        firld[y][x]=f
        g+=100
        if y==-1&&x==7
          y=14
        end
      elsif firld[y-1][x]=="2"
        firld[y][x]=e
        y-=1
        firld[y][x]=f
        if y==-1&&x==7
          y=14
        end
      end
      
    elsif key==b #↓
      
      if firld[y+1]==nil
        firld[y][x]=e
        y=0
        firld[y][x]=f
      elsif firld[y+1][x]=="1"
        return true
      elsif firld[y+1][x]=="0"
        firld[y][x]=e
        y+=1
        firld[y][x]=f
        g+=100
      elsif firld[y+1][x]=="2"
        firld[y][x]=e
        y+=1
        firld[y][x]=f
      end
      
    elsif key==c #→
      
      if firld[y][x+1]==nil
        firld[y][x]=e
        x=0
        firld[y][x]=f
      elsif firld[y][x+1]=="1"
        return true
      elsif firld[y][x+1]=="0"
        firld[y][x]=e
        x+=1
        firld[y][x]=f
        g+=100
      elsif firld[y][x+1]=="2"
        firld[y][x]=e
        x+=1
        firld[y][x]=f
      end
      
    elsif key==d #←
      
      if firld[y][x-1]=="1"
        return true
      elsif firld[y][x-1]=="0"
        firld[y][x]=e
        x-=1
        firld[y][x]=f
        g+=100
        if y==7&&x==-1
          x=14
        end
      elsif firld[y][x-1]=="2"
        firld[y][x]=e
        x-=1
        firld[y][x]=f
        if y==7&&x==-1
          x=14
        end
      end
    end
    return false
  end

  firld = CSV.read("course1.csv")
  stage1 = {
    "0" => "\e[37m・",
    "1" => "\e[34m@@",
    "2" => "  ",
    "3" => "\e[33m●",
    "4" => "\e[31m●",
    "5" => "\e[33m○",
    "6" => "\e[36m●"
  }
  stage2 = {
    "0" => "\e[37m・",
    "1" => "\e[34m##",
    "2" => "  ",
    "3" => "\e[33m:D",
    "4" => "\e[31m●",
    "5" => "\e[33m○",
    "6" => "\e[36m●"
  }

  def makescreen(map, code)
    print("\e[2J\e[1;1H")
    map.each do |row|
      row.each do |column|
        print code[column]
      end
      puts
    end
    print("\e[m")
  end

  a=0
  y=11
  x=7
  firld[y][x]="3"

  ey=5
  ex=7
  b=firld[ey][ex]
  firld[ey][ex]="4"

  ey1=5
  ex1=6
  c=firld[ey1][ex1]
  firld[ey1][ex1]="6"
  
  makescreen(firld, stage1)
  #makescreen(firld, stage2)
  printf("score:%d",a)
  while true

    if (key=STDIN.getch)=="\C-c"
      break
    end
    if key=="\e"&&STDIN.getch=="["
      key=STDIN.getch
    end
    if move(firld,"A","B","C","D",y,x,"2","3",a,key)
      redo
    end
    makescreen(firld, stage1)
    #makescreen(firld, stage2)
    printf("score:%d",a)

    c=0
    while true
      if c>1
        sleep(0.5)
        firld[y][x]="5"
        makescreen(firld, stage1)
        #makescreen(firld, stage2)
        sleep(0.9)
        print("\e[5mgameover!")
        return
      end
      srand()
      enemy=rand(4)
      if move(firld,0,1,2,3,ey,ex,b,"4",0,enemy)
        redo
      else
        break
      end
      c+=1
    end
    makescreen(firld, stage1)
    #makescreen(firld, stage2)
    printf("score:%d",a)
    
    if a==10000
      print("\e[5mgameclear!")
      return
    end
  end
end
pacman
