Connect4 with Java AI

Connect 4 including AI


Everybody knows it, everybody loves it: Connect 4. Place your stones so that they connect in a row of 4 pieces diagonally, horizontally or vertically and win.

You hate “4-In-A-Row” – the Connect4 game that e.g. comes per default with ubuntu – because even on the easiest setting it always beats you? Download this game! Its AI makes thoughtful but not too hard moves, it will feel like playing against an actual human being – that’s just a little worse than you are ;) It is also possible to play against another human being. The second player uses right-click to place.

System Requirements / Performance info

Java must be installed, other than that, there should not be any problem running this game.

Source Info and implementation details


The source code is open and contained in the zip files.


The mvc structure is enforced where feasible. At some points the structure is a little weak, this is mainly because it is a university project and every week there where new tasks (but none included – or made time for – making any diagrams or the like).

The current gui is implemented with java swing.

The ai is implemented using the MiniMax-Algorithm (because it is a university project; in the future I might change it to the more efficient alpha-beta algorithm). This was my first implementation, and there are still some mistakes in it. If you would only download the game to see the implementation i would recommend downloading mill with alpha-beta pruning, as the code is nicer and the implementation better (also, it uses alpha-beta).

Download Connect4 Game and Java Source Code

  Connect4 [106.12 kB]


connect 4 screenshot


Thanks go out to fiv who developed this game with me. He is responsible for the graphics as well as large parts of the game logic implementation.

Related Posts:

5 thoughts on “Connect4 with Java AI

  1. Hello.

    I tried to download source code but I’m unable to compile it because there are not the source of 4-winsmanager.jar library. Could you provide the library please? It would be very useful.

    And could you explain how it would be possible to manually change MAXDEPTH ?

    Thank you

  2. Hi Manuel,
    The library 4-winsmanager.jar is attached in the download, in can be found under the /lib dir (so it is outside the source folder).

    “And could you explain how it would be possible to manually change MAXDEPTH ?”

    By manually, do you mean in the GUI? If so, that is not possible at the moment.
    If you mean where it can generally be changed: contains a public static final int MAXDEPTH field right on top (as mentioned, it was a university project, so we did not have tons of time to design the source code and some stuff ended up in weired places^^ That is also the reason why 4-winsmanager.jar is needed at all, we hat to implement their interfaces and I was too lazy to change it later on).


  3. yes,I understand.
    But I mean: is there any source avalaible for 4-winsmanager.jar?
    I’m interested in the code.

    Thank you

  4. Ah, I’m sorry, I misunderstood you.
    No, sadly I do not have the code for it. As it only contains a couple small files you could try to decompile it (not as good as the real source, but might be good enough?)

Leave a Reply

Your email address will not be published.