Skip to content

Instantly share code, notes, and snippets.

@tokutoku3
Created July 1, 2012 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokutoku3/3028448 to your computer and use it in GitHub Desktop.
Save tokutoku3/3028448 to your computer and use it in GitHub Desktop.
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