Method: Fae.interactive_mode

Defined in:
lib/fae.rb

.interactive_modeObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fae.rb', line 57

def interactive_mode
  states  = []
  strings = []

  print "~ Enter the letters of your language separated by a comma: ".colorize(:yellow)
  letters = gets.chomp.split(",")
  letters.map(&:strip!)

  language = Language.new(letters)
  print "~ Enter the description of your state diagram: ".colorize(:yellow)
  description = gets.chomp

  fa = FiniteAutomata.new(language, description)
  print "~ Enter your state names separated by a comma: ".colorize(:yellow)
  state_names = gets.chomp.split(",")
  state_names.map(&:strip!)

  state_names.each do |state|
    paths = {}
    puts "\nState #{state}:".colorize(:blue)

    letters.each do |letter|
      valid = false
      while (!valid)
        print "~ In state ".colorize(:yellow) + state.colorize(:blue) + " the letter ".colorize(:yellow) + letter.colorize(:blue) + " will take you to what state? ".colorize(:yellow)
        next_state = gets.chomp
        if (!state_names.include?(next_state))
          puts "State #{next_state} is not one of your state names. Please choose from the following: #{state_names}".colorize(:red)
        else
          paths[letter.to_sym] = next_state
          valid = true
        end
      end
    end

    print "~ Is state ".colorize(:yellow) + state.colorize(:blue) + " an accepting state? (y/n): ".colorize(:yellow)
    accepting = gets.chomp.casecmp('y').zero?
    states << State.new(state, paths, accepting)
  end

  finished = false
  string_values = []
  puts "~ Enter strings to test your state diagram with (type 'done' when finished):".colorize(:yellow)

  while(!finished)
    value = gets.chomp
    if (value == 'done')
      finished = true
    else
      string_values << value
    end
  end

  string_values.each do |value|
    print "~ Is ".colorize(:yellow) + value.colorize(:blue) + " a valid string for this state diagram? (y/n): ".colorize(:yellow)
    valid = gets.chomp.casecmp('y').zero?
    strings << String.new(value, valid)
  end
  puts

  fa.add_states(states)
  fa.add_strings(strings)
  fa.evaluate!
end