如何在Ruby中修复NoMethodError?

问题描述

我写了一个基于Ruby的后端多人游戏(蛇)。此功能必须接收前端命令并发送游戏的当前状态。但是对于changeside方法,在函数的四行上抛出NoMethod错误,我不知道为什么会因为该类方法而存在。

方法示例

  def changeside(nump,newSide)
     opp = {'R':'L','U':'D','D':'U','L':'R'}
     cx = {'L':-1,'R':1,'U':0,'D':0}
     cx = {'L':0,'R':0,'U':-1,'D':1}
     if newSide!=opposite[players[nump]['side']] && ['R','L','U','D'].count( newSide)==1
       players[nump]['side']['cx'] = cx[newSide]
       players[nump]['side']['cy'] = cy[newSide]
     end

  end

错误示例

2020-10-07 18:01:23 - NoMethodError - undefined method `[]' for nil:NilClass:

我正在运行的功能

def getStatus(game)
      ans = {"opStatus"=>TRUE}
      puts game
      $games[game['gid'.to_i]]['game'].changeside(2,game['side'])
      $games[game['gid'.to_i]]['game'].move(($games[game['gid']]['fid']==game['pid']?1:2))
      ans["gameStatus"] = $games[game['id']]['game'].run
      ans['timer'] = $games[game['id']]['game'].timer
      ans['snake'] = $games[game['id']][($games[game['id']]['fid']==game['pid']?1:2)].snake
      ans['enemy'] = $games[game['id']][($games[game['id']]['fid']==game['pid']?2:1)].snake
      ans['food'] = $games[game['id']]['game'].food
    end

这是JSON发送到函数

{"gid"=>"1","side"=>"R","pid"=>"1"}

解决此问题,我调试了这一刻,但无法进入类方法。 这是类游戏的完整代码。 它位于另一个文件中,并被

调用
`require_relative 'game.rb'`

该类的完整代码

class Game
  attr_accessor :players,:food,:walls,:timer,:run,:winner

  def initialize()
    @players = {}
    @winner = -1
    @players[1] = {'snake'=>[[9,2],[8,[7,2]]}
    @players[1]['cx'] = 0
    @players[1]['cy'] = -1
    @players[1]['side'] = 'U'
    @players[2] = {'snake'=>[[2,9],[2,8],7]]}
    @players[2]['cx'] = -1
    @players[2]['cy'] = 0
    @players[2]['side'] = 'L'
    @timer = 0
    @run = FALSE

    genFood
  end
  def genFood()
    @food = [rand(10)+1,rand(10)+1]
    while  players[1]['snake'].include?(food) || players[2]['snake'].include?(food)
      @food = [rand(10)+1,rand(10)+1]
    end
  end

  def move(nump)

    if @run
      @timer+=0.25
      @players[nump]['snake'].shift
      @players[nump]['snake'].push([@players[id]['snake'][0]+@players[id]['cx'],@players[id]['snake'][1]+@players[id]['cy']])
      if @players[nump]['snake'][0] == food
        @players[nump]['snake'][0].unshift(food)
        @food = [rand(10)+1,rand(10)+1]
        while  players[nump]['snake'].include(food) or players[1]['snake'].include(food)
          @food = [rand(10)+1,rand(10)+1]
        end
      end
      if @player[nump]['snake'].count(@player[nump]['snake'][-1])==2 ||
          @winner = ((nump==1)?2:1)
      elsif @players[((nump==1)?2:1)]['snake'].count(@player[nump]['snake'][-1])==1
        @winner = 0
      else
        if @timer==60
          if @players[1]['snake'].length()>@players[2]['snake'].length()
            @winner = 1
          elsif @players[1]['snake'].length()<@players[2]['snake'].length()
            winner = 2
          else
            winner = 0
          end
        end
      end
    end
  end

  def changeside(nump,newSide)
    opp = {'R':'L','L':'R'}
    cx = {'L':-1,'D':0}
    cx = {'L':0,'D':1}
    if newSide!=opposite[players[nump]['side']] && ['R','D'].count( newSide)==1
      players[nump]['side']['cx'] = cx[newSide]
      players[nump]['side']['cy'] = cy[newSide]
    end
  end

end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)