Code.Blog

Code Talk for the Application/Game Developer

  Home  |   Contact  |   Syndication    |   Login
  80 Posts | 0 Stories | 19 Comments | 0 Trackbacks

News

View Matt Christian's profile on LinkedIn

Archives

My Sites

Monday, August 18, 2008 #

PongRPG, my first XNA game, is finally available for download on my website here.  There may still be some bugs in it but hopefully it's nothing that makes the game break (if you find anything let me know).  The game is for PC and unfortunately will require a full install of XNA GS (which means you either need to have Visual Studio 2005 installed, or you'll need to install Visual Studio C# Express Edition, sorry!).  If you have time please check it out and let me know what you think.

Recently I made a very simple Random Number Generator (only took a few hours).  The purpose was to build a program for a football pool and the user needed a program that would have two types of functionality:

  1. Need 10 random numbers (0-9) and allow repeating digits, but digits cannot be next to each other.  The output should be put into a comma delimited file.  This needs to be repeated 38 times.
  2. Need 10 random numbers (0-9) but do not allow repeating digits.  Output to a comma delimited file.  This should be repeated 2 times.

Of course, I could make a basic, statically-coded, console app but I chose instead (for fun) to build it into a Win32 app in VB.  Here's a quick screen of the program:

I accept a low number and high number (in these cases, 0 for low and 9 for high), as well as how many rows and columns.  Columns are the count of numbers you need (in this case, 10 numbers) and rows are how many times the program should repeat (in case 1, 38; in case 2, 2).  Next, a check box can be used to prevent repeating numbers, as needed in example 2.  Then, you can browse to a file or use the Save File dialogue that opens when Browse.. is clicked, to determine the file being outputted to.  Since the text box is ReadOnly, a clear button clears the box.  Finally, there's a Generate button (which obviously runs the generator) and an output window that displays the numbers that are generated (which is space-delimited for readability).

EDIT: I've removed the base code because the blog erased almost all of it.  I'll post the source on my site later and link it.

The first GenerateRandomNumber (the one which accpets no arguments) is used for the second version of this program, the one requiring no repeating digits.  The second version of this method (the one accepting arguments) simply returns a random number.  Now what is very important is that while the method receiving arguments returns the random number to be used, the one accepting arguments DOES NOT!  It actually returns a random number to be selected in a list.

Private Function GenerateRandomNumber(ByVal iLow As Integer, ByVal iHigh As Integer) As Integer
'Randomize number
Return CInt((iHigh - iLow) * Rnd()) + iLow
End Function

The Method

For the first version of this program, all I need to do is grab a random number, and if that number is the same as the previous, re-generate a new number and check it again.  This is done above in a while statement.

For the second method, I initially created a list to store the numbers in, then each time I would generate a number, check to see if it was in the list, and if it was I would re-generate a number (similar to the first way and looking at the previous number except now I have a list instead of one number).  Now an extreme problem came up, here's the basic example of that problem.  Let's assume we're on the first row and can select numbers 0-9 and require 10 numbers.  The random generator runs 8 times and in some order produces the numbers 0-8, leaving only the 9 left.  Now, the generator will loop over and over and over until it randomly guesses 9, the only remaining available number.  Maybe that doesn't seem hard, but it gives the computer a 1/10 chances to guess it.  Now repeat that 2 times, how about 38 times?

So, what I implemented was a simple list that stored the numbers from the low to high.  If you start the program and type in low as 0 and 9 as high, the program builds 2 lists, one that is completely empty, and one that is filled with each number between those values (0, 1, 2, 3, ..., 9).  Now the generate method runs and picks a position (let's say 3) and places that positions value into the blank array (now contains: 2).  Next the generate grabs number 0 (now contains: 2, 0).  Uh-oh, it again chooses 3!  But, since we removed that number from the list, position 3 is now containing the next number in the sequence, 3.  Blank array now contains 2, 0, 3.  And so on.  Then, there's an added part to the generate stating, if the array containing the numbers to choose from only has a size of 1, we only have 1 value left and it will be stored in position 0 so we can automatically assume to return position 0.

Problems

This method has a few problems though.  The most immediately concerning, is if the user decides to generate random numbers without repeating digits (method 2) but decides to create more columns than numbers.  For example, we have numbers 0-9, but the user requires 11 columns.  Our generate will run but will constantly be searching for another number and the array will run out and all sorts of messes occur.  I've included protection against this in the final code (via CheckChanged on the check box and a Leave check on the columns box).

Secondly, and I have no idea why this happens, if you run the first method and then run the second method right after, the first row has repeating digits in it.  But, if you run the second method more than once, it works fine.  I'll look into this error if I have time to revisit this program.

I'll post the code on my website later if you want to grab the source.


Friday, August 15, 2008 #

Before I blogged a lot about random junk that was just on my mind on a day to day basis, often non-technical.  Now I've stopped and want to continue to blog about technical game programming but I'm not sure what to talk about.  So, what do you want to see?


Wednesday, August 13, 2008 #

I just finished up an interview about programming at whohub.  Check it out here!

I don't know how many of you out there like writing, but I tend to love it (probably why I have a blog, write tutorials, and constantly am looking for new stuff to write).  About 2 years ago (probably closer to 3) I published my first small book through LuLu.com.

LuLu is a great site if you want to publish a work of yours without having to go through all the application processes and such that are normally required with a work being published through a publishing house.  Granted, it is a few bucks more expensive to print copies of your book (considering it is print on-demand), but well worth it when you get that shiny covered book in the mail.

I'm working on 2 other books to be published on LuLu as well as a companion CD-ROM for one of the books.  You can check out my store HERE.  Like I said, I only have one book on there and it's got loads of mistakes, but it is a free download, so if you have time check it out.  Let me know if you've got some stuff on there as well, I'd love to check out some other authors!


Tuesday, August 12, 2008 #

Recently I pulled a very bad mistake while programming/testing.  Here's the description, maybe you'll learn a thing or two.

While at work, I created an automated process that would be started when a button on a form is clicked and would then run to dynamically generate numerous text files, build an Excel file from the data in several pre-made text files, and compare that data to database values which would then populate the database.  I built the programming and got it running a few times to put the correct data in the database.  The program went to test where people were testing it so I couldn't very well be constantly continuing to change values.  I updated the program one final time and ran it against my local database (realize there are 3 databases, Local, Test, Production) and the program failed.  I figured it was just some accidental runtime exception due to a previously unclosed Excel or text file and closed the program.  Mistake 1:  Don't leave a program broken if you intend to come back to it.  Always leave code, at the least, runnable if possible.

I came back to it recently (a few weeks after initially stopping work on it) because the system is being moved to production.  I ran the program locally and the error reappeared.  I worked on it until the program ran clean, did a very very quick check of the data to make sure it wasn't Null and ran it in Test.  I only then noticed (after Test had been filled with data) that ALL of the ID's I was populating were auto-generated (I auto-generate ID's for a very small percentage of people without an ID).  I went to my Local and checked, yup, all auto-generated.  Mistake 2:  Make sure you thoroughly check your Local/Test data before moving it where others will use it.

I set all the ID's locally to NULL and rerun the program over and over, no change, all NULL.  What was going on?  What code did I have put in last that broke my program?  Mistake 3:  Keep a change log of your code if it is a program that could possibly mess with data or, at the very least, comment in what changes were made and when they were made.  In this case, I wasn't using a source control server so I wasn't able to rollback my code.

Turns out, I was in such a panic/rush that tomorrow would come and my program wouldn't run correctly, that I hadn't changed the Web.config to point back to Local from Test, I was constantly updating incorrect data in a usable Test database!  Mistake 4:  Don't panic.  When you panic, you make mistakes.

I spoke to my boss, and it turned out I had one more day (today) to fix the program.  I come in this morning, and run the program again (without changes) on my Local, and the program works just as I had always tested it.  No one changed any of the code, something on my machine must have shut down or restarted that made the data come out correctly.  In any case, it was a series of mistakes I could kick myself for.


Friday, August 08, 2008 #

The long-anticipated (at least I anticipate it) Home for the PlayStation 3 has been in beta for probably close to a year if not more.  The beta was initially as a sign-up type deal but now Sony is adding more people to the Home beta via a download on the PSN.  Yesterday, a PS3 Home Theme was placed on PSN allowing for users to download and instantly be submitted to be included in the Home beta, expanding later this month.  Just downloading the theme won't automatically guarantee you get in, they will take in your activity on the PSN as well and that will determine who's in and who's out.  Anyone with a PS3 though should consider downloading it, if not for the cool looking theme itself.

Side note: You may have noticed I haven't been blogging everyday.  The reason is I noticed I was blogging about a lot of random useless crap when this blog was initially created to focus on game development and programming.  I'll try to cut down on the posting and only update when there's industry news, PS3 news, or I've got some code/game tips.


Wednesday, August 06, 2008 #

I just had to padd an integer with zeros when I displayed it and thought it would be a good piece of code to post considering there are some crazy difficult versions of doing this on the net that seem quite unnecessary.

iNum.ToString("000")

This will pad iNum with leading zeros if it's length is smaller than 3.  For example, if iNum = 42, the output would be the string "042".

HERE's a great MS resource for numerous ways to pad strings including with zeros and blank spaces (it's about padding in PowerShell but the general concepts still apply to VB, I can assume it would with C# as well).


Summer is not my time of the year.  I'm not a fan of warm sunny days.  The only good summer day, is a few hours lounging on a deck with some beers and the grill going.  Other than that, summer is so dull and repetative, almost every day (except stormy ones, which are few) is bright outside and warm or humid.  There's no huge change other than an occasional rainstorm.  Here are days I've been daydreaming about lately:

I love fall/winter days where the sky is a dull grey, just ready to burst with snow.  Outside there's little to no noise with a chill blowing in the air, making you bundle up in a jacket.  As a light variation on this, I love days where there's a slow snowfall, where the snow just gently falls from the sky.  These days you can go outside and it's completely silent outside.  The air is cold but not freezing and there's only a slight breeze.  Sitting outside in a nice warm coat your face is cold but your body is warm.  Going inside you warm up and drink/eat lots of very warm calming things.  Perfect.

The other day I love is more of a location idea.  Imagine sitting in a small wood (or some type of) cabin in the woods.  Outside rain continues to fall all day long and every once in a great while there's an occasional rumble of thunder.  Sitting outside, the air is cool but nice enough to sit out in a t-shirt.  The rain falls on leaves, sticks, and trees making little noises as they hit the ground.  A perfect day to sit inside and look out, totally calming.

For some reason both these day-types have very low volumes, either silence or very calming noises.  I would love to find a CD just to relax to of the sounds of the second day but the problem is everything is this stupid rain-forest crap.  Seriously, I listened to that and it was louder than some rock music I listen to, with torrential downpour of heavy rain slamming against creature that are squaking and howling uncontrollably.  Writing that little paragraph about the winter I was totally calm and felt great, writing that sentence about the rainforest totally tensed me up.


Monday, August 04, 2008 #

Here are the things I need to work on/ finish up:

  • Pong RPG fixes and updates (Including CD creation)
  • XNA Book (3 tutorials, introduction, and editing)
  • Articles (for online stuff)

I've also been interested in continuing my work on this fictional story I've got going.  Every time I think of it I love the story, it's just so much to write while I do all this other work taking up all my free time.

Speaking of free time, I haven't had much because of one thing: adventure games.  I've been playing games like Broken Sword 3 & 4 (one of my favorite series), Syberia, and Dreamfall.  So much adventure, so little time, such crazy mixtures of randomly picked up items.