Last week, Week 3, was our first project week. I was out sick with a bad cold on Tuesday, when they were assigned, and scrambled on Wednesday to catch up. Out of three projects, I was given Battleship. Or, creating a Ruby program for a command line Battleship game. It was generally accepted to be the hardest of the projects and just seeing the directions were overwhelming at first.
Deciding on what type of grid I would use for the board was my first step. It took way too long, but I felt that I had to have the board setup before I could hope to do anything else, since so much would rely on it. Finally, I decided on a one-dimensional array, which I would print out on the screen for people to view. I think it was the simplest method to use and it made finding the actual hits and misses very easy.
The grid printed out the coordinates, as well, and players could just type in the grid location, like you would say in a real life Battleship game, “A1,” “B2,”.
From there, I had to build up the game itself, using While loops and If/Elsif to go through the motions of every possibility that might come up. I created a game table in ActiveRecord, with ships, players, and turns as subtables. Since ActiveRecord isn’t the biggest fan of arrays, separating turns into their own rows and then turning them back into an array later on seemed like a better choice.
It was a harrowing experience, but after the fact it was a great learning opportunity. The fact there were so many ways to go about making the Battleship game meant that it was next to impossible to fall back on anyone else for help, unlike with many past homework experiences where we were able to work basically in a group. So, while it is very important for coding to have experience working with others, it was good to spread my wings and have to rely basically on myself.
(Coincidentally, on Thursday afternoon our class took a fieldtrip to USAToday and part of the presentation we were given involved slides where the person who created them used Battleship as a metaphor. Since most of us were desperate to get done by Friday morning, we shared a slightly hysterical chuckle.)
The necessity to use ActiveRecord to save game information–not just players and scores, but the actual ship locations and previous turns–so people could restart them made things unnecessarily difficult in my opinion. While it was necessary for what we were learning and trying to practice at the time, in a real life situation I feel it’s unlikely anyone would want to restart their Battleship game partway through, as opposed to just starting a new game each time.
Things I wish I’d gotten to:
Changing the places on the grid to reflect a hit or miss (I had started, but had to trash that code because I couldn’t get it to work). Making it look nicer (I had tried to design all my own text art, but realized after the fact I should have just used a website). Adding a two player option (again, something I started, but didn’t get to finish). Separating the game out into more methods, so it’s more compartmentalized and would be easier to switch between single player and two player games.
I also was wondering about how much time this would take in a larger app, how many bits I was taking up that might be a constraint with something else.
You can find my most recent work on the game on Github.