Tuesday, December 8, 2015

A Day at the Races...Finally

Well the day has come. I finally finished the first lab in the C# book. The book reads that we should know everything you need to in order to complete the lab. I found there were a few things that either I glossed over or the book did not touch on the subject. That was moving pictures in the Windows Form. A quick Google search (yes I used Google in the lab) helped me understand that I needed to find the location of the picture and then I change the location by changing one of the points (X axis or Y axis). I then joked with my cousin (who is a much better coder than I am) that I didn't know how to move the dogs and he said to use dog.Move();. Great idea I said, except they returned dog.NotListen();.

Here is the opening screen of the wonderful Day at the Races app.
The dogs and the race track are 5 pictures and, as discussed above, the key was figuring out how to get the dogs move by changing the point on one of the axis. That movement is supposed to be random. The randomness comes between zero and four spaces forward (I decided to add 0 to try and make it more random). Well, it is random, but the races are always very close. You have to stay on the edge of your seat in anticipation.

And don't worry, you can see where the dogs have been! They leave a trail of...let's call it droppings. I tried a few different sets of code in the Run() object to fix this issue. I found that I was reliably able to get all the dogs to run at the same speed and finish at the same time, except it always had dog number one win. Hey, you could make a lot of money betting on number one! I found that if I used Application.DoEvents(); the droppings don't appear anymore, but the races get stuck in an infinite loop. Then if I add a break; the loop is broken, but all the dogs finish at the same time. I am thinking it maybe has to do with the order which I have the code written, but I tried rearranging and in some cases made it a little better, but generally made things worse.

This was the first project in the book that required multiple classes in order to run. I have a guy, greyhound, and bet class. It was fun trying to instantiate an instance of bet for the guy class (or is that backwards?). I have instantiated an instance or two at work, but I have not fully understood what or why I was doing it. I feel that having walked through having to write a  program that uses an instance of another class helped me understand these much better.

In the end I wasn't given much direction on how to write the code, just a skeleton frame. I added a few objects that weren't in the original skeleton I was given. I hope it was needed and that I am not just adding objects that aren't needed (I will say I used all the objects somewhere in the code).

I want to include the SLN so that if anyone wants to take a look and provide constructive feedback they can. This is the first time I have ever shared code, so I hope I included everything that is needed. If it matters I have the solution saved at C:\Source\Personal\CSharpBook\labADayAtTheRaces (maybe it matters for where it will look for the dog and racetrack pictures). Here is the code and sln files (code was written in Visual Studio 2015).

Well, here is the Day at the Races (don't worry, no actually money is exchanged so it is legal in every state). Have fun playing with Joe, Bob, and Al's money. Happy betting!

12/8/2015 Update - I was able to fix the dog droppings. The pictures were not not refreshing so each picture was left behind leaving a trail of the back of the dog legs. All it took was talking to my team lead about the issue and he said, "Try this.Refresh();" Easy fix. No more trails. (I have updated the link above).

3 comments:

K.C. said...

I liked the poo version better

Robert Jorgensen said...

I could post it again for you, K.C.!

Robert Jorgensen said...

For K.C. https://drive.google.com/file/d/0B_gdRyqtrsL6dTNNbWJtMVdnR2c/view?usp=sharing