Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

c#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mide - 22 May 2008 20:24 GMT
how do I create in c# sharp a program for car racer
Peter Duniho - 22 May 2008 20:40 GMT
> how do I create in c# sharp a program for car racer

Here you go:

using System

namespace Mazda
{
    static void Main()
    {
        while (!Console.KeyAvailable)
        {
            Console.Write("Vroom");
        }

        Console.Write("Checkered Flag!");
    }
}

If that's not sufficient for your needs, may I suggest expressing your  
question in a more specific way?  You might try a more specific "Subject:"  
field for your message too.

Pete
sloan - 22 May 2008 20:59 GMT
A small bug fix :

Console.Write("Vroom,Vroom,Vroom");

But other than that, you really nailed the requirements document.  Good
work!

>> how do I create in c# sharp a program for car racer
>
[quoted text clipped - 20 lines]
>
> Pete
Peter Duniho - 22 May 2008 21:03 GMT
> A small bug fix :
>
> Console.Write("Vroom,Vroom,Vroom");

Did you try running it?  You'd be hard-pressed to not get the necessary  
three "Vrooms" before terminating the execution.  :p  (Besides, as we  
should all know very well, as few as two "Vrooms" is acceptable,  
especially if whispered).

> But other than that, you really nailed the requirements document.  Good
> work!

Thank you.  I aim to please.  :)

Pete
Family Tree Mike - 23 May 2008 03:28 GMT
A one car race seems very boring to me...  Obviously each car should be in
it's own thread and fight for a shared resource (aka, a race condition :)

> > A small bug fix :
> >
[quoted text clipped - 11 lines]
>
> Pete
Peter Duniho - 23 May 2008 04:12 GMT
> A one car race seems very boring to me...  Obviously each car should be  
> in
> it's own thread and fight for a shared resource (aka, a race condition :)

Very good point!  I acknowledge your superior design skills, and hereby  
submit my conciliatory modification to my original proposal (with not one,  
but _two_ race conditions, both of which are genuine bugs :) ):

using System;
using System.Threading;

namespace Mazda
{
    static class Program
    {
        static volatile bool _fMoorv;
        static volatile bool _fFinished;
        static volatile bool _fWinner;

        static void ThreadProc(object objParm)
        {
            while (!_fFinished)
            {
                Console.Write(_fMoorv ? "moorV" : "Vroom");
                _fMoorv = !_fMoorv;
            }

            if (!_fWinner)
            {
                _fWinner = true;
                Console.Write("Car " + objParm.ToString() + " wins!");
            }
        }

        static void Main()
        {
            for (int icar = 0; icar < 3; icar++)
            {
                new Thread(ThreadProc).Start(icar);
            }

            Console.ReadKey();

            Console.Write("Checkered Flag!");

            _fFinished = true;

            Console.ReadKey();
        }
    }
}

Oh, and as an added bonus, this code sample will (probably) actually  
compile.  :)
Marc Gravell - 23 May 2008 08:11 GMT
I dunno, that ParameterizedThreadStart doesn't give you much type-
safety (objParm can't be verified at compile), so as a
"fix" (cough)... plus it starts the "race" earlier, and now the
competition can be won by a car that wasn't even competing!

               ThreadStart start = delegate { ThreadProc(icar); };
               new Thread(start).Start();
Marc
Peter Duniho - 23 May 2008 18:20 GMT
> I dunno, that ParameterizedThreadStart doesn't give you much type-
> safety (objParm can't be verified at compile),

Hey, I know the drivers wear helmets and fire suits, but no one ever said  
racing was _safe_.  :p

> so as a
> "fix" (cough)... plus it starts the "race" earlier, and now the
> competition can be won by a car that wasn't even competing!

Yes, I do like that your code has the possibility that all the cars that  
start will be #4, with 0, 1, and 2 left out of the race entirely.  :)  I'm  
not sure I see how it starts the race "earlier" though.

Pete
Marc Gravell - 23 May 2008 22:51 GMT
> not sure I see how it starts the race "earlier" though.

Because the (thread) race can happen before any of the cars even start
their engines ;-p
With the original code I can see that multiple cars can win, and I can
see  some randomness in the Vroom / moorV, but neither can happen
until at least 2 cars are moving ;-p

Marc
Peter Duniho - 24 May 2008 00:18 GMT
>> not sure I see how it starts the race "earlier" though.
>
[quoted text clipped - 3 lines]
> see  some randomness in the Vroom / moorV, but neither can happen
> until at least 2 cars are moving ;-p

How does your proposal change the behavior of the original code?  With  
either code, as near as I can tell, you can have a winner with only one  
car "moving", and you only get "randomness" with two.  How does calling  
the ThreadProc method through an anonymous method rather than directly via  
a delegate make things different?

And by "I can see" are you telling me that you actually compiled and ran  
the code?  It never occurred to me to try that.  :p

Pete
Marc Gravell - 24 May 2008 09:48 GMT
No - with the race condition introduced by the captured variable, you
can get randomness with one car - because it can start as either car 0
or car 1 [in reality I wouldn't expect to see many 0s, but it is
possible]

And the "I can see" was based on looking at the code, not running
it... funny thing about race conditions is that they are a pain to
reproduce on demand ;-p

I guess you could say that the captured variable changes which cars
make it to the starting grid, where-as the bools affect the in-race
behavior. Just thought - you could make it even more interesting* by
removing the volatile - then it is up to the compiler whether it still
has the bool cached...

*=for small values of interesting

I'm thinking we're probably taking this too seriously, though ;-p

Marc
Peter Duniho - 24 May 2008 18:08 GMT
> No - with the race condition introduced by the captured variable, you
> can get randomness with one car - because it can start as either car 0
> or car 1 [in reality I wouldn't expect to see many 0s, but it is
> possible]

Ahh...I see.  We have a terminology disconnect.  I was viewing the number  
passed as the stencil put on the side of a given car (which would possibly  
then be erroneous for one or more cars...and most likely would be :) ),  
with the thread representing the car.  You are viewing the number as the  
car itself.

Now everything makes sense.

> [...]
> I'm thinking we're probably taking this too seriously, though ;-p

"Too seriously" for what?  What good is a joke if you can't beat it to  
death?  :)

Pete
Marc Gravell - 25 May 2008 09:15 GMT
> "Too seriously" for what?  What good is a joke if you can't beat it to  
> death?  :)

;-p
qglyirnyfgfo@mailinator.com - 22 May 2008 21:59 GMT
LOL

And here I am thinking the nerds don’t have a sense of humor :)

On May 22, 2:40 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > how do I create in c# sharp a program for car racer
>
[quoted text clipped - 21 lines]
>
> Pete
sloan - 22 May 2008 23:51 GMT
I think I dropped a pretty good one here:

http://groups.google.com/group/microsoft.public.dotnet.general/msg/3af3ed8543d2756c

talking about "resume next"

LOL

And here I am thinking the nerds don’t have a sense of humor :)

On May 22, 2:40 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > how do I create in c# sharp a program for car racer
>
[quoted text clipped - 21 lines]
>
> Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.