My Minesweeper Clone + other stuff
I haven’t been posting much on tumblr lately, mostly because I’ve been enjoying my summer and getting wrapped in other things. I made a twitter account, which I’m much more active on lately. Give me a follow if you’d like.
Last May, for my penultimate project in an introductory Java course, me and the two other kids in my class were assigned to “just make Minesweeper,” which is far more challenging than it sounds. Initially, I had difficulty with much of the logic. I managed to figure out how to assign the number of adjacent mines to each square, despite using a non-optimal and very convoluted set of rules. The one thing I just couldn’t figure out how to do (given a one-two week timespan) was the logic beyond squares with no adjacent mines revealing its surrounding squares and so forth. I told my teacher about it and he didn’t care, replying that it was “an advanced feature” and that I “shouldn’t worry about.” Needless to say, the code that I ended up turning in was very crude but still worthy of getting an A.
I felt cheated in a way, because I hadn’t really created Minesweeper, just a cheap knockoff that you couldn’t even play. So I decided to finish the program on my own time because it would be a good learning experience. When I resumed my project, the first thing I decided to tackle was my earlier problem.
I eventually found out a way to make it work, but I wasn’t satisfied with it. It was a non-optimal chunk of code that was very repetitive. After countless searches on Google, I was able to fix both the logic for counting mines and the problem I previously discussed in a very optimal fashion. I decided, “why stop here?” I might as well add a timing function just like the real Minesweeper, which is exactly what I did. It was pretty easy to do; I used the java.util.Timer class and had it execute the actionPerformed() method every second, in turn updating the amount of elapsed time in hh:mm:ss format. (which may have been overkill)
Afterwards, I decided to take it a step further and give the user the ability to customize the size and difficulty of the board. This proved to be a big problem for a long time. The size of the array of buttons was dependent on the number of rows and columns. After the array had been established, I wouldn’t be able to change its size. I played around with idea of using four arrays, which I thought was a waste of resources and it ultimately didn’t work. I settled on declaring the maximum size of the array (in this case [16][30]) and removing/adding the correct number of buttons from/to the grid depending on which difficulty option is chosen. Simple, easy, and it makes sense. Lastly, I decided to create two separate versions of the program: an applet and an application and placed them in convenient packages (com.tumblr.aguiney.minesweeper) instead of the default package, which I’ve always used up until now. I encourage you to take a look at it and tell me what you think.
The complete source code, including the application jar file, can be found on my GitHub account.
Unfortunately, I have no way of hosting the applet version because a) tumblr doesn’t allow applets, b) the applet won’t load on dropbox, and c) the Google App Engine doesn’t support client side code or most of the Swing/AWT libraries. For now, just download the application.
Also, I have a few ideas for some blog posts that are still in the drafting stages. Look forward to reading them; I promise they’ll be good reads. Next, I’ll probably finish my text adventure game which is now way overdue.