Skip to content

[FEATURE REQUEST] Add TicTacToe and Connect Four games to puzzlesandgames package #7772

Open

Description

@aniketShukla1605

Title

Add TicTacToe and Connect Four implementations with game logic and optimal move finder

Description

I would like to propose adding two classic board games, TicTacToe and Connect Four, to the puzzlesandgames package.

Both implementations will provide core game functionality, including board management, player turn handling, move validation, win detection, and game-over conditions.

Motivation

These additions would expand the existing puzzles and games collection with interactive game-logic implementations while demonstrating 2D array manipulation, recursion, game-state management, adversarial search, and efficient win detection.

Issue details

Proposed Implementations

1. TicTacToe

A two-player TicTacToe implementation featuring:

  • Standard 3×3 game board.
  • Two players using X and O, with X moving first.
  • Move placement with validation for occupied and out-of-range cells.
  • Automatic player turn switching.
  • Detection of horizontal, vertical, and diagonal wins.
  • Draw and game-over detection.
  • Retrieval of the current board, winner, and active player.
  • Defensive copying when returning the board.
  • An optimal move finder using the Negamax form of the Minimax algorithm, ensuring perfect-play decisions.

2. Connect Four

A two-player Connect Four implementation featuring:

  • Standard 6×7 game board.
  • Two players using Red (R) and Yellow (Y), with Red moving first.
  • Disc placement using column selection, with discs falling to the lowest available position.
  • Validation for invalid and full columns.
  • Automatic player turn switching.
  • Detection of horizontal, vertical, and both diagonal winning combinations.
  • Draw and game-over detection.
  • Retrieval of the current board, winner, and active player.
  • Defensive copying when returning the board.
  • Efficient win detection by examining the four directions through the last placed disc.

Unit Tests

Both implementations will include JUnit 5 test classes covering:

  • Initial board and player state.
  • Valid and invalid moves.
  • Player turn switching.
  • Horizontal, vertical, and diagonal wins.
  • Draw conditions.
  • Game-over handling.
  • Board defensive copying.
  • Game-specific functionality, including optimal move selection in TicTacToe and disc stacking in Connect Four.

Proposed File Structure

src/main/java/com/thealgorithms/puzzlesandgames/

  • TicTacToe.java
  • ConnectFour.java

src/test/java/com/thealgorithms/puzzlesandgames/

  • TicTacToeTest.java
  • ConnectFourTest.java

Additional Information

No response

Activity

  1. changed the title [-][FEATURE REQUEST] <title>Add TicTacToe and Connect Four games to puzzlesandgames package[/-] [+][FEATURE REQUEST] <title>Add TicTacToe and Connect Four games to puzzlesandgames package</title>[/+] on Nov 23, 2026
  2. changed the title [-][FEATURE REQUEST] <title>Add TicTacToe and Connect Four games to puzzlesandgames package</title>[/-] [+][FEATURE REQUEST] Add TicTacToe and Connect Four games to puzzlesandgames package[/+] on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions