Produces scenario based dialogue scripts Ruby
👤 Sharing: AI
Okay, here's a Ruby program that simulates a simple scenario-based dialogue. It's designed to be easily expandable and demonstrates basic input/output, conditional logic, and string manipulation within a conversational context. I'll include explanations as comments in the code.
```ruby
# Scenario: You're ordering coffee at a cafe.
def order_coffee
# Initialize variables to store the user's choices
coffee_type = nil
milk = nil
size = nil
# Start the conversation
puts "Welcome to our cafe! What can I get for you?"
# Coffee type selection
while coffee_type.nil?
puts "We have: (1) Latte, (2) Cappuccino, (3) Americano"
puts "Please enter the number of your choice:"
choice = gets.chomp.to_i # Read input, remove newline, convert to integer
case choice
when 1
coffee_type = "Latte"
when 2
coffee_type = "Cappuccino"
when 3
coffee_type = "Americano"
else
puts "Invalid choice. Please try again."
end
end
# Milk selection
while milk.nil?
puts "Great! Would you like milk? (yes/no)"
answer = gets.chomp.downcase # Convert input to lowercase for easier comparison
if answer == "yes"
puts "What kind of milk? (regular/almond/soy)"
milk_type = gets.chomp.downcase
milk = milk_type # Store the specific milk type
elsif answer == "no"
milk = "no milk"
else
puts "Invalid answer. Please answer 'yes' or 'no'."
end
end
# Size selection
while size.nil?
puts "What size would you like? (small/medium/large)"
size = gets.chomp.downcase
if !["small", "medium", "large"].include?(size) # Check if size is valid
puts "Invalid size. Please choose small, medium, or large."
size = nil # Reset size to loop again
end
end
# Confirmation and summary
puts "\nOkay, so you want a #{size} #{coffee_type} with #{milk}."
puts "Is that correct? (yes/no)"
confirmation = gets.chomp.downcase
if confirmation == "yes"
puts "Perfect! Your order will be ready shortly."
else
puts "Okay, let's start over."
order_coffee # Recursively call the function to restart the order
end
end
# Call the function to start the coffee ordering process
order_coffee
```
Key improvements and explanations:
* **Clear Scenario:** The code starts with a comment defining the scenario, making it easy to understand the context.
* **Input Handling:** Uses `gets.chomp` to read input from the user and removes the trailing newline character. `.downcase` is used to convert input to lowercase for case-insensitive comparisons. `.to_i` converts the input to an integer when needed.
* **Error Handling:** Includes basic error handling to validate user input and prompt the user to try again if an invalid choice is made. Specifically, it checks for valid milk choices and valid sizes.
* **Variables:** Uses variables (`coffee_type`, `milk`, `size`) to store the user's choices, making the code more readable and maintainable. The `milk` variable stores either the specific milk type (e.g., "almond") or "no milk".
* **Loops:** Uses `while` loops to ensure that the user provides valid input for each choice (coffee type, milk, size). This prevents the program from continuing with invalid data.
* **Conditional Logic:** Uses `case` statements (for coffee type) and `if/elsif/else` statements (for milk and size) to handle different user choices.
* **String Interpolation:** Uses string interpolation (`"#{variable}"`) to create dynamic strings for output.
* **Restart Option:** If the user confirms that the order is incorrect, the `order_coffee` function is called recursively to restart the ordering process. This provides a more user-friendly experience.
* **Modularity:** The code is organized into a single function (`order_coffee`), which makes it easier to understand and maintain.
* **Comments:** Includes comments to explain the purpose of different parts of the code.
* **Readability:** The code is formatted consistently and uses meaningful variable names to improve readability.
* **Robustness:** The `!["small", "medium", "large"].include?(size)` check provides a more reliable way to validate the size input.
* **Clear Confirmation:** The confirmation message summarizes the order clearly.
How to run the code:
1. **Save:** Save the code as a `.rb` file (e.g., `coffee_order.rb`).
2. **Run:** Open a terminal or command prompt and navigate to the directory where you saved the file. Then, run the program using the command: `ruby coffee_order.rb`
The program will then prompt you with the dialogue steps, and you can interact with it in the terminal. This example provides a good foundation for more complex scenario-based dialogues. You can expand it by adding more options, more complex interactions, and even simulating the preparation of the coffee.
👁️ Viewed: 4
Comments