【问题标题】:Why does the else condition of my if-statement not work?为什么我的 if 语句的 else 条件不起作用?
【发布时间】:2013-11-19 01:38:20
【问题描述】:

我写了一个井字游戏程序。我遇到的问题是在我的if 语句中,它允许用户输入他/她想要的坐标,我的else 条件不起作用。 else 条件适用于用户输入不在板上的坐标。

这是我的代码:

class Game

  def initialize
    @board=Array.new
    @board[1]="1  __|"
    @board[2]="__"
    @board[3]="|__"
    @board[4]="\n2  __|"
    @board[5]="__"
    @board[6]="|__"
    @board[7]="\n3    |"
    @board[8]="  "
    @board[9]="|  "
    @turn="o"
    @win_status = false
  end

  def turn
    @turn
  end

  def show_board
    puts "   1  2  3"
    @board.each do |i|
      print i
    end
    puts ""
  end

  def set_turn #switches turns
    if @turn == "x"
      @turn = "o"
    else @turn == "o"
      @turn = "x"
    end
  end

  def make_move
    puts "Enter x coordinate"
    x=gets.to_i
    puts "Enter y coordinate"
    y=gets.to_i
    if y==1 && x==1
      @board[1]="1  _"+@turn+"|"
    elsif y==2 && x==1
      @board[2]="_"+@turn
    elsif  y==3 && x==1
      @board[3]="|_"+@turn
    elsif y==1 && x==2
      @board[4]="\n2  _"+@turn+"|"
    elsif y==2 && x==2
      @board[5]="_"+@turn
    elsif y==3 && x==2
      @board[6]="|_"+@turn
    elsif y==1 && x==3
      @board[7]="\n3   "+@turn+"|"
    elsif y==2 && x==3
      @board[8]=" "+@turn
    elsif y==3 && x==3
      @board[9]="| "+@turn+" \n"
    else
      "You entered an invalid coordinate"
    end

  end

  def win_combo
    return [[@board[1][4] + @board[2][1] + @board[3][2]], [@board[4][5] + @board[5][1] + @board[6][2]], [@board[7][5] + @board[8][1] + @board[9][2]],[@board[1][4] + @board[4][5] + @board[7][5]], [@board[2][1] + @board[5][1] + @board[8][1]], [@board[3][2] + @board[6][2] + @board[9][2]], [@board[1][4] + @board[5][1] + @board[9][2]], [@board[3][2] + @board[5][1] + @board[7][5]]]
  end

  def check_win
    #if some row or column or diagonal is "xxx" or "ooo" then set @win_status = true
    self.win_combo.each do |arr|
      str = arr.join
      if str == "xxx"
        puts "X Wins!"
        return true
      elsif str == "ooo"
        puts "O Wins!"
        return true
      end
    end
    return false
  end
  g = Game.new
  while g.check_win != true
    g.show_board
    g.set_turn
    g.make_move
  end
end

【问题讨论】:

    标签: ruby class if-statement tic-tac-toe


    【解决方案1】:

    你只是返回字符串:“你输入了一个无效的坐标”。

    我怀疑您想使用以下方式显示它:

    puts "You entered an invalid coordinate"
    

    否则,它作为g.make_move 的结果传递,然后被忽略。

    【讨论】:

      【解决方案2】:

      我假设您想在 x、y 坐标无效的情况下向控制台打印:“您输入的坐标无效”。您需要在该语句中添加一个方法,例如:

      else
        puts "You entered an invalid coordinate"
      end
      

      或者:

      else
        abort "You entered an invalid coordinate"
      end
      

      【讨论】:

      • abort 会非常严厉。循环直到用户输入一个可接受的坐标会更友好和更温和。
      • 好电话。会做几个 while 循环,直到输入有效,否则你会推荐一种更有效的方法。
      • 我会使用一个简单的loop ... until 并测试有效条件作为退出标准,然后以此为基础。人们总是用粗手指输入数字条目,因此 X/Y 条目可能无效,但尝试使用已分配的单元格也是如此,因此至少应检查两者。
      【解决方案3】:

      您似乎忘记在"You entered an invalid coordinate" 字符串前面使用putsprint。按照目前的写法,它是从方法中返回的。

      在 Ruby 中,方法的返回值是值returned by the last statement evaluated。例如,如果x=3,这两种方法都将返回相同的值:

      def square_example(x)
        if x ==3
          x_squared = 9
        end
      end
      
      
      def square_example2(x)
        if x == 3
          x_squared = 9
        end
        return x_squared
      end
      

      为了简化测试,您可以尝试使用显式返回,以便您可以轻松地判断您从该方法返回的是什么。或者(作为 Ruby 的初学者)您可以在每个 if/else 结果中添加一个 puts 语句,以便您可以轻松监控每个移动的结果,然后在您知道一切正常时删除那些 puts 行.

      【讨论】:

        【解决方案4】:

        看起来这是对下面网站的误解,但如果您对“and”和“&&”之间的区别感兴趣,您应该查看下面的 cmets。 来自:http://www.tutorialspoint.com/ruby/ruby_operators.htm

        你会想要使用“and”而不是“&&”,例如:

        if y==1 and x==1
            # do move
        elsif y==2 and x==1
            # do move
        .
        .
        .
        else
            "invalid coordinate"
        end
        

        “&&”运算符将检查它两边的值是否非零。如果它们都是非零,那么它将返回 true。在您的情况下,它正在执行操作

        false && false
        

        其中 false != 0,因此返回 true。

        这里是另一个讨论:http://archive.railsforum.com/viewtopic.php?id=27353

        【讨论】:

        • and&& 仅在优先顺序上有所不同,&& 更高,因此在某些情况下,您需要在 and 后面加上括号,但在 && 后面不需要。这不是问题——你可以使用任何一个。在a && b(或and)中,Ruby 检查a。如果它评估false(即falsenil,仅此而已),它会返回false,甚至不检查b。如果a 为真,则返回b 的逻辑值。一些 Ruby 编码人员喜欢 and(优先级不是问题),因为他们认为它读起来更好;其他人总是使用&&,这部分是为了减少被优先级问题咬的机会。
        • +1 @CarySwoveland。被优先级错误所困扰可能很难找到和修复。我们中的一些人使用&&|| 以及括号来直观地展示我们想要什么,并强制解释器完全按照我们的意愿去做,而不是依赖于在语言之间移动时可能会改变的优先顺序.
        • +1 感谢您的澄清!对任何对此感到困惑的人表示歉意。似乎我只是误读了我发布的 ruby​​ 逻辑运算符网站。
        猜你喜欢
        • 1970-01-01
        • 2016-07-24
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        相关资源
        最近更新 更多