“ nil:NilClass的未定义方法“ +””

时间:2018-07-23 18:53:32

标签: ruby rest this

# .              gLayer1. gLayer2  gLayer3  gLayer4. gLayer5. gLayer6. gLayer7. gLayer8. gLayer9
gameBoard = %w[oooooooo xooooooo oxoooooo ooxooooo oooxoooo ooooxooo oooooxoo ooooooxo ooooooox]

puts 'hello, welcome to eve sim'
sleep 1
puts 'this is a game, where the player has to avoid stuff, and collect candy.'
sleep 2
puts 'please use wasd to move around'
puts ' '

puts gameBoard[1]
puts gameBoard[0]
puts gameBoard[0]
puts gameBoard[0]
puts gameBoard[0]

gLayer1 = 1
gLayer2 = 0
glayer3 = 0
gLayer4 = 0
gLayer5 = 0

x_layer = 1
x_pos = 1

loop do
  move_command = gets.chomp

  if move_command == 'd'

    # \/.    \/.    \/.    \/.    \/.    \/.    \/.
    # this is the code to move the player right
    if x_layer == 1
      gLayer1 += 1

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_pos += 1

    elsif x_layer == 2
      gLayer2 += 1

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_pos += 1

    elsif x_layer == 3
      glayer3 += 1

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_pos += 1

    elsif x_layer == 4
      gLayer4 += 1

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_pos += 1

    elsif x_layer == 5
      gLayer5 += 1

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_pos += 1

    end

    if x_pos == 9
      puts 'you have died, sorry'
      sleep 2
      exit!
    end

  elsif move_command == 's'

    if x_layer == 1
      gLayer1 -= gLayer1
      gLayer2 += x_pos

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_layer += 1

    elsif x_layer == 2
      gLayer2 -= gLayer2
      gLayer3 += x_pos

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_layer += 1

    elsif x_layer == 3
      gLayer3 -= gLayer3
      gLayer4 += x_pos

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_layer += 1

    elsif x_layer == 4
      gLayer4 -= gLayer4
      gLayer5 += x_pos

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_layer += 1

    elsif x_layer == 5
      gLayer5 -= gLayer5
      gLayer6 += x_pos

      puts gameBoard[gLayer1]
      puts gameBoard[gLayer2]
      puts gameBoard[glayer3]
      puts gameBoard[gLayer4]
      puts gameBoard[gLayer5]

      x_layer += 1

      puts 'you are dead, sorry.'
      sleep 1
      exit!
    end

  elsif move_command == 'a'

  elsif move_command == 'w'

    x_layer += 1

  end
end

首先出现的部分很好。它只是s控制器。当我进入第3层时,它给了我这个错误。

我很确定这与将两个负数加在一起有关,但我仍然不知道那将如何引发错误。我不知道这段代码出了什么问题。

请不要添加任何内容;只需解释一下,然后修复需要修复的部分即可。

3 个答案:

答案 0 :(得分:1)

gLayer3没有价值。或者更好,它的值为nil。因为您将其称为glayer3。因此,您实际上正在做nil + something_else。那就是nil.+(something_else)。而且+没有nil方法。

答案 1 :(得分:1)

在xlayer == 5块中,未定义gLayer6并返回nil。只需在程序开始时设置gLayer6 = 0。

答案 2 :(得分:1)

请记住everything on Ruby are objects。因此,当您致电a + b时,与a.+ b相同。因此+是来自“ a”的方法。

因此,如果您收到“ nil:NilClass的未定义方法+,是因为运算符/方法左侧的某些变量为nil。

要准确检查发生的位置,请在日志中检查发生的行。希望我能帮助您理解您的错误,并在下次使事情变得简单。