Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Rubyで簡易oxゲーム
$ary = (1...10).to_a
$player = -1
$cnt = 1
def showDisp
puts "Select number between 1...9"
(0...9).each do |i|
if (i + 1) % 3 == 0 then
puts getStone($ary[i])
else
print getStone($ary[i])
end
end
end
def getStone(n)
if n == 12
return "o"
elsif n == 10
return "x"
end
return n
end
def setStone(p, n)
if getStone($ary[n]).class == Fixnum && n >= 0 then
$ary[n] = 11 + p
$cnt += 1
else
puts "error"
$player *= -1
end
end
def checkComp
if $ary[0] == $ary[1] && $ary[1] == $ary[2] then return true end
if $ary[3] == $ary[4] && $ary[4] == $ary[5] then return true end
if $ary[6] == $ary[7] && $ary[7] == $ary[8] then return true end
if $ary[0] == $ary[3] && $ary[3] == $ary[6] then return true end
if $ary[1] == $ary[4] && $ary[4] == $ary[7] then return true end
if $ary[2] == $ary[5] && $ary[5] == $ary[8] then return true end
if $ary[0] == $ary[4] && $ary[4] == $ary[8] then return true end
if $ary[6] == $ary[4] && $ary[4] == $ary[2] then return true end
return false
end
system("cls")
showDisp
while (s = gets) != "q\n"
setStone($player *= -1, s.to_i - 1)
system("cls")
showDisp
if checkComp then
print "Winner Player \"", getStone(11 + $player), "\"\n"
exit
end
if $cnt > 9 then
break
end
end
puts "drow."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment