Game.rb
class Game
attr_reader :name,:total_score,:top_scores
def initialize
@maingame = Story.new
end
def self.instructions
font = TTY::Font.new(:doom)
#puts font.write ("MIRKWOOD")
#artii 'MIRKWOO'
end
prompt = TTY::Prompt.new
font = TTY::Font.new(:doom)
# name = prompt.ask
puts " "
puts " "
puts " "
puts ""
puts "Welcome to Mirkwood".center(80)
puts " "
puts "This is a text based role playing game in the style of the old Choose Your Own Adventure books set in Tolkien's Middle Earth.".center(80)
puts " "
puts "At the end of a body of text from the story you will be presented with choices which will determine the path the story takes.".center(80)
puts " "
puts " "
puts " "
prompt = TTY::Prompt.new
font = TTY::Font.new(:doom)
# name = prompt.ask
name = prompt.ask("Before you enter the Mirkwood adventurer do tell me your name?", default: "hero") do |q|
q.required true
q.validate /Aw+Z/
q.modify :capitalize
end
puts " "
puts " "
puts "Your cloak flicks widly behind you, wrenched back and forth by an ill and cold"
puts "wind bringing the smell of dank and decay from the dark forest ahead stretching"
puts "as far as can be seen north, south and west."
puts " "
puts " "
puts "To the southern side of the road sits a rickety old log cabin, there upon the front landing sits"
puts "a withered old man whose eyes are covered by a strip of cloth belying the blindness beneath."
puts " "
puts "Greetings #{name} I have been expecting you! Many have passed this way and so few have"
puts "returned from the cursed Mirkwood, those that have rarely do speak of what lies within"
puts "or beyond that dark place. You are either very brave or very motivated to enter here."
puts " "
puts " "
puts "The road forks here brave #{name} and which of these three roads into the Mirkwood you take"
puts "will be the first of many choices you must make on your journey."
puts " "
puts " "
end
#instructions
prompt = TTY::Prompt.new
choices1 = {The_north_road: 1, The_middle_road: 2, The_south_road: 3}
choice = prompt.select("Which road do you choose?", choices1)
def
next_fork = item.paragraph1 if choice == 1
next_fork = item.paragraph2 if choice == 2
next_fork = item.paragraph3 if choice == 3
item = @maingame.get_fork(next_fork)
end
Fork.rb
require_relative 'story'
class Fork
attr_reader :id, :story, :path1, :path2, :finish, :score
def initialize(id,story,paragraph1,paragraph2,paragraph3,paragraph4,paragraph5,finish,score)
@id = id
@story = story
@paragraph1 = paragraph1
@paragraph2 = paragraph2
@paragraph3 = paragraph3
@paragraph4 = paragraph4
@paragraph5 = paragraph5
@finish = finish
@score = score
end
end
Story.rb
class Paragraph1
attr_accessor :id , :description
paragraph1 = puts"Ah the north road, a wise choice friend as your way will meander towards Ered Mithrin and Durins folk of the Grey Mountains'
'Perhaps like Gandalf and his companions who whence this way you will find Erebor the lonely mountain, whereby Smaug the Terrible met his end.'
'Now friend the hour grows late and you best away lest you not find a clearing within the woods to light a fire for camp at night.
'Bidding the kind old man well and flicking a silver coin his
我正在尝试用Ruby构建一个简单的、选择自己的基于冒险的终端应用程序。
我正在使用tty提示发布问题并给出选项-
puts "The road forks here brave #{name} and which of these three roads into the Mirkwood you take"
puts "will be the first of many choices you must make on your journey."
puts " "
puts " "
end
prompt = TTY::Prompt.new
choices1 = {The_north_road: 1, The_middle_road: 2, The_south_road: 3}
choice = prompt.select("Which road do you choose?", choices1)
我想根据选择引出一个字符串,字符串存储在另一个ruby文件中。然而,这似乎并不奏效。选择会导致意外的输入。
错误=game.rb:117:语法错误,意外的"=",应为";"或'\n'next_fork=item.paraght1 if choice==1–
任何帮助都将不胜感激。
您发布的错误指的是game.rb:117。game.rb文件中的第117行。你添加到问题中的game.rb只有100行,但我想你在发布之前已经删除了一些代码。如果你转到game.rb文件中的第117行,你会发现:
def
next_fork = item.paragraph1 if choice == 1
next_fork = item.paragraph2 if choice == 2
在ruby中,关键字def
定义了一个方法,但它也需要一个名称。Ruby假定next_fork
是方法的名称,因为这是def
之后的第一个名称。在那之后,有一个=
,它不能刚好出现在方法定义之后,并且是语法错误。这就是鲁比要告诉你的。
game.rb:117: syntax error, unexpected '=', expecting ';' or 'n' next_fork = item.paragraph1 if choice == 1
为方法指定一个名称。