为什么我为此方法收到“方法未定义”错误?

时间:2014-11-28 01:25:08

标签: ruby methods undefined

我在调用之前已经定义了方法启动,但我仍然被告知它尚未被定义。我已经尝试过寻找答案,但我能找到的唯一信息是说这个方法需要在被调用之前定义,我已经清楚地完成了!代码如下,启动在底部调用。我知道我的代码不完整,我无法理解这个未定义的错误。

# Ruby code file - All your code should be located between the comments provided.

# Add any additional gems and global variables here
# require 'sinatra'     # remove '#' character to run sinatra wen server

# Main class module
module OXs_Game
# Input and output constants processed by subprocesses. MUST NOT change.
NOUGHT = 0
CROSS = 1

class Game
    attr_reader :matrix, :input, :output, :player1, :player2, :winner
    attr_writer :matrix, :input, :output, :player1, :player2, :winner

    def initialize(input, output)
        @input = input
        @output = output
    end

    # Any code/methods aimed at passing the RSpect tests should be added below.
        def start
            @output.puts "Welcome to Noughts and Crosses!"
            @output.puts "Starting game..."
            @output.puts "Created by:Stephen Mitchell"
            @output.puts "Player 1: 0 and Player 2: 1"
        end

        def created_by
            return "myname"
        end

        def student_id
            return numberhere
        end

        def setplayer1
            @player1 = 0
        end

        def setplayer2
            @player2 = 1
        end

        def clearmatrix
            @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
        end

        def getmatrixvalue(n)
            @matrix[n]
        end

        def setmatrixvalue(i, v)
            @i = 1
            @v = "0"
            @matrix[i] = "0"            
        end

        def displaykey(matrix)
            @matrix = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
            @output.puts "Table key:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
        end

        def displaymatrix
            @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
            @output.puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
        end

        def finish
            @output.puts "Finishing game..."
        end

        def displaymenu
            @output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
        end

        def checkwinner
            if @matrix[0] = "0" && @matrix[1] = "0" && @matrix[2] = "0" then
                winner = 1
            elsif @matrix[3] = "0" && @matrix[4] = "0" && @matrix[5] = "0" then
                winner = 1
            elsif @matrix[6] = "0" && @matrix[7] = "0" && @matrix[8] = "0" then
                winner = 1
            elsif @matrix[0] = "0" && @matrix[3] = "0" && @matrix[6] = "0" then
                winner = 1
            elsif @matrix[1] = "0" && @matrix[4] = "0" && @matrix[7] = "0" then
                winner = 1  
            elsif @matrix[2] = "0" && @matrix[5] = "0" && @matrix[8] = "0" then
                winner = 1
            elsif @matrix[0] = "0" && @matrix[4] = "0" && @matrix[8] = "0" then
                winner = 1
            elsif @matrix[2] = "0" && @matrix[4] = "0" && @matrix[6] = "0" then
                winner = 1
            elsif @matrix[0] = "1" && @matrix[1] = "1" && @matrix[2] = "1" then
                winner = 2
            elsif @matrix[3] = "1" && @matrix[4] = "1" && @matrix[5] = "1" then
                winner = 2
            elsif @matrix[6] = "1" && @matrix[7] = "1" && @matrix[8] = "1" then
                winner = 2
            elsif @matrix[0] = "1" && @matrix[3] = "1" && @matrix[6] = "1" then
                winner = 2
            elsif @matrix[1] = "1" && @matrix[4] = "1" && @matrix[7] = "1" then
                winner = 2  
            elsif @matrix[2] = "1" && @matrix[5] = "1" && @matrix[8] = "1" then
                winner = 2
            elsif @matrix[0] = "1" && @matrix[4] = "1" && @matrix[8] = "1" then
                winner = 2
            elsif @matrix[2] = "1" && @matrix[4] = "1" && @matrix[6] = "1" then
                winner = 2  
            end
        end

    # Any code/methods aimed at passing the RSpect tests should be added above.

end
end

# Main program
module OXs_Game
@input = STDIN
@output = STDOUT
g = Game.new(@input, @output)
matrixkey = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
playing = true
input = ""
option = 0
turn = 0

# Any code added to output the activity messages to the command line window should be added below.

def startup
    g.start
    g.displaykey(matrixkey)
    g.displaymatrix
    puts "Please select an option:"
    g.displaymenu
    menu_input = gets.chomp
end

def menu
    if menu_input == 1 then
        player1turn
    elsif menu_input == 2 then
        g.clearmatrix
        startup
    elsif menu_input == 9 then
        exit
    end
end

def player1turn
    g.displaymatrix
    puts "Player 1 to move."
    player1input = gets.chomp
    if player1input == 1 then
        @matrix[0] == "0"
    elsif player1input == 2 then
        @matrix[1] == "0"
    elsif player1input == 3 then
        @matrix[2] == "0"
    elsif player1input == 4 then
        @matrix[3] == "0"
    elsif player1input == 5 then
        @matrix[4] == "0"
    elsif player1input == 6 then
        @matrix[5] == "0"
    elsif player1input == 7 then
        @matrix[6] == "0"   
    elsif player1input == 8 then
        @matrix[7] == "0"   
    elsif player1input == 9 then
        @matrix[8] == "0"
    else
        puts "Invalid input, please try again."
        player1turn
    end

end 

startup 
# Any code added to output the activity messages to the command line window should be added above.

3 个答案:

答案 0 :(得分:0)

您似乎已将startup方法定义为OXs_Game模块的一部分实例方法,但是当您调用它时,您不在模块之外。

尝试删除OXs_Game模块(但在代码中保留其中定义的所有方法)。

如果您保留OXs_Game模块,因为您在模块定​​义中使用了局部变量,那么您的代码会遇到其他问题,但是只要您使用{{1}定义一个创建新范围的方法,您将无法再访问本地变量。使用def定义类或class定义模块时也是如此。

答案 1 :(得分:0)

跳出来的一件事是:

def setmatrixvalue(i, v)
    @i = 1
    @v = "0"
    @matrix[i] = "0"            
end

为什么不对v做任何事情?你为什么不打电话给这个方法?

答案 2 :(得分:0)

这是工作代码。 我做了两个重大改变。 在主程序中,您声明了在startupmenu方法中无法访问的局部变量,因此我将它们更改为实例变量。

其次,您的startup方法是实例方法,如果您想以startup方式调用它,则调用OXs_Game::startup无法直接调用该方法。在这个i代码中,我将startup方法更改为类级方法,因此可以直接调用它。您可以通过OXs_Game::startup调用它并将其保留为代码中的实例级别:)

module OXs_Game
 #Input and output constants processed by subprocesses. MUST NOT change.
NOUGHT = 0
CROSS = 1
class Game
    attr_reader :matrix, :input, :output, :player1, :player2, :winner
    attr_writer :matrix, :input, :output, :player1, :player2, :winner
    def initialize(input, output)
        @input = input
        @output = output
    end

#Any code/methods aimed at passing the RSpect tests should be added below.
    def start
        @output.puts "Welcome to Noughts and Crosses!"
        @output.puts "Starting game..."
        @output.puts "Created by:Stephen Mitchell"
        @output.puts "Player 1: 0 and Player 2: 1"
    end

    def created_by
        return "myname"
    end

    def student_id
        return numberhere
    end

    def setplayer1
        @player1 = 0
    end

    def setplayer2
        @player2 = 1
    end

    def clearmatrix
        @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
    end

    def getmatrixvalue(n)
        @matrix[n]
    end

    def setmatrixvalue(i, v)
        @i = 1
        @v = "0"
        @matrix[i] = "0"            
    end

    def displaykey(matrix)
        @matrix = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
        @output.puts "Table key:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
    end

    def displaymatrix
        @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
        @output.puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
    end

    def finish
        @output.puts "Finishing game..."
    end

    def displaymenu
        @output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
    end

    def checkwinner
        if @matrix[0] = "0" && @matrix[1] = "0" && @matrix[2] = "0" then
            winner = 1
        elsif @matrix[3] = "0" && @matrix[4] = "0" && @matrix[5] = "0" then
            winner = 1
        elsif @matrix[6] = "0" && @matrix[7] = "0" && @matrix[8] = "0" then
            winner = 1
        elsif @matrix[0] = "0" && @matrix[3] = "0" && @matrix[6] = "0" then
            winner = 1
        elsif @matrix[1] = "0" && @matrix[4] = "0" && @matrix[7] = "0" then
            winner = 1  
        elsif @matrix[2] = "0" && @matrix[5] = "0" && @matrix[8] = "0" then
            winner = 1
        elsif @matrix[0] = "0" && @matrix[4] = "0" && @matrix[8] = "0" then
            winner = 1
        elsif @matrix[2] = "0" && @matrix[4] = "0" && @matrix[6] = "0" then
            winner = 1
        elsif @matrix[0] = "1" && @matrix[1] = "1" && @matrix[2] = "1" then
            winner = 2
        elsif @matrix[3] = "1" && @matrix[4] = "1" && @matrix[5] = "1" then
            winner = 2
        elsif @matrix[6] = "1" && @matrix[7] = "1" && @matrix[8] = "1" then
            winner = 2
        elsif @matrix[0] = "1" && @matrix[3] = "1" && @matrix[6] = "1" then
            winner = 2
        elsif @matrix[1] = "1" && @matrix[4] = "1" && @matrix[7] = "1" then
            winner = 2  
        elsif @matrix[2] = "1" && @matrix[5] = "1" && @matrix[8] = "1" then
            winner = 2
        elsif @matrix[0] = "1" && @matrix[4] = "1" && @matrix[8] = "1" then
            winner = 2
        elsif @matrix[2] = "1" && @matrix[4] = "1" && @matrix[6] = "1" then
            winner = 2  
        end
    end

# Any code/methods aimed at passing the RSpect tests should be added above.

end
end

module OXs_Game
@input = STDIN
@output = STDOUT
@g = Game.new(@input, @output)
@matrixkey = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
@matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
@playing = true
@input = ""
@option = 0
@turn = 0

#Any code added to output the activity messages to the command line window should be added below.

def self.startup
    @g.start
    @g.displaykey(@matrixkey)
    @g.displaymatrix
    puts "Please select an option:"
    @g.displaymenu
    menu_input = gets.chomp
end

def menu
    if menu_input == 1 then
        player1turn
    elsif menu_input == 2 then
        @g.clearmatrix
        startup
    elsif menu_input == 9 then
        exit
    end
end

def player1turn
    @g.displaymatrix
    puts "Player 1 to move."
    player1input = gets.chomp
    if player1input == 1 then
        @matrix[0] == "0"
    elsif player1input == 2 then
        @matrix[1] == "0"
    elsif player1input == 3 then
        @matrix[2] == "0"
    elsif player1input == 4 then
        @matrix[3] == "0"
    elsif player1input == 5 then
        @matrix[4] == "0"
    elsif player1input == 6 then
        @matrix[5] == "0"
    elsif player1input == 7 then
        @matrix[6] == "0"   
    elsif player1input == 8 then
        @matrix[7] == "0"   
    elsif player1input == 9 then
        @matrix[8] == "0"
    else
        puts "Invalid input, please try again."
        player1turn
    end

end 
startup
end