【发布时间】:2014-11-28 01:25:07
【问题描述】:
我在调用方法启动之前定义了它,但我仍然被告知它尚未定义。我试过寻找答案,但我能找到的唯一信息说该方法需要在调用之前定义,我显然已经完成了!代码如下,启动在底部被调用。我知道我的代码不完整,我就是无法克服这个未定义的错误。
# 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.
结束
【问题讨论】:
-
错误出现在哪里?不要指望人们会筛选所有代码...
-
它发生在调用启动时,在代码的末尾。就像我在帖子中所说的那样。
-
请求帮助调试问题的问题需要演示问题所需的最少代码。你有没有把它剥离到那个地步?很有可能将其剥离的过程会向您显示问题。
-
几个问题: 1. 方法
student_id中的numberhere是未定义的局部变量或未定义的方法。 2.局部变量matrixkey在startup中不可见。