Class: BurgerGame
- Inherits:
-
Object
- Object
- BurgerGame
- Defined in:
- lib/burger_game.rb
Instance Method Summary collapse
-
#initialize(*args) ⇒ BurgerGame
constructor
A new instance of BurgerGame.
Constructor Details
#initialize(*args) ⇒ BurgerGame
Returns a new instance of BurgerGame.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/burger_game.rb', line 13 def initialize(*args) # Initialise game_state = GameState.new = Recipe.new no_of_recipe = Recipe.no_of_recipe screen = ScreenMessage.new = PlayerOption.new customer = CustomerRequest.new no_of_customer = CustomerRequest.no_of_customer = OpenStruct.new # Handle command line argument opt_parser = OptionParser.new do |opt| opt. = "Usage (Gem's executable): start_burger_game [OPTION]\nOR\nUsage (bash script - install game): install.sh\nUsage (bash script - run game): burger_game.sh [OPTION]\n\n" opt.on("-h", "--help", "Print this Help menu for Burger Game.") do |arg| puts opt exit end opt.on("-m", "--money [TARGET_MONEY]", screen.display_h_money) { |arg| .target_money = arg } opt.on("-r", "--reputation [MAX_REPUTATION]", screen.display_h_reputation) { |arg| .max_reputation = arg } end # ERROR HANDLING for command line argument begin opt_parser.parse!(args) rescue OptionParser::InvalidOption => e puts "You have entered an invalid option. Please check the available options in our Help menu '-h' or '--help'." puts e. exit rescue OptionParser::MissingArgument => e puts "You have not entered the argument for your option." puts e. exit rescue OptionParser::ParseError => e puts "Error when parsing argument." puts e. exit rescue => e puts "Something went wrong." puts "Error message: " + e. exit end if (.target_money) if ((.target_money.to_i >= 10 ) && (.target_money.to_i <= 99 )) puts "Change TARGET_MONEY to: $#{.target_money}.00." # Set target money in GameState game_state.set_target_money(.target_money.to_f) else puts screen.display_invalid("for TARGET_MONEY.") exit end end if (.max_reputation) if ((.max_reputation.to_i >= 1 ) && (.max_reputation.to_i <= 10 )) puts "Change MAX_REPUTATION to: #{.max_reputation}." # Set max reputation in GameState game_state.set_max_reputation(.max_reputation.to_i) else puts screen.display_invalid("for MAX_REPUTATION.") exit end end # Ask user if they want to launch the game or exit puts launch_game = .launch_game # Exit command line if user select Exit exit if launch_game === false # Show welcome message puts puts screen.display_welcome puts screen.go_to_next # Feature 1: Options to see instructions or to start the game loop do puts start_game = .start_game break if start_game === true # Show instructions puts puts screen.display_instructions puts screen.go_to_next end # Show prologue puts puts screen.display_prologue puts screen.go_to_next # Loop game until WIN / GAME OVER loop do # Display current money and reputation status puts game_state.display_game_state puts screen.go_to_next # Feature 2: Formatted display for showing shop's menu puts puts "Ruby Burger's Menu" puts puts puts "We have #{no_of_recipe} recipes. Try to remember the recipe name, the stack order of ingredients and the quantity. We will build the burger from bottom to top." puts # Loop to display all recipes i = 0 loop do puts .display_recipe(i) puts screen.go_to_next i += 1 break if i > (no_of_recipe - 1) end # Feature 3: Randomised customers with set of request (and associated preferences) and responses # Display customer request puts puts "There is a customer in the queue..." puts # Randomise customer customer_no = rand(no_of_customer) puts customer.display_request(customer_no) puts puts screen.go_to_next ARGV.clear # Feature 4: Selectable options for list of ingredients, so no manual entry (typing) is needed. # Quantity input as integer within a pre-set range. # Display player's options player_recipe = .get_selection customer_recipe = customer.get_request(customer_no) # Feature 5: Score calculation based on customer's request and preferences compared to player's input # Calculate score compare = ScoreComparison.new(player_recipe, customer_recipe) score = compare.get_score mood = compare.get_mood(score) # Feautre 6: Get customers' responses from a JSON file # Display customer's response puts puts "The customer wants to say something..." puts puts customer.display_response(customer_no, mood) puts screen.go_to_next puts # Update game state compare.calculate_state(mood) # Feature 7: Lose/win criteria based on reputation and money # GAME OVER condition if GameState.current_reputation == 0 puts screen.display_game_over puts break end # WIN condition if GameState.current_money >= GameState.target_money puts screen.display_win puts break end end end |