November 22nd, 2010, 11:35 AM
Help with game, just starting
My boss told me to pick up a ruby book and start figuring, so he gave me this assignment to write a small game:
He sent me an email with:
> construct an array of cars,
> accept entry of two cars,
> race the two cars,
> determine and display a winner
> #each car is [name, horsepower, weight] cars = [
> ['mustang', 300, 3480 ],
> ['camaro', 400, 3780 ],
> ['s2000', 237, 2864 ],
> ['350z', 287, 3288 ]
> run it giving two car names on the command line, example:
> ruby race.rb mustang camaro
> Use ARGV as the name of the first car, and ARGV as the name of
> the second car, as follows:
> name1 = ARGV || 'mustang'
> name2 = ARGV || 'camaro'
> Locate both cars in the array, compare their performance numbers, and
> pick a winner.
So how on earth do i do this???? Please help!!
November 22nd, 2010, 12:59 PM
The only thing not spelled out in that description is the equation of speed as it relates to weight and horsepower. What exactly have you tried?
I should warn you that as an employee you are expected to think - that is what a portion of your paycheck is intended to represent. If I had given you this problem to work out I would have a bank of questions intended to determine if you understood what you just did. I would want to know what roadblocks you ran into (however trivial) and how you overcame them. You farming an answer sets you up for a hard failure in that respect. Maybe your employer wont care how you got your answer - I wouldn't bank on that, however.
True happiness is not getting what you want, it's wanting what you've already got.
November 23rd, 2010, 05:30 PM
He's already given you most of the code.
#each car is [name, horsepower, weight]
cars = [
['mustang', 300, 3480 ],
['camaro', 400, 3780 ],
['s2000', 237, 2864 ],
['350z', 287, 3288 ]
name1 = ARGV || 'mustang'
name2 = ARGV || 'camaro'
That's a pretty good start. The next step is to match the correct car with the name
car1 = *your code here*
I'd recommend learning about accessing Array elements with  and then look into the detect method on the Array class.
Once that's done, figure out how to compare the speed.
I was able to create a working version with just a few lines of code beyond what he gave you.