From 1ac3abd42d400c1177027b66c4641083fc004e01 Mon Sep 17 00:00:00 2001 From: andrew morton Date: Mon, 23 Oct 2017 20:57:13 -0600 Subject: [PATCH 1/2] Put the first look back before the occurances Restores the behavior that was changed in #11 --- lib/scottkit/play.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/scottkit/play.rb b/lib/scottkit/play.rb index 41b67fd..343b9c0 100644 --- a/lib/scottkit/play.rb +++ b/lib/scottkit/play.rb @@ -68,6 +68,8 @@ def prepare_to_play @fh = File.new(file) raise "#$0: can't read input file '#{file}': #$!" if !@fh end + + actually_look end def finished? From cf2b41b5318024e16d3636b2aefa5a6a7199ebaf Mon Sep 17 00:00:00 2001 From: andrew morton Date: Mon, 23 Oct 2017 21:31:10 -0600 Subject: [PATCH 2/2] Refactor game loop to allow easy external use Fixes #8 --- lib/scottkit/play.rb | 51 ++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/scottkit/play.rb b/lib/scottkit/play.rb index 343b9c0..15e522a 100644 --- a/lib/scottkit/play.rb +++ b/lib/scottkit/play.rb @@ -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 } @@ -72,10 +54,32 @@ def prepare_to_play 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 @@ -364,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