Connect Four game vs life challenges

 

It’s been a long break in writing. Throughout the past three weeks, a lot has happened. A week after publishing my last post, I flew to Poland to take an honorable role of the best man at my friend’s wedding. I have also visited my family. The start of my trip made me distressed as I couldn’t code my game due to lack of spare time – seemed like there was constantly something going on – meeting brothers, father, driving far for the wedding, etc. I felt I had to let go the thinking about the course to enjoy the time with the dearest one. So that’s how a very long break started in my coding. Moreover, Mastermind’s AI class I was working on became very tough to crack . This problem was waiting for me while holidays clearly didn’t give any opportunities to ease it out – situation started draining my energy. After coming back to Dublin, it took me about a week to slowly get back into the routine and sorting out this procrastinated problem from my code. It took another number of days to crack it and get into the flow back again. Nothing pleasant.

I am happy to be here now – starting new lessons and challenges, refreshed, having rested and with growing motivation again. This little crisis helped me to cut other small pieces from my daily routine. Having spoken to some of the other students at Viking, it became apparent to me that the week plans I was practicing were not working. Now, instead of having 2-4hours Mon – Fri and 5-6h Sat-Sun, I am having Tue and Wed totally off, while having at least 4 hours of concentration on other days. Thanks to that, pressure is off, I can call anyone I want to or go to the city, meet friends, etc. on those days without any guilt and the outcome is still about 24 hours a week – at the smaller effort.

I am currently digging deeper in RSpec and instead of testing my code like before, I am building tests and gems. A lot of new stuff to learn and new projects are approaching. This subject, however, I will continue next time. Also, in a moment of being stuck, I am working on creating my website portfolio.

I would like to share my experience from creating Connect Four game. It’s as complex as nothing else I did so far. I had to rearrange the whole code a good few times completely. I think I spent about 30 hours to make it work, but now finding solutions to bugs are so quick to me as never, objects very familiar. I have also learned to clean my code better and keep methods smaller. On the top of that, I understood better traversing 2D arrays, detecting the state of complex objects, toggle players, building simple strategy AI player, etc.

So connect four game is a version of tic tac toe with a board six by seven and rules forcing a player to connect four pieces instead of three. The code, as always is available in my GitHub repository. The game works in Command Line. It allows playing against another player or a computer. The computer is an AI, whose goal is to put piece wherever it has already three pieces on the board. If it’s not the case, AI is meant to block any three pieces of its opponent, otherwise to choose a random place.

Needless to say, the most challenging part of the game was to make it look for diagonal four pieces or three pieces in various positions, i.e.,. piece_piece_space_piece – so third space can be filled to win. This role is taken by the PegSystem class in my game which is responsible for detecting relationships between different pegs/pieces.I am still learning from other people’s solutions, and still see a lot I can improve. In order to improve these skills I am reading now three little chapters every day from Rus Olsen book “Eloquent Ruby” – there is a good part of very minimalistic clean code included and practices in order to  improve my code in next challenges.

Screen Shot 2016-11-20 at 15.24.23.png

The code above is just a snippet out of the main game class. There ar five classes overall. Here we see my PegSystem class mentioned above that checks situation on the board and can be used by computer AI class to get messages about where are three pegs belonging to the opponent so he can block or three pegs of his so he can win when adding his own there in the next move. I have all included in  loop which is broken only when player makes the right decision about who he wants to play with… so I excluded situation where he enters ‘w’, as the only choices are C and U (I added extra method to make it capital even if player types small c as we know how players are 🙂 ). This method sets the main objects for the game as board object was initiated at the beginning. I am happy to start new challenges now  – need a break. I am going back to building my Rspec tests. Till next time.

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s