Newer
Older
Ruby / marubatu.rb
#!/usr/bin/env ruby
# -*- coding: utf-8

iti_=[0,1,2,3,4,5,6,7,8]
hyou=["  ","  ","  ","  ","  ","  ","  ","  ","  "]

def zibun(hyou,iti_)
  while true
    puts("あなたは○です。")
    puts("下の表を見て対応する数字で座標を指定してください。")
    puts("012","345","678")
    iti=gets.to_i
    if iti<9
      iti_[iti]= ""
      hyou[iti]= "○"
      break
    else
      puts("そんな座標はないよ。8以下の数字で指定してね。")
    end
  end
  end

def aite(hyou,iti_)
  puts("僕のターンですね")
  d=0
  while d<1
    srand()
    a=rand(8)
    c=0
    while c<9
      if a==iti_[c]
        hyou[a]="×"
        iti_[a]=""
        d+=1
        break
      end
      c+=1
    end
  end
end

def hyouzi(hyou)
  print("--------\n")
  print("|",hyou[0],hyou[1],hyou[2],"|","\n")
  print("|",hyou[3],hyou[4],hyou[5],"|","\n")
  print("|",hyou[6],hyou[7],hyou[8],"|","\n")
  print("--------\n")
end

puts("マルバツゲーム!")
srand()
b=rand(2)
if b==0
  puts("あなたが先攻です。")
  e=0
  while ((hyou[0]==hyou[1])&&(hyou[1]==hyou[2]))||((hyou[3]==hyou[4])&&(hyou[4]==hyou[5]))||((hyou[6]==hyou[7])&&(hyou[7]==hyou[8]))||((hyou[0]==hyou[3])&&(hyou[3]==hyou[6]))||((hyou[1]=hyou[4])&&(hyou[4]==hyou[7]))||((hyou[2]==hyou[5])&&(hyou[5]==hyou[8]))||((hyou[0]==hyou[4])&&(hyou[4]==hyou[8]))||((hyou[2]==hyou[4])&&(hyou[4]==hyou[6]))

    zibun(hyou,iti_)
    hyouzi(hyou)
    sleep(3)
    e+=1
    if e>=9
      break
    end
    aite(hyou,iti_)
    hyouzi(hyou)
    puts(hyou)
    sleep(3)
    e+=1
    if e>=9
      break
    end
    puts(e)
  end
else
  puts("僕が先攻です。")
  f=0
  while (hyou[0]==hyou[1])&&(hyou[1]==hyou[2])||(hyou[3]==hyou[4])&&(hyou[4]==hyou[5])||(hyou[6]==hyou[7])&&(hyou[7]==hyou[8])||(hyou[0]==hyou[3])&&(hyou[3]==hyou[6])||(hyou[1]=hyou[4])&&(hyou[4]==hyou[7])||(hyou[2]==hyou[5])&&(hyou[5]==hyou[8])||(hyou[0]==hyou[4])&&(hyou[4]==hyou[8])||(hyou[2]==hyou[4])&&(hyou[4]==hyou[6])
    aite(hyou,iti_)
    hyouzi(hyou)
    sleep(3)
    f+=1
    if f>=9
      break
    end
    puts(hyou)
    zibun(hyou,iti_)
    hyouzi(hyou)
    sleep(3)
    f+=1
    if f>=9
      break
    end
    puts(f)
  end
end
if (hyou[0]==hyou[1])&&(hyou[1]==hyou[2])&&(hyou[2]=="○")||(hyou[3]==hyou[4])&&(hyou[4]==hyou[5])&&(hyou[2]=="○")||(hyou[6]==hyou[7])&&(hyou[7]==hyou[8])&&(hyou[2]=="○")||(hyou[0]==hyou[3])&&(hyou[3]==hyou[6])&&(hyou[2]=="○")||(hyou[1]=hyou[4])&&(hyou[4]==hyou[7])&&(hyou[2]=="○")||(hyou[2]==hyou[5])&&(hyou[5]==hyou[8])&&(hyou[2]=="○")||(hyou[0]==hyou[4])&&(hyou[4]==hyou[8])&&(hyou[2]=="○")||(hyou[2]==hyou[4])&&(hyou[4]==hyou[6])&&(hyou[2]=="○")
  puts("あなたの勝ちです。")
elsif ((hyou[0]==hyou[1])&&(hyou[1]==hyou[2])&&(hyou[2]=="×"))||((hyou[3]==hyou[4])&&(hyou[4]==hyou[5])&&(hyou[2]=="×"))||((hyou[6]==hyou[7])&&(hyou[7]==hyou[8])&&(hyou[2]=="×"))||((hyou[0]==hyou[3])&&(hyou[3]==hyou[6])&&(hyou[2]=="×"))||((hyou[1]=hyou[4])&&(hyou[4]==hyou[7])&&(hyou[2]=="×"))||((hyou[2]==hyou[5])&&(hyou[5]==hyou[8])&&(hyou[2]=="×"))||((hyou[0]==hyou[4])&&(hyou[4]==hyou[8])&&(hyou[2]=="×"))||((hyou[2]==hyou[4])&&(hyou[4]==hyou[6])&&(hyou[2]=="×"))
  puts("あなたの負けです。")
else
  puts("引き分けです。")
end