A blog about software by Alexander Yaäkov Garber.

Object Oriented Programming: The Tree House Castle

Learn Ruby the Hard Way: Exercise 43

From the book:
I’m going to describe a process to use when you want to build something using Ruby, specifically with object-oriented programming (OOP). What I mean by a ”process” is that I’ll give you a set of steps that you do in order, but that you aren’t meant to be a slave to or that will totally always work for every problem. They are just a good starting point for many programming problems and shouldn’t be considered the only way to solve these types of problems. This process is just one way to do it that you can follow.
The process is as follows: 
1. Write or draw about the problem.
2. Extract key concepts from 1 and research them.
3. Create a class hierarchy and object map for the concepts.
4. Code the classes and a test to run them.
5. Repeat and refine.

Step One: Write about the Problem

A Fairy Tale About a Tree House, A Prince, and a Princess

The Tree Castle / A.J. Garber
Once upon a time there was a wandering prince who had no castle to call his home.  To be fair, he was not the son of a king, nor an heir to a throne, but this did not altogether rule out his royal ambitions, although it did put something of a damper on them.
He wore as a pendant an emerald ring made of silver, which bore the inscription 'Mano Karalienė'.  He didn't usually care much for pretty things, but he felt he might need it one day. 
At any rate, he had found a plot of land in the middle of a meadow, not far from a river, and a strong pair of hands, patience, and time.  He felled trees, cut timber, planted studs, built up walls, and eventually put together for himself a rudimentary castle.  Well, maybe not a castle per se, more of a house, a hovel, if you will -- but still better than sleeping without a roof over one's head! 
It had a door and a rough hole that could be covered when it rained, and that was better than nothing, and after all, one has to start somewhere, right?
The river flowed, the moons waxed and waned, and the hopeful prince's castle-in-the-making grew and branched out.  And by 'branched out' I mean the prince accidentally dropped a magical seed on the dirt floor and spilled some tea on it a few days later. 
Within a few days, a large tree sprang up, broke through the thatched roof, and branched out over the house, like a rather uninvited garden umbrella.
Not to be discouraged, the prince used the heretofore unforeseen central column as a scaffold for various extensions to his castle-in-the-making.  He had always wanted to build up from the ground floor, and a rapidly growing, sturdy tree in what had been his living room gave him exactly what he needed to work with.  As the tree grew taller and wider, the flexibly defined castle-in-the-making grew up and out around it. 
It should come as no surprise that the prince became thoroughly absorbed in the ever on-going construction of his castle-in-the-making.  There was always another room to build, another branch to work an extension from, some improvement that could be made somewhere.  And, to be fair, the humble shack grew into a most impressive albeit unorthodox dwelling. 
And yet, the prince felt keenly the want of something in his abode.
Fortifications? Underway.
Decorations? Unnecessary.
A kingdom to rule over? Perhaps, although who was to say the prince's domain was insufficient in and of itself?
He was fond of his patch in the meadow by the river, and had a castle-in-the-making to defend it from... But no-one to rule over it with.  That was problem. 
'A prince needs a princess as a house needs a tree,' he decided and went to the highest room amidst the upper-most branches to ponder the matter.  He ought to find himself a princess, he reasoned.  Not the sort of princess accompanied by handmaids and livery that attracted knights in shining armour as suitors: such a princess would not appreciate the irregularity of his castle-in-the-making, or the paucity of furniture, or the animals that sometimes invited themselves in.  Also, aristocratic accessories such as horses and armour cost a fortune, and he preferred getting about on his own two feet with a simple knapsack thank-you-very-much.  But then again, princesses seemed to go in for things that glitter, shine, glisten, prance and neigh.  What if there were a princess who preferred sitting on a tree branch on a sunny day, enjoying a gentle breeze and sunlight speckled through the leaves?  There wasn't anything better than that, except perhaps taking an afternoon nap on his large sleeping cushion with the sunlight on his back.  Surely there was a princess out there who could appreciate that. 
As the prince contemplated his solitary reign, a beautiful young princess crossed the meadow and came up to his castle-in-the-making.  She had long dark braids and perfect almond chestnut eyes.  She had no livery or handmaids, but was proud of a stout leather backpack and a durable pair of waterproof boots that she could even wade through a shallow stream in without getting her feet wet. 
'This must be the most ridiculous tree-house I have ever seen,' she said to herself as she looked up at the hodge-podge of rooms leaning out like drunkards from the trunk.  'It does look like a fun place to live in, though.' 
The princess knocked on the front door, but the prince was lost in thought at the top of the arboresque tower.  The princess entered and ascended the spiral staircase around the trunk.  She was alternately delighted and dismayed, amused and bemused by the eclectic, haphazard arrangements; as she explored, she divined the intimate relationship of the builder to his environment and wondered to herself what sort of idiosyncrasy was composed of creativity, recklessness, attentiveness, and absentmindedness in such large doses.  She reasoned that, if nothing else, the creator of this place -- whatever it was -- had passion and energy. 
She found what seemed a bedroom without a bed, and weary from her exploration, curled up on a large floor cushion.  The afternoon sunlight, filtered by the leaves outside the window, provided a gentle sheet of warmth to her back, and the princess quickly fell asleep. 
The prince entered the bedroom distracted by his day's unfruitful ponderings and nearly fell over when he saw the princess on his cushion.  After some hesitation he sat down on the floor before her and watched her softly parted lips and admired her fair cheeks.  She snored gently, in the most fetching way, and drooled slightly on his cushion.  He took out a handkerchief and wiped the cushion -- it was no bother, he didn't mind at all. 
The prince grew relaxed and then sleepy, and stretched out on the floor.
Some time later, the princess awoke to see a beautiful man on the floor beside her.  His fingers were blotched with ink, and there were leaves in his curly hair which cascaded across his eyes and glittered in the late afternoon sunlight.  She admired his handsome, glowing, freckled face and knelt down to look at him more closely.  Then she lay down next to him, face to face, and put her hands in his. 
The following morning, they opened their eyes to each other.
'Art thou an angel?' He asked her.
'Nay, a princess.  Art thou thyself an angel?'
'Nay, but a prince.'
'Is this thy castle?'
'A castle-in-the-making.'
'It pleaseth me.'
They fell silent.
The prince reached remembered his emerald ring pendant, and showed it to her.  'I think this ring should belong to thee.'
The princess read the inscription and smiled.  'Yes, I am.'
He placed the ring on her finger.
'We are not, then, in a dream?' He ventured.
'A dream come true, methinks.' She replied.
'If that be so, then thou art my queen.'
'And thou in that case art my king.'
And the queen placed her lips on his, and sealed their marriage with a kiss.

Step Two: Extract Key Concepts and Research Them

Nouns

Princess (You)
Meadow
River
Tree house castle
Front door
Riddle
Door lock
Entrance
Spiral staircase
Machine room
Kitchen
Linux sandwich maker
Sandwich
Library
Talking books
Bedroom
Floor cushion
Prince
Conversation (acquaintence)
Ring
Inscription
Translation
Conversation (engagement)
Conclusion (marriage)

Verbs

Go, Walk (river, tree house castle)
Drink water (river)
Cross (river)
Knock (front door)
Read (riddle)
Unlock (front door)
Enter (entrance, machine room, kitchen, library, bedroom)
Exit (entrance, machine room, etc.)
Ascend (spiral staircase)
Pick up (gadget, talking book, etc.)
Look at (gadget, talking book, etc.)
Lie down (floor cushion, floor)
Translate (inscription)
Kiss (prince)

Status

Prince in the room (bedroom)
Prince not in the room (bedroom)

Step Three: Create a Class Hierarchy and Object Map for the Concepts

Create a Class Hierarchy


Map
  - next_scene
  - opening_scene
Engine
  - play
Scene
  - enter_scene
  - exit_scene
* Meadow
* River bank
* Entrance
* Spiral staircase
* Machine room
* Kitchen
* Library
* Bedroom

Create an Object Map


Step Four: Code the Classes and a Test To Run Them


Step Five: Repeat and Refine