Connect 4 in Java

Connect4 Game

While studying at the University of Nebraska at Kearney (UNK), I took CSIT150 which is an object oriented programming course based on Java.  For this course we were required to come up with a project which would encompass all of the skills we learned in that course as well as the preceding Java introductory course, CSIT130.   This project was essentially the course’s Final.

I received an A for the project and an A- for the whole course.

Completed Project Goals:

  • Functional and clean GUI
  • Option to play the computer
  • Option to play another human (hot-seat functionality.  no online play)
  • All exceptions must be caught and handled correctly.
  • Save and load games
    • Extra functionality:  custom file extensions
  • Win conditions
    • Extra functionality:  highlight the winning tokens
  • About & Help screens built into program


I created a fully functional and user friendly Java Connect 4 game with an interactive GUI and file I/O.  A presentation was given to the class to explain the program and showcase its functionality as well as the problems that I encountered.  While many features were basic requirements for the overall project, I chose to go above and beyond on a few parts of the project.  The user interface in particular was not left half-baked.  I encountered a few oddities while setting up the GUI where small imperfections would arise – such as a game token appearing a couple pixels out of place – each of these imperfections was reviewed and tweaked until I found a way to eliminate them.  The status/message bar was also designed to change colors depending on the situation for a better user experience.  In addition, buttons like “clear game” were grayed out when not relevant.  Some might say I was a perfectionist, but I really just hate sloppiness!

Connect 4 GUI Screenshot

Diagonal victory message in Connect 4

I also ran into innumerable problems trying to get win-detection working properly.  Many late nights and several visits to the Java documentation and my professor’s office later, I managed to finally get them all working.  The win condition that gave me the most trouble were the diagonal checks.  Those in particular were the bane of my existence for about a week or so.   In the end, I managed to deliver a result that I felt was something I could be proud of.  It looked pretty good and the code wasn’t a total mess either, as I tried to follow standard indentation and variable naming conventions.

JavaDocs & File IO Dialog

File Open Dialog

File Open Dialog

Following the completion of the code, a complete series of JavaDocs were generated to provide documentation of the code.  These JavaDocs are included in the linked materials section.  Another feature I’m rather proud of was the implementation of a file dialog which utilizes custom file extensions (.c4g and .c4gtest).  It also saves files using .c4g file extension.  A special “file type” selection was created for the dialog which filtered the browsing pane to show only recognized Connect4 file types.

As with the rest of the code, try-catches were used to catch and handle any errors that were thrown.  In this case, if no file was selected and the dialog was cancelled by the user, then an exception was caught and allowed the dialog to close gracefully without throwing errors out like candy in a parade.

File IO code

File IO code

Final Thoughts

I had a lot of fun making this project because of the challenges it presented and the cool bonus features I was able to implement.  Given more time, I would have liked to include other features like cheat codes, keyboard shortcuts, and custom token colors.