#!/usr/bin/env ruby
#-*- coding:utf-8 -*-
require 'io/console'
require 'csv'
def pacman
  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=2
  ex1=7
  d=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 key=="A"#↑
      
      if firld[y-1][x]=="1"
        redo
      elsif firld[y-1][x]=="0"
        firld[y][x]="2"
        y-=1
        firld[y][x]="3"
        a+=100
        if y==-1&&x==7
          y=14
        end
      elsif firld[y-1][x]=="2"
        firld[y][x]="2"
        y-=1
        firld[y][x]="3"
        if y==-1&&x==7
          y=14
        end
      end
      
    elsif key=="B"#↓
      
      if firld[y+1]==nil
        firld[y][x]="2"
        y=0
        firld[y][x]="3"
      elsif firld[y+1][x]=="1"
        redo
      elsif firld[y+1][x]=="0"
        firld[y][x]="2"
        y+=1
        firld[y][x]="3"
        a+=100
      elsif firld[y+1][x]=="2"
        firld[y][x]="2"
        y+=1
        firld[y][x]="3"
      end
      
    elsif key=="C"#→
      
      if firld[y][x+1]==nil
        firld[y][x]="2"
        x=0
        firld[y][x]="3"
      elsif firld[y][x+1]=="1"
        redo
      elsif firld[y][x+1]=="0"
        firld[y][x]="2"
        x+=1
        firld[y][x]="3"
        a+=100
      elsif firld[y][x+1]=="2"
        firld[y][x]="2"
        x+=1
        firld[y][x]="3"
      end
      
    elsif key=="D"#←
      
      if firld[y][x-1]=="1"
        redo
      elsif firld[y][x-1]=="0"
        firld[y][x]="2"
        x-=1
        firld[y][x]="3"
        a+=100
        if y==7&&x==-1
          x=14
        end
      elsif firld[y][x-1]=="2"
        firld[y][x]="2"
        x-=1
        firld[y][x]="3"
        if y==7&&x==-1
          x=14
        end
      end
    end
    makescreen(firld, stage1)
    #makescreen(firld, stage2)
    printf("score:%d",a)
    c=0
    e=[0,1,2,3]
    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
      if firld[ey-1][ex]=="3"
        c+=1
        redo
      elsif firld[ey+1]==nil
        
      elsif firld[ey+1][ex]=="3"
        c+=1
        redo
      elsif firld[ey][ex+1]=="3"
        c+=1
        redo
      elsif firld[ey][ex-1]=="3"
        c+=1
        redo
      end
      srand()
      f=rand(e.length)
      enemy=e[f]
      if enemy==0 #↑
        
        if firld[ey-1][ex]=="1"
          #壁なので進まない=何もしない
          e.delete_at(f)
          redo
        elsif firld[ey-1][ex]=="0"
          firld[ey][ex]=b
          ey-=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        elsif firld[ey-1][ex]=="2"
          firld[ey][ex]=b
          ey-=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        else
          break
        end
        
      elsif enemy==1 #↓
        
        if firld[ey+1]==nil
          firld[ey][ex]=b
          ey=0
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        elsif firld[ey+1][ex]=="1"
          #
          e.delete_at(f)
          redo
        elsif firld[ey+1][ex]=="0"
          firld[ey][ex]=b
          ey+=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        elsif firld[ey+1][ex]=="2"
          firld[ey][ex]=b
          ey+=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        else
          break
        end
        
      elsif enemy==2 #→
        
        if firld[ey][ex+1]==nil
          firld[ey][ex]=b
          ex=0
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        elsif firld[ey][ex+1]=="1"
          #
          e.delete_at(f)
          redo
        elsif firld[ey][ex+1]=="0"
          firld[ey][ex]=b
          ex+=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        elsif firld[ey][ex+1]=="2"
          firld[ey][ex]=b
          ex+=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        else
          break
        end
        
      elsif enemy==3 #←
        
        if firld[ey][ex-1]=="1"
          #
          e.delete_at(f)
          redo
        elsif firld[ey][ex-1]=="0"
          firld[ey][ex]=b
          ex-=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        elsif firld[ey][ex-1]=="2"
          firld[ey][ex]=b
          ex-=1
          b=firld[ey][ex]
          firld[ey][ex]="4"
          break
        else
          break
        end

      elsif enemy==nil
        sleep(0.5)
        firld[y][x]="5"
        makescreen(firld, stage1)
        #makescreen(firld, stage2)
        sleep(0.9)
        print("\e[5mgameover!")
        return
      end
      
    end
    makescreen(firld, stage1)
    #makescreen(firld, stage2)
    printf("score:%d\n",a)
    
    c=0
    e=[0,1,2,3]
    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
      if firld[ey1-1][ex1]=="3"
        c+=1
        redo
      elsif firld[ey1+1]==nil
        
      elsif firld[ey1+1][ex1]=="3"
        c+=1
        redo
      elsif firld[ey1][ex1+1]=="3"
        c+=1
        redo
      elsif firld[ey1][ex1-1]=="3"
        c+=1
        redo
      end
      srand()
      f=rand(e.length)
      enemy=e[f]
      if enemy==0 #↑
        
        if firld[ey1-1][ex1]=="1"
          #壁なので進まない=何もしない
          e.delete_at(f)
          redo
        elsif firld[ey1-1][ex1]=="0"
          firld[ey1][ex1]=d
          ey1-=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        elsif firld[ey1-1][ex1]=="2"
          firld[ey1][ex1]=d
          ey1-=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        else
          break
        end
        
      elsif enemy==1 #↓
        
        if firld[ey1+1]==nil
          firld[ey1][ex1]=d
          ey1=0
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        elsif firld[ey1+1][ex1]=="1"
          #
          e.delete_at(f)
          redo
        elsif firld[ey1+1][ex1]=="0"
          firld[ey1][ex1]=d
          ey1+=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        elsif firld[ey1+1][ex1]=="2"
          firld[ey1][ex1]=d
          ey1+=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        else
          break
        end
        
      elsif enemy==2 #→
        
        if firld[ey1][ex1+1]==nil
          firld[ey1][ex1]=d
          ex1=0
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        elsif firld[ey1][ex1+1]=="1"
          #
          e.delete_at(f)
          redo
        elsif firld[ey1][ex1+1]=="0"
          firld[ey1][ex1]=d
          ex1+=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        elsif firld[ey1][ex1+1]=="2"
          firld[ey1][ex1]=d
          ex1+=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        else
          break
        end
        
      elsif enemy==3 #←
        
        if firld[ey1][ex1-1]=="1"
          #
          e.delete_at(f)
          redo
        elsif firld[ey1][ex1-1]=="0"
          firld[ey1][ex1]=d
          ex1-=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        elsif firld[ey1][ex1-1]=="2"
          firld[ey1][ex1]=d
          ex1-=1
          d=firld[ey1][ex1]
          firld[ey1][ex1]="6"
          break
        else
          break
        end
      elsif enemy==nil
        sleep(0.5)
        firld[y][x]="5"
        makescreen(firld, stage1)
        #makescreen(firld, stage2)
        sleep(0.9)
        print("\e[5mgameover!\e[m")
        print("enemy==nil")
        return
      end
    end
    
    makescreen(firld, stage1)
    #makescreen(firld, stage2)
    printf("score:%d\n",a)
    if a==10000
      print("\e[5mgameclear!")
      return
    end
  end
end
pacman
