module Console

Functions which create a user-interface in the terminal

Public Instance Methods

a2i(array) click to toggle source
# File lib/console.rb, line 149
def a2i(array)
  UserInput::a2i(array)
end
ask_new_event() click to toggle source
# File lib/console.rb, line 160
def ask_new_event
  puts "\nEnter number for more information about an event, 'a' to play with one new, random event, 'q' to quit." 
  UserInput::input()
end
clear() click to toggle source
# File lib/console.rb, line 121
def clear
  system 'clear'
end
compare_order(response) click to toggle source
# File lib/console.rb, line 204
def compare_order(response)
  puts "\n... You say: " << bold(response.join(', '))
  devents = @m_events.dup.sort {|f, s| f.year <=> s.year } 
  cmt = "\tThe right order is:\n" << devents.collect {|ev| ev.disp_index.to_s << ') ' << ev.year.to_s }.join("\n").box()
  wait(@@wait_sequence, 1, cmt)
end
detail(num) click to toggle source

show information on an event

# File lib/console.rb, line 185
def detail(num)
  if(0 != num && num <= @m_events.length)
    det = @m_events.detect{|ev| ev.disp_index == num}.detail
    if(det && !det.empty?)
      puts ("\n" << num.to_s << ') ' << det).box(:color => :green)
    else
      puts " - No details known -"
    end
  end
end
int_array_input(len, range) click to toggle source
# File lib/console.rb, line 156
def int_array_input(len, range)
  UserInput::int_array_input(len, range ) 
end
int_input(range = nil) click to toggle source
# File lib/console.rb, line 153
def int_input(range = nil)
  UserInput::int_input(range)
end
lose() click to toggle source
# File lib/console.rb, line 169
def lose
  cmt = 'You ' << red('lose.') 
  wait @@fail_sequence, 1, cmt 
end
new_event() click to toggle source

add a new event to the game

# File lib/console.rb, line 127
def new_event
  known_events = @m_events.collect {|ev| ev.title}
  available = $events.collect {|ev| ev unless known_events.include?(ev[0]) }.compact
  if !available || available.empty?
    puts "\n\tNo more events available. Aborting. Bye"
    puts
    return :all_events_done
  end
  clear
  out = ''
  @m_events.each_with_index {|ev, i| out << (i + 1).to_s << ') ' << ev.title << "\n"}
  puts out.box(:left => 2)

  puts "Indicate the event from the previous list, which precedes the following, or '0' (zero) to put it in front:"
  srand(Time.new.usec)
  index = rand(available.length)
  ev = Event.new(available[index]) 
  @m_events << ev
  puts ev.title.box(:color => :yellow)
  return ev
end
position_by_user(p) click to toggle source
# File lib/console.rb, line 196
def position_by_user(p)
  if(p > 0)
    puts "\n... You say: " << bold("after \"" << @m_events[p-1].title << "\"")
  else
    puts "\n... You say: " << bold("Earlier than any of the listed events")
  end
end
score(pts) click to toggle source
# File lib/console.rb, line 165
def score(pts)
  puts ' (Current score: %i %s)' %[pts, (pts == 1 ? 'point' : 'points')]
end
show_correct_order() click to toggle source
# File lib/console.rb, line 179
def show_correct_order
  cmt = "\tThe right order is:\n" << @m_events.collect {|ev| (@m_events.index(ev) + 1).to_s << ') ' << ev.title << ' (' << ev.year.to_s << ')'}.join("\n").box()
  wait(@@wait_sequence, 1, cmt) 
end
start_game() click to toggle source

start with some arbitrary events

# File lib/console.rb, line 37
def start_game
  available = $events
  clear
  puts "Put the following events into the right chronological order (e.g.: 2 3 1)."
  @m_events = Array.new
  out = ''
  $num_events.times do |i|
    srand(Time.new.usec)
    index = rand(available.length)
    ev = Event.new(available.delete_at(index))
    ev.disp_index = (i + 1)
    @m_events << ev
    out << (i + 1).to_s << ') ' << ev.title << "\n"
  end
  puts out.box(:left => 2)
  response = nil

  response = int_array_input($num_events, (1..$num_events) ) 
  if response == :quit_game
    score @@points
    exit true
  end

  compare_order(response)
  # good order of the response-array
  if good_order(response)
    @@points += $num_events 
    win
    score(@@points)
  else
    lose
    score(@@points)
  end

  # Start the loop. Add events as requested.
  until @m_events.length == $MAX_NUM_EVENTS do
    ui = ask_new_event
    if ui == :quit_game
      score @@points
      exit true
    end
    if('a' == ui)
      ev = new_event
      if ev == :all_events_done
        score(@@points)
        exit true
      end

      # ------> unresolved trouble with 0-based array.indices. <----------
      # 1) exclude new event from the choices (hence -1), allow 0 as 'earlier than any'.
      r = int_input(0..(@m_events.length - 1) )
      if r == :quit_game
        score @@points
        exit true
      end
      # User-input can be 0, which complicates the comparison with the
      # 0-based event-array.

      # 2) in the range of known events, if not 0 (hence 0 is allowed).
      position_by_user(r)

      # 4) 'order' must fit in between the indices of the event-array (0-based, hence -1. Again.)
      ok = good_order(r - 1, ev)

      sort_years(true)
      # 5) show the resulting order as starting from 1, not 0.
      show_correct_order
      # <---------- This should be it. ------------>

      if ok
        @@points += 1
        win
        score(@@points)
      else
        lose
        score(@@points)
      end
    else
      detail( a2i(ui) )
    end
  end

end
wait(seq, sec, cmt) click to toggle source
# File lib/console.rb, line 211
def wait(seq, sec, cmt)
  bi = BusyIndicator.new(false)
  bi.sequence = seq
  bi.run
  sleep sec
  bi.stop(cmt)
end
win() click to toggle source
# File lib/console.rb, line 174
def win
  cmt = 'You ' << cyan(' W O N !!') 
  wait(@@win_sequence, 1, cmt)
end