#!/usr/env/bin ruby
# -*- coding:utf-8 -*-
hyou=[["  ","  ","  "],
      ["  ","  ","  "],
      ["  ","  ","  "]]
def cp(hyou,cpmb)
  while true
    srand()
    aa=rand(3)
    bb=rand(3)
    if hyou[aa][bb]=="  "
      hyou[aa][bb]=cpmb
      break
    end
  end
end
def pl(hyou,plmb)
  puts("↑を見て「x y」で入力してください。")
  while true
    x,y=gets.split.map(&:to_i)
    case hyou[x][y]
    when "  "
      hyou[x][y]=plmb
      break
    when "○"||"× "
      print("そこには書けないよ")
      sleep(0.5)
      print("\e[2K")
    when nil
      print("そこには書けないよ")
      sleep(0.5)
      print("\e[2K")
    end
  end
end
def hyouzi(hyou)
  puts("  ||0 |1 |2 |")
  puts("==||========|")
  puts(" 0||#{hyou[0][0]}|#{hyou[0][1]}|#{hyou[0][2]}|")
  puts("--||--------|")
  puts(" 1||#{hyou[1][0]}|#{hyou[1][1]}|#{hyou[1][2]}|")
  puts("--||--------|")
  puts(" 2||#{hyou[2][0]}|#{hyou[2][1]}|#{hyou[2][2]}|")
  puts("-------------")
end
puts("マルバツゲームです。")
srand()
a=["僕","あなた"]
mb=a[rand(2)]
puts("%sが先攻で,○です。"mb)
z=0
if mb="僕"
  plmb="○"
  cpmb="× "
  while z<8
    hyouzi(hyou)
    pl(hyou,plmb)
    hyouzi(hyou)
    sleep(0.3)
    cp(hyou,plmb)
    hyouzi(hyou)
    sleep(0.3)
  end
  hyouzi(hyou)
  pl(hyou,plmb)
  hyouzi(hyou)
else
  cpmb="○"
  plmb="× "
end
