Connect Four Replica
Implementation of a Classic Game

This project is a digital implementation of the classic Connect 4 game, where two players compete to align four of their pieces in a row. The game is developed in Java and features a user-friendly interface for an engaging gaming experience.

graphical interface for connect four game

Why This Project?

I chose to create this game because it required a foundational understanding of Java, and a beginner level knowledge in graphical user interfaces. The game being widely popular meant I would have resources to fall back on when I would be struggling. Along with the minimal experience the development demanded, my time commitment to designing this game also aligned with the general time it took to complete a game of this difficulty. I wanted to invest around 3-4 weeks from the start of understanding win conditions, implementing a GUI, and finally the game development itself, and I found this project to be entertaining, challenging, and a huge progression in my programming knowledge.

Concept

picture of a normal connect four game

The concept is alike the original game. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own tokens. The two player game involves alternating turns where each player is allowed to place one token during their turn. After every turn, players check if a win condition has been met, or if the game continues. After a game is completed, they have the option to restart the game and play again.

Struggles

Although the concept is simple for the game, the development process was a lot harder. Win conditions must be checked from the token placed during that turn. Horizontal and vertical checks are simple to check, since they only require me to check in one direction (either up and down or left and right). The checks end either when the token changes, or the piece is at the edge of the grid. The diagonal checks proved to be much harder. The logic behind it was a bit more complicated, having to move the counter in a row and column pattern. I had to implement checks to make sure that when the counter moved into a new column and row, it didn't leave the board.

Another problem I encountered was that the tokens would not be visible until the next token was placed. The players wouldn't be able to see the previous turn by the opponent. This one was a simple fix, as I created a new function to repaint the board throughout the game. In an endless loop, the board would be updated after each token placed.

Finally, the last problem of the notable issues was dealing with the end of the game. Disabling key listeners after the game-over value was true was a learning experience as I had never done this before. The Java AWT API that I was using for the GUI would forcefully close after the game was over. Experimenting with the API and its key listeners, I added a new function to keep the game going, a restart button. Pressing 'r' would restart and the clear the board, removing all tokens from the grid.

What did I learn?

implementation of 'L' shaped win

Majority of this project was new to me. I had basic knowledge in Java, but little to none with APIs and graphical interfaces. I found it enjoyable to test my knowledge with 2D arrays, conditionals, key listeners, and a simple UI design. Game development opened my eyes to a whole new world of logic, and connect four being a simple game, there is significant room for creative enhancements. For instance, you could introduce alternative winning conditions, such as allowing a player to win by forming an 'L' shape, similar to the movement pattern of a knight in chess. The coding aspect would involve checking all directions that an 'L' can make from the token placed, which would add a level of difficulty to the development, as well as the game.

To showcase my learning, you can view my implementation of a connect four game and find more information about this project on my Github.