繁体   English   中英

如何使我的井字游戏打成平手?

[英]How can I make my tic-tac-toe game end in a tie?

我已经在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>'

更换

while @turn == "x" or "o"

while @turn == "x" or @turn == "o"

看来@square_count可能没有分配值,因此您可能会收到所指示的错误,因为如果没有分配, @square_count将保留nil值。

另外,您的while语句可能不是您想要的, while语句由于值“ o”当前将始终评估为'true'。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM