Created
December 17, 2016 19:08
-
-
Save kossgreim/71cfd61197e1d6fb5b5e0d71792195ca to your computer and use it in GitHub Desktop.
Chatar ukol Programovani 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Chatar | |
ACTIONS = { | |
1 => :cut_trees, | |
2 => :cut_logs, | |
3 => :sleep, | |
4 => :end_game | |
} | |
def initialize | |
@days = 0 | |
@logs = 0 | |
@firewood = 3 | |
end | |
def greetings | |
puts "Stal ses chatarem v roce 1526. zkus prezit!" | |
end | |
def play! | |
game_info | |
user_input = get_user_input | |
if ACTIONS.has_key?(user_input) | |
self.send(ACTIONS[user_input]) | |
next_day | |
else | |
puts 'Neplatny vstup' | |
_ | |
end | |
end | |
protected | |
def game_info | |
puts "\n\n" | |
puts "Je #{@days}. den. V kulne mas #{@logs} klad a #{@firewood} polen." | |
puts "\n" | |
puts 'Co udelas?' | |
puts '1. Pokacet strom' | |
puts '2. Narezat kladu na polena' | |
puts '3. Jit spat' | |
puts '4. Ukoncit simulaci' | |
puts "\n" | |
puts "Tva volba:" | |
end | |
def sleep | |
_ | |
puts "Sel jsi spat … chrrr chrrr chrrr" | |
end | |
def cut_trees | |
_ | |
cut_tmp = rand(2..4) | |
puts "Sel si na drevo, pokacel si #{cut_tmp} stromy, mas z nich #{cut_tmp} klady" | |
add_logs(cut_tmp) | |
end | |
def cut_logs | |
# raising an exception when we don't have logs to cut | |
if @logs > 0 | |
# take logs | |
taken_logs = rand(2..5) | |
taken_logs = @logs if taken_logs > @logs | |
# cut logs to firewood | |
new_firewood = taken_logs * 2 | |
take_logs(taken_logs) | |
add_firewood(new_firewood) | |
# output result to a player | |
_ | |
puts "Narezal jsi #{taken_logs} klad na #{new_firewood} polen" | |
else | |
_ | |
puts 'namas klady' | |
end | |
end | |
def use_firewood | |
# raise an exception when user has no firewood to burn | |
raise '........umrzl' if @firewood == 0 | |
@firewood -= 1 | |
end | |
def end_game | |
# just ending the game | |
_ | |
raise 'Konec simulace' | |
end | |
def add_logs(logs) | |
@logs += logs | |
end | |
def add_firewood(firewood) | |
@firewood += firewood | |
end | |
def take_logs(logs) | |
@logs -= logs | |
end | |
def next_day | |
@days += 1 | |
use_firewood | |
end | |
def get_user_input | |
gets.strip.to_i | |
end | |
def _(times = 70) | |
puts '-' * times.to_i | |
end | |
end | |
# game | |
logger = Chatar.new | |
logger.greetings | |
# let's play | |
loop do | |
begin | |
logger.play! | |
rescue Exception => e | |
puts e.message | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment