#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
tairu["1","2","3","4","5","6","7","8"]
8.times do
  puts tairu.shuffle
end

require 'curses'
include Curses

init_screen
cbreak
noecho
begin
  def start(where)
    if where == hl
      system("display -geometry +0+0 8puzzle1big.png&")
    end
    if where == hc
      system("display -geometry +150+0 8puzzle2big.png&")
    end
    if where == hr
      system("display -geometry +300+0 8puzzle3big.png&")
    end
    if where == cl
      system("display -geometry +0+150 8puzzle4big.png&")
    end
    if where == cc
      system("display -geometry +150+150 8puzzle5big.png&")
    end
    if where == cr
      system("display -geometry +300+150 8puzzle6big.png&")
    end
    if where == ll
      system("display -geometry +0+300 8puzzle7big.png&")
    end
    if where == lc
      system("display -geometry +150+300 8puzzle8big.png&")
    end
    if where == lr
      system("display -geometry +300+300 8puzzle8big.png&")
    end
  end
  puts "3✕3のスライドパズル。\n動かしたいタイル番号を入力して動かしてね。\nタイルの数字が左上から右に向かって右下まで順に1〜9になればクリア。"
  if puzzle == 0
    if
      while air == 1
        idou = gets.to_i
        if idou == 2
          hl += hc
          hc -= hc
          air = 2
          break
        elsif idou == 4
          hl += cl
          cl -= cl
          air = 4
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 2   #空白が2のとき
      printf("-------------\n| %d |   | %d |\n-------------\n| %d | %d | %d |\n-------------\n| %d | %d | %d |\n-------------\n",hl,hr,cl,cc,cr,ll,lc,lr)
      while air == 2
        idou = gets.to_i
        if idou == 1
          hc += hl
          hl -= hl
          air = 1
          break
        elsif idou == 3
          hc += hr
          hr -= hr
          air = 3
          break
        elsif idou == 5
          hc += cc
          cc -= cc
          air = 5
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 3   #空白が3のとき
      printf("-------------\n| %d | %d |   |\n-------------\n| %d | %d | %d |\n-------------\n| %d | %d | %d |\n-------------\n",hl,hc,cl,cc,cr,ll,lc,lr)
      while air == 3
        idou = gets.to_i
        if idou == 2
          hr += hc
          hc -= hc
          air = 2
          break
        elsif idou == 6
          hr += cr
          cr -= cr
          air = 6
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 4   #空白が4のとき
      printf("-------------\n| %d | %d | %d |\n-------------\n|   | %d | %d |\n-------------\n| %d | %d | %d |\n-------------\n",hl,hc,hr,cc,cr,ll,lc,lr)
      while air == 4
        idou = gets.to_i
        if idou == 1
          cl += hl
          hl -= hl
          air = 1
          break
        elsif idou == 5
          cl += cc
          cc -= cc
          air = 5
          break
        elsif idou == 7
          cl += ll
          ll -= ll
          air = 7
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 5   #空白が5のとき
      printf("-------------\n| %d | %d | %d |\n-------------\n| %d |   | %d |\n-------------\n| %d | %d | %d |\n-------------\n",hl,hc,hr,cl,cr,ll,lc,lr)
      while air == 5
        idou = gets.to_i
        if idou == 2
          cc += hc
          hc -= hc
          air = 2
          break
        elsif idou == 4
          cc += cl
          cl -= cl
          air = 4
          break
        elsif idou == 6
          cc += cr
          cr -= cr
          air = 6
          break
        elsif idou == 8
          cc += lc
          lc -= lc
          air = 8
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 6   #空白が6のとき
      printf("-------------\n| %d | %d | %d |\n-------------\n| %d | %d |   |\n-------------\n| %d | %d | %d |\n-------------\n",hl,hc,hr,cl,cc,ll,lc,lr)
      while air == 6
        idou = gets.to_i
        if idou == 3
          cr += hr
          hr -= hr
          air = 3
          break
        elsif idou == 5
          cr += cc
          cc -= cc
          air = 5
          break
        elsif idou == 9
          cr += lr
          lr -= lr
          air = 9
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 7   #空白が7のとき
      printf("-------------\n| %d | %d | %d |\n-------------\n| %d | %d | %d |\n-------------\n|   | %d | %d |\n-------------\n",hl,hc,hr,cl,cc,cr,lc,lr)
      while air == 7
        idou = gets.to_i
        if idou == 4
          ll += cl
          cl -= cl
          air = 4
          break
        elsif idou == 8
          ll += lc
          lc -= lc
          air = 8
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 8   #空白が8のとき
      printf("-------------\n| %d | %d | %d |\n-------------\n| %d | %d | %d |\n-------------\n| %d |   | %d |\n-------------\n",hl,hc,hr,cl,cc,cr,ll,lr)
      while air == 8
        idou = gets.to_i
        if idou == 5
          lc += cc
          cc -= cc
          air = 5
          break
        elsif idou == 7
          lc += ll
          ll -= ll
          air = 7
          break
        elsif idou == 9
          lc += lr
          lr -= lr
          air = 9
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    elsif air == 9   #空白が9のとき
      printf("-------------\n| %d | %d | %d |\n-------------\n| %d | %d | %d |\n-------------\n| %d | %d |   |\n-------------\n",hl,hc,hr,cl,cc,cr,ll,lc)
      while air == 9
        idou = gets.to_i
        if idou == 6
          lr += cr
          cr -= cr
          air = 6
          break
        elsif idou == 8
          lr += lc
          lc -= lc
          air = 8
          break
        else
          puts "それは動かせないよ。"
          redo
        end
      end
    end
    if hl == 1
      if hc == 2
        if hr == 3
          if cl == 4
            if cc == 5
              if cr == 6
                if ll == 7
                  if lc == 8
                    if lr == 0
                      
                      def stop(x)
                        sleep(x)
                      end
                      
                      puts"-------------\n| 1 | 2 | 3 |\n-------------\n| 4 | 5 | 6 |\n-------------\n| 7 | 8 |   |\n-------------\nクリア!!!おめでとう!!!"
                      stop(5)
                      puts "...もっかいやる?"
                      stop(1)
                      puts "いや、やらんわな。おつかれさま。\nCtrlとCを一緒に押してゲームを終了してね。"
                      stop(3000)
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end
