Sunday, 25 May 2025

New game of the Year Rationz

# Rations Game in Ruby class RationsGame attr_accessor :rations, :day, :max_days def initialize @rations = 10 # Starting amount of rations @day = 1 @max_days = 30 # Optional: set a max number of days to survive end def start puts "Welcome to Rations!" puts "You start with #{@rations} rations." puts "Survive as many days as you can." game_loop end def game_loop while @rations > 0 puts "\n--- Day #{@day} ---" show_status action = get_player_action process_action(action) check_game_over @day += 1 end end def show_status puts "You have #{@rations} rations remaining." end def get_player_action puts "Choose an action:" puts "1. Eat 1 ration" puts "2. Save rations for later" print "Enter your choice (1 or 2): " choice = gets.chomp until ["1", "2"].include?(choice) print "Invalid choice. Please enter 1 or 2: " choice = gets.chomp end choice end def process_action(action) case action when "1" eat_ration when "2" skip_eating end end def eat_ration if @rations > 0 @rations -= 1 puts "You ate 1 ration. Rations left: #{@rations}." else puts "You have no rations left to eat!" end end def skip_eating puts "You chose not to eat today." end def check_game_over if @rations <= 0 puts "\nYou have run out of rations. Game over!" puts "You survived #{@day} days." elsif @day >= @max_days puts "\nCongratulations! You survived #{@max_days} days!" end end end # Run the game game = RationsGame.new game.start

No comments:

Post a Comment