Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 22nd, 2010, 12:35 PM
 cbeyond12
Registered User

Join Date: Nov 2010
Posts: 1
Time spent in forums: 12 m 29 sec
Reputation Power: 0
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:

> race.rb
> construct an array of cars,
> accept entry of two cars,
> race the two cars,
> determine and display a winner
>
> example:
>
> #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[0] as the name of the first car, and ARGV[1] as the name of
> the second car, as follows:
> name1 = ARGV[0] || 'mustang'
> name2 = ARGV[1] || 'camaro'
>
> Locate both cars in the array, compare their performance numbers, and
> pick a winner.
>
>

#2
November 22nd, 2010, 01:59 PM
 L7Sqr
Contributing User

Join Date: Jan 2004
Location: Constant Limbo
Posts: 989
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
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.

My Blog

#3
November 23rd, 2010, 06:30 PM
Registered User

Join Date: Nov 2010
Posts: 7
Time spent in forums: 1 h 24 m 16 sec
Reputation Power: 0
He's already given you most of the code.

ruby Code:
 Original - ruby Code
```
# race.rb

#each car is [name, horsepower, weight]
cars = [
['mustang', 300, 3480 ],
['camaro', 400, 3780 ],
['s2000', 237, 2864 ],
['350z', 287, 3288 ]
]

name1 = ARGV[0] || 'mustang'
name2 = ARGV[1] || 'camaro'
```

That's a pretty good start. The next step is to match the correct car with the name

ruby Code:
 Original - ruby Code
```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.

 Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Help with game, just starting