Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions lib/scottkit/play.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
module ScottKit
class Game

# Returns 1 if the game was won, 0 otherwise
def play
prepare_to_play

while true
# TODO: Find out if this can move to the bottom of the loop. If so we
# could probably turn the outter loop into `while !finished?`
run_matching_actions(0, 0)

actually_look if @need_to_look

return @finished if finished?
while !finished?
prompt_for_turn

print "Tell me what to do ? "
if !(line = gets)
# End of file -- we're done
puts
break
end

words = line.chomp.split
if words.length == 0
puts "I don't understand your command."
next
end

execute_command(words[0], words[1])

process_lighting
process_turn(line)
end

0
return @finished
end

private

def prepare_to_play
@finished = nil
@items.each { |x| x.loc = x.startloc }
Expand Down Expand Up @@ -68,12 +50,36 @@ def prepare_to_play
@fh = File.new(file)
raise "#$0: can't read input file '#{file}': #$!" if !@fh
end

actually_look
end

def prompt_for_turn
run_matching_actions(0, 0)

actually_look if @need_to_look

print "Tell me what to do ? "
end

def process_turn(line)
words = line.chomp.split
if words.length == 0
puts "I don't understand your command."
return
end

execute_command(words[0], words[1])

process_lighting
end

def finished?
!@finished.nil?
end

private

def process_lighting
if items.size > ITEM_LAMP && items[ITEM_LAMP].loc != ROOM_NOWHERE && @lampleft > 0
@lampleft -= 1
Expand Down Expand Up @@ -362,7 +368,8 @@ def is_dark
@flags[15] && loc != ROOM_CARRIED && loc != @loc
end

public :prompt_and_save, :need_to_look, :score, :ncarried, :inventory, :finish # Invoked from Instruction.execute()
# Invoked from Instruction.execute()
public :prompt_and_save, :need_to_look, :score, :ncarried, :inventory, :finish

class Condition
def evaluate
Expand Down