Day 26, part 1: Getting in Some Coding in the Morning

I thought I would get some studying in before breakfast. (Note: I thought I published this earlier but guess not!) Let’s get to it:

Exercise 30: Else and If

This is now my third time doing if, elsif and else statements. It is starting to sink in!

and the results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should take the cars.
Maybe we could take the trucks.
Alright, let’s just take the trucks.

1. Try to guess what elsif and else are doing.

It does different branches in the code. It provides different options depending on if the criteria is met.

2. Change the numbers of cars, people, and trucks and then trace through each if-statement to see what will be printed.

So, I switched the numbers around:

And here is the result, with the old results in italics:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should take the cars.
Maybe we could take the trucks.
Alright, let’s just take the trucks.

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should not take the cars.
That’s too many trucks.
Alright, let’s just take the trucks.

3. Try some more complex boolean expressions like cars > people || trucks < cars.

OK. Here is the code:

people = 60
cars = 52
trucks = 50

if cars > people
puts “We should take the cars.”
elsif cars < people puts "We should not take the cars." else puts "We can't decide." end if trucks > cars
puts “That’s too many trucks.”
elsif trucks < cars puts "Maybe we could take the trucks." else puts "We still can't decide." end if cars < people && trucks < people puts "Blame management." else puts "Fine, let's stay home them." end and the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should not take the cars.
Maybe we could take the trucks.
Blame management.

4. Above each line write an English description of what the line does.

Exercise 31: Making Decisions

OMG we are writing an adventure! And Cthulhu!

And a bunch of results because there is a lot here.

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There’s a giant bear eating a cheese cake. What do you do?
1. Take the cake.
2. Scream at the bear.
> 1
The bear eats your face off. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There’s a giant bear eating a cheese cake. What do you do?
1. Take the cake.
2. Scream at the bear.
> 2
The bear eats your legs off. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There’s a giant bear eating a cheese cake. What do you do?
1. Take the cake.
2. Scream at the bear.
> 3
Well, doing 3 is probably better. Bear runs away.
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu’s retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 1
Your body survives powered by a mind of jello. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu’s retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 2
Your body survives powered by a mind of jello. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu’s retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 3
The insanity rots your eyes into a pool of muck. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 3
You stumble around and fall on a knife and die. Good job!

Study Drills:

1. Make new parts of the game and change what decisions people can make. Expand the game out as much as you can before it gets ridiculous.
2. Write a completely new game. Maybe you don’t like this one, so make your own. This is your computer, do what you want.

I did 1. but 2. will take some work. I really want to do this, however! Let’s add another door to the adventure!

And the results are:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 1
The clown introduces you to his good buddy Abe the Axe. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 2
You reach down to get the clown repellent but realize too late you are not Batman! Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 3
You turn to run and see that Cthulu came to visit Bozo the clown. You sink to your knees in terror. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 6
Really, there is no good option, even 6 is bad.

And that is it until later. I want to get through