0

I'm making a simple command line game with Ruby and I'm having trouble saving some information without a database/HTTP dynamic.

For example, let's say I have to make a sandwich (in the game). I am presented with an array of ingredients to choose from, like so:

[1] Carrot
[2] Banana
[3] Cheese
[4] Tomato

I cannot hardcode a direct correspondence between number and ingredient because, before that, I was forbidden to use a couple of ingredients, at random (so the complete ingredients array is two items longer). And I don't want to present a list numbered like [1] [2] [4] [6] because it gets confusing.

What I'm doing right now is hardcoding a direct correspondence between a letter and an item, so for Banana press B, for Cheese press C and so on. But it's less than ideal, particularly because this is a pattern used throughout the game, and in some contexts it will get very inconvenient, both for me and the player.

So, is there a better way for me to do this? How can I associate an input with an item of a list that is generated randomly, and also save that information for further use down the line)?

San Diago
  • 1,030
  • 1
  • 12
  • 26
  • a hardcoded hash at the start of the game...? ie. {'B' => 'Banana', 'C' => 'Cheese'} – Matthew Sep 03 '15 at 20:41
  • But then, there will be conflicts in keys. e.g. `C` for both Carrot and Cheese. – K M Rakibul Islam Sep 03 '15 at 20:42
  • That's what I'm doing right now, using hashes to hardcode direct correspondences between input and data. I'd like to be able to just present the data first and create a correspondence, save the data, once the input happens, the way it's usually done in a web environment. – San Diago Sep 03 '15 at 20:49
  • Why can't you use a database? SQLite is an excellent candidate for a small, fast database for a game. It'd help avoid someone messing with the input data also. You can easily use an ORM, like Sequel, to talk to it. Otherwise use a YAML file. – the Tin Man Sep 03 '15 at 21:05
  • 1
    So you have the menu as an array and you can use the number next to each ingredient (actually number - 1) as the index to access the ingredient in the array. I don't see the problem. What I'm missing? – Mario Zannone Sep 03 '15 at 21:29
  • Do you want the keymap to always changed based on the array/hash of ingredients? Why not define a keymap first (e.g. q, w, e, r) and then map those key events to an index? – Snarf Sep 03 '15 at 23:13

1 Answers1

0

Here's how I solved it:

Mario Zannone's comment made me realize I could use the index of the array elements as an id, whereas I had been looking at the whole thing as if it was sort of just text.

So here's the code I came up with to take advantage of that:

(0...@ingredients.length).each do |i|
    puts "[#{i+1}] #{@ingredients[i]}"
end

That way I now have a direct correspondence between element and input with:

choice = gets.chomp.to_i - 1
@selected_ingredient = @ingredients[choice]
Community
  • 1
  • 1
San Diago
  • 1,030
  • 1
  • 12
  • 26