Wednesday, 16 October 2013

Game #002 - Your Interplanetary Age

Game #002 on Github

My second game is another little tech demo which asks your age and returns its equivalent on every planet. Yes, another ground breaking show of technology!

I quite enjoyed make the interplanetary age calculator. This was mainly due to the challenges I encountered whilst coding.

Feature Creep

As someone in the IT (I am a software tester) I am very familiar with feature creep. The phrases "Couldn't we just" and "It would be nice if" are punishable offences around me!  I started #002 with the idea to ask the user their age, and break it down to months, days, minutes. It's a simple one, and a small step up from #001. Then I thought "Wouldn't it be nice if I finished with the user's age on Mars?" Yep, feature creep almost before I had started to code. A bit of searching later, I found a site with Your Age on Other Worlds. And the wish to write a python script which gives you your age on all the planets in the solar system was born. First I put together a table to help me with the different revolution periods of each planet. This formed the basis of my calculations further down the programme.

Planet Rotation Period (Earth days) Revolution Period (Earth years/days)
58.6 days
87.97 days
243 days
1 day
365.25 days
1.03 days
1.88 years
0.41 days
11.86 years
0.45 days
29.46 years
0.72 days
84.01 years
0.67 days
164.79 years
6.38 days
248.59 years

The Int to Str, Str to Int Switcheroo 

When I started coding, I asked the person their age and updated a variable, put that variable into an equation, and print the answer. Simple. For a newbie like me, it took a little bit of research to find out why my code kept on failing. It was great fun. It turns out that the input command produced a string, which wouldn't play well with my equation. The solution was to take the user input and turn it into an integer. This meant that the equation worked.
The next problem encountered was that the print statement wouldn't work because the print command doesn't work with integers. Having researched the need to change the user's input to an integer, it was relatively quick to realise what needed to be done. The result was the following flow of code:

 print("What is your age?")age = int(input(">>> "))  
 mars_age_years = age / 1.88  
 print("You are " + str(mercury_age_years) + "Mercury years old")  

This bit of code would essentially calculate and print a user's age on Mars

Round, Float, Decimal - Who knows!?

If the code above is run the result is that you age is likely to be have a lot of numbers after the decimal point (eg. 5.319148936170213 if you input an age of 10). This doesn't scan as easily as having an  age to two decimal points. This is where the majority of my research came in.
I recall in java it is easy to restrict to a number of decimal points by wrapping your equation/result in float. In python there are three options, all with different foibles of their own. I won't go into them here as I don't entirely understand what the difference is at this point in my learning.
After a decent bit of research, I found that rounding the age to two decimal points produced the answer I wanted. So this:
mars_age_years =  age / 1.88
became this:
mars_age_years = round(age / 1.88 ,2)
I now had a working script I could apply to all the revolutionary cycles of all the planets.

Github Learnings

Part of the fun of this project is putting the code out there for all to see. Like many people I use Github. I have the windows client installed, and it is very easy to push everything to the cloud for all to see. I am researching whether there is an online python interpreter so the scripts can be run without the need to have python on the machine.
I had just finished the game and was attempting to put the final version up on Github when I inadvertently rolled back on my machine. Being new to it all, I attempted to revert to the version online, but because there was now a disconnect between local and git, I was getting an error. It took me two and a half hours to sort out what would have taken someone with the knowledge five minutes to do! I really need to be a little more careful with how I roll back in future.

Game #002 Conclusion

If you have taken a look at the script in full, you will note that everything is still very procedural. There are no functions defined, for example. This is ripe for a revisit further down the line to refactor just about every line of code to make it easier to read and maintain.
Another thing that needs to change is that the user input is incredibly brittle, and not very interesting. If you put 0 in, the programme crashes. If you put a decimal in, the programme will crash. A loop needs to be added to handle these two things.
And what about interplanetary days?! The site I found during my initial design and research calculates the user's age in days as well. This is particularly interesting as each planet's rotation period is vastly different. So, this would definitely be a good addition to the whole programme.

Watch this space.

Post a Comment