我在Ruby中创建了一个tic-tac-toe程序。我遇到了代码的一部分,它让游戏以平局结束。
我写了一个if语句来检查玩家何时赢得比赛。这是我的另一个条件。当我尝试运行程序时出现错误。怎么了?
else
while @turn == "x" or "o"
@square_count -= 1 # I set empty_count to 9 in the initialize of the class
# of this program. This would minus 1 from empty) count_each every turn
end
if @square_count == 0 #when all the slots are taken, its a tie game
puts "Tie game!"
return true #this makes the program end
end
我得到的错误是:
tac.rb:89: warning: string literal in condition
tac.rb:88:in `block in check_win': undefined method `-' for nil:NilClass (NoMethodError)
from tac.rb:77:in `each'
from tac.rb:77:in `check_win'
from tac.rb:108:in `<class:Game>'
from tac.rb:1:in `<main>'
答案 0 :(得分:0)
替换
while @turn == "x" or "o"
带
while @turn == "x" or @turn == "o"
答案 1 :(得分:0)
看起来可能没有为@square_count
分配值,因此您可能会收到您指出的错误,因为如果没有作业,@square_count
将保留一个零值。
此外,您的while语句可能不是您想要的,while
语句当前将始终评估为'true',因为值为“o”。