🔗 Demo

Description

Connect 4 is a two-player zero-sum connection game.

Features

  • Default behavior:

    • Human is yellow. CPU is red.
    • Human moves first. CPU moves second.
    • Game Over condition: when either player achieve 4 consecutive marbles (horizontally, vertically, and diagonally). It’s a draw if there’s no more slot on the board.
  • Allows either the AI or the user to play.

  • It is possible for the AI to play against itself through network.

  • Auto-saved game on close using serialization.

  • Menu -> New Game should reset the game state.

Implementation

The game’s implemented using the MVC design pattern.

Intelligent Computer Player

Computer player’s using Minimax alogrithms with alpha-beta pruning. More details here: https://en.wikipedia.org/wiki/Minimax

Network Protocol

Establishing Client and Server Roles

The Server will always take the first turn. If it is a human player, the player will click and send the event to the client. Otherwise the AI will generate its turn and send it to the client. The client will go second. This will repeat until the game is over.

A move made over network is implemented using serialization. Here’s the details:

public class Connect4MoveMessage implements Serializable {

  public static final int YELLOW = 1;
  public static final int RED = 2;
  private static final long SERIAL_VERSION_UID = 1L;
  private int row;
  private int col;
  private int color;

  public Connect4MoveMessage(int row, int col, int color) { //… }

  public int getRow() { //… }
  public int getColumn() { //… }
  public int getColor() { //… }
}