2/3 Player Tic Tac Toe
Language used: Java
We have all played tic tac toe at some point in our life, but it usually involved 2 players. However, this game allows a 2 player or 3 player game according to choice. The players can play amongst themselves or against the computer. Error messages are displayed when none of the game types are chosen or when none of the players have been specified.
Planning
I started off by finalising the different functionalities that would be in the game, then I moved on to decide the different classes that I would require and distributed the responsibilities amongst those instances. I had to ensure that the coupling was low, and cohesion was high. Creating a UML diagram was quite helpful as it enabled me to clearly see the relationship between the different classes and the purpose that they served. I planned to develop the game using three different stages, first being the main logic development and testing, second being a TUI, and lastly the final GUI.
Action
I used Test Driven Development to create the classes. This method was extremely beneficial when developing the logic as it reduced the amount of error and the pressure of testing everything together at the end. During the first stage I created the different classes for the Board, Player and the main simulation. After having completed this stage, I created a TUI for testing the entire game. Finally, I developed the GUI for the game, this included various classes for the different aspects such as displaying the main home page, analysing the inputs and displaying the game. After having the structure and logic ready for the GUI, I designed and customised it using different colours and fonts.
Design and OOP Principles
The project has been created keeping all the OOP principles in mind.
- High cohesion and low coupling have been maintained throughout. For example, there are different classes for initialising and displaying the main window, taking the input from the user, analysing the input for the creation of the game board and initialising and displaying the game board.
- Implementation hiding and data encapsulation have been obeyed by declaring fields "private" and providing getter methods and by binding the data (fields) together with the behaviour (methods).
- It is a responsibility driven design, as the instances have some behaviour and are not just bundles of data.
- DRY (Do Not Repeat) has been implemented in the code, by reusing the code whenever needed through composition.
- Error avoidance has been followed by checking the parameters, whenever passed, for validation and throwing an exception in case the parameter is not valid. For example, when a player occupies a place, a check is performed whether the position is already occupied or not.
Result
The video below shows the final result.
Learnings and Improvements
Through this project, I was able to learn a lot about GUI, such as designing, efficiently setting up multiple windows, user interactivity and various other features and functionalities of JavaFX. I have also been able to write better test cases with higher coverage rates. I have gained further experience in creating responsibility driven design which is very essential when coding larger systems.
However, there is a further improvement required which is a little delay in the time lap within which the computer responds. At the moment, it responds immediately which is the reason why the statement for the Computer’s turn in not visible. Also, there can be further improvements in making the computer play smartly. If you have any suggestions or ideas regarding these or any other feedback, then please feel free to contact me via email or LinkedIn.