Mastermind game

This is my first Object Oriented game in Ruby. I have recently finished it after working on it over few evenings. I felt excitement when played it during testing phase or just when I was looking for a bug. It brings some nostalgia for games I played in early 90’s. Games such as Arkanoid or Tetris and even some of the games like Wheel of Fortune which one could play in DOS environment, with no graphical interface. It was just a black screen with white letters describing scores, questions, answers, etc.

Here I am, a “creator” of a reproduction of the one of the most successful games in the ’70s – Mastermind. My code as usually is available in my Github repository.  I wished to be able to present it in the form of game anyone can play on my blog. Unfortunately, this is something t come in the later part of the course. This game can be run in Command Line environment where Ruby was setup beforehand. Here are few screenshots of the gameplay: screen-shot-2016-10-18-at-20-29-33

I have created six classes for this game. The player can choose whether to be the one creating code and letting the computer to guess the secret colour combination or to be a codebreaker and let the computer create a secret colour code. The human player class asks a user for the input of specific guess, while Computer codemaker class generates simple random code. In the similar fashion, I have created Human codemaker and Computer codebreaker – where the human player creates secret code and the computer just generates random colour combination guesses. The board class is responsible for storing colour combinations (just like in the real game) or calculates the feedback scores in the form of pegs – white or black.

For the feedback, I have created simple ASCII “graphical” interface. So we have six colour pegs and two feedback pegs which will inform the player how close were his guesses. Board class also renders colour guesses and feedback pegs on the screen.

During the building process, I followed the idea of creating small blocks – objects and creating communication between themscreen-shot-2016-10-18-at-20-56-45 in a low cohesive way. This become a difficult task as many times I faced an issue of their dependability while my intention was to avoid it – lessons are best learnt on own mistakes. I tried to follow simple rules like one task per method or Tell Don’t Ask.

I have started creating the whole game from pseudocode. After that, I was gradually replacing it with more detailed descriptions. Here I started thinking that if I require a particular action (let’s say adding pegs into the board), I would try to split it into most basic actions, pack them into methods and name them in self-explanatory fashion. It made the whole debugging process much easier. Before, whenever I started coding, I had to spend few minutes to recall how a certain method work, while now I could easily see what’s going on just by looking on it.

My plan is now to review some codes written by other developers of this or similar game and copy the best practicies of coding – so my next game is, even more, OOP and cleaner. The next game I am working on is Conect Four. I can’t wait to put my hands on.

As for the life matters, I can only say that it’s easier to plan a schedule than follow it. Unforseen life circumstances took me many hours out of my schedule, among them overtimes at work, meeting friends I haven’t seen ages, leakage in the house… and some adventures in personal life. Overall I spent over 16 hours last week on coding – about 8 hours on the goal.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s