Open links in new tab
  1. Connect 4 is a classic two-player game where players take turns dropping colored discs into a vertical grid. The objective is to connect four of one's own discs in a row, either horizontally, vertically, or diagonally.

    Example Implementation

    Here's a simple implementation of Connect 4 in Java:

    import java.util.Scanner;

    public class Connect4 {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    char[][] grid = new char[6][7];

    // Initialize the grid
    for (int row = 0; row < grid.length; row++) {
    for (int col = 0; col < grid[0].length; col++) {
    grid[row][col] = ' ';
    }
    }

    int turn = 1;
    char player = 'R';
    boolean winner = false;

    // Play the game
    while (!winner && turn <= 42) {
    boolean validPlay;
    int play;
    do {
    display(grid);
    System.out.print("Player " + player + ", choose a column: ");
    play = in.nextInt();
    validPlay = validate(play, grid);
    } while (!validPlay);

    // Drop the checker
    for (int row = grid.length - 1; row >= 0; row--) {
    if (grid[row][play] == ' ') {
    grid[row][play] = player;
    break;
    }
    }

    // Check for a winner
    winner = isWinner(player, grid);

    // Switch players
    player = (player == 'R') ? 'B' : 'R';
    turn++;
    }

    display(grid);
    if (winner) {
    System.out.println("Player " + ((player == 'R') ? "B" : "R") + " won!");
    } else {
    System.out.println("Tie game");
    }
    }

    public static void display(char[][] grid) {
    System.out.println(" 0 1 2 3 4 5 6");
    System.out.println("---------------");
    for (int row = 0; row < grid.length; row++) {
    System.out.print("|");
    for (int col = 0; col < grid[0].length; col++) {
    System.out.print(grid[row][col]);
    System.out.print("|");
    }
    System.out.println();
    System.out.println("---------------");
    }
    }

    public static boolean validate(int column, char[][] grid) {
    if (column < 0 || column >= grid[0].length || grid[0][column] != ' ') {
    return false;
    }
    return true;
    }

    public static boolean isWinner(char player, char[][] grid) {
    // Check horizontal, vertical, and diagonal lines for a win
    for (int row = 0; row < grid.length; row++) {
    for (int col = 0; col < grid[0].length - 3; col++) {
    if (grid[row][col] == player && grid[row][col + 1] == player &&
    grid[row][col + 2] == player && grid[row][col + 3] == player) {
    return true;
    }
    }
    }
    for (int row = 0; row < grid.length - 3; row++) {
    for (int col = 0; col < grid[0].length; col++) {
    if (grid[row][col] == player && grid[row + 1][col] == player &&
    grid[row + 2][col] == player && grid[row + 3][col] == player) {
    return true;
    }
    }
    }
    for (int row = 3; row < grid.length; row++) {
    for (int col = 0; col < grid[0].length - 3; col++) {
    if (grid[row][col] == player && grid[row - 1][col + 1] == player &&
    grid[row - 2][col + 2] == player && grid[row - 3][col + 3] == player) {
    return true;
    }
    }
    }
    for (int row = 0; row < grid.length - 3; row++) {
    for (int col = 0; col < grid[0].length - 3; col++) {
    if (grid[row][col] == player && grid[row + 1][col + 1] == player &&
    grid[row + 2][col + 2] == player && grid[row + 3][col + 3] == player) {
    return true;
    }
    }
    }
    return false;
    }
    }
    Copied!
  1. Implement Connect 4 Game with Java - Baeldung

    Mar 7, 2025 · In this article, we’re going to see how we can implement the game Connect 4 in Java. We’ll see what the game looks like and how it plays and then …

  2. Connect 4 in Java · GitHub

    Dec 8, 2025 · Connect 4 in Java. GitHub Gist: instantly share code, notes, and snippets.

  3. Connect4 game using Java - Medium

    Jun 24, 2024 · Develop a Desktop application using Java to recreate the Connect4 …

  4. Creating a Java Connect Four Game: A Complete Guide

    This tutorial will guide you through the process of creating a Connect Four game using Java, covering everything from basic game mechanics to advanced features.

  5. Connect 4 Game in Java - Tutorial - CodePal

    Nov 9, 2023 · Learn how to create a Connect 4 game in Java with this step-by-step tutorial. Understand the game mechanics and implement the logic to check for …

  6. Connect Four game in Java - Code Review Stack Exchange

    Aug 14, 2015 · I was wondering if you guys could point out any mistakes I've made in this program, and how to improve the code. It's a simple console-based Connect Four game. You use 0-5 to place your …

  7. Build and Play Connect Four Game in Java | Full Tutorial

    Jul 13, 2024 · In this video, we'll walk through the entire process of creating a console-based Connect Four game from scratch.

    • Author: Prince Kamal Raj
    • Views: 317
  8. connect-4---java/src/connect4/gui.java at master - GitHub

    a connect 4 (or connect n) cli game engine in java for n human players - connect-4---java/src/connect4/gui.java at master · sloev/connect-4---java

  9. connect-four-javafx-9 | Connect Four is a two-person connection game …

    Here are some attached screenshots of code from IDE and the actual JAVA Game running

  10. People also ask
    Loading
    Unable to load answer