Monday, May 4, 2015

File I/O Fun with Mario


For my latest project in Intermediate Object-Oriented Programming, we got to make a trivia game. The project taught me a few new things, including how to make the game fun for my kids:




I learned how to read a text file line by line, and how to setup error checking via try and catch. The following code is used to try and load the trivia questions when launching the game:





The majority of this is example code from my teacher that she lets us use to understand new concepts, but it's directly from my program. Streamreader is the first new thing to me, you give it a path and it reads through the text file line by line. When it starts the try if the path is set correctly and the file exists, it begins storing everything into questions, answers, etc. This particular program is limited to first reading a question, then a list of answers, a letter representing the correct answer choice, and an explanation if the user got the question wrong. If for some reason there's an error, that's when catch comes into play. In this case it's looking for exception errors, and if it finds one it will give you the error message plus the custom message in red pointing you to where in the code an error popped up. It's really handy for things where you know an error could occur, such as if someone got a hold of this program but without the text file holding all the data.


But easily the most fun part of this program was sharing it with my sons. Sure, it's only a quick 5 question quiz but having them next to me yelling out answers was really fun. However, as the quiz questions increase in difficulty and this one really stumped them:




Do you know the answer? My boys haven't been exposed to Super Mario 3 and they're missing out on one of my favorite Mario items ever.

No comments:

Post a Comment