A sophisticated game development framework in Kotlin, currently featuring a Connect Four implementation with an AI opponent powered by the minimax algorithm with alpha-beta pruning.
- Connect Four Implementation: Complete Connect Four game with human vs AI gameplay
- Minimax AI Algorithm: AI uses minimax search with alpha-beta pruning for optimal moves
- Heuristic Evaluation: Strategic position evaluation favoring center columns
- Interactive Console Interface: Clean, visual board representation
- Move Validation: Comprehensive input validation and error handling
- Undo Functionality: Support for undoing moves (used internally by AI)
- Extensible Framework: Designed to support multiple game types through common interfaces
Player: Enum representing game players (BLACK, WHITE)Move: Interface defining the contract for game moves with player informationConnectMove: Data class implementing Move with Connect Four specific move dataSearchableBoard: Interface defining the contract for game boards used with SearchEngineConnectBoard: Game board implementation with state management and win detectionSearchEngine: Minimax algorithm implementation with alpha-beta pruningConnectPlayer: Wrapper for AI move generation (formerly AIPlayer)AIGame: Main game controller for human vs AI gameplay
- Optimized Win Detection: O(1) winner checking around move positions
- Heuristic Evaluation: Column values
[10, 20, 50, 70, 50, 20, 10]favoring center play - Configurable Search Depth: Adjustable AI difficulty via search depth
- Complete Game State Management: Move history, undo support, ConnectBoard copying
- Java 21 or higher (configured in build.gradle.kts)
- Kotlin 2.3.10 (automatically managed by Gradle)
- Gradle (included via wrapper - version 9.2.1)
# Clone or navigate to project directory
cd KotlinGameBench
# Build the project
./gradlew build
# Run the Connect Four game
./gradlew run# Build JAR and run directly
./gradlew build
java -cp build/libs/KotlinGameBench-1.0-SNAPSHOT.jar li.kausch.kgb.MainKt
# Or run JAR directly (if configured)
java -jar build/libs/KotlinGameBench-1.0-SNAPSHOT.jarThis framework can be integrated into macOS and iOS applications via XCFramework.
# From the KotlinGameBench project directory
cd KotlinGameBench
# Build all framework components (iOS, iOS Simulator, macOS)
./gradlew buildXCFramework
# Create the complete XCFramework bundle
./gradlew createXCFrameworkThe buildXCFramework task builds framework binaries for:
- iOS ARM64 (devices):
build/bin/ios/releaseFramework/KotlinGameBench.framework - iOS Simulator:
build/bin/iosSimulator/releaseFramework/KotlinGameBench.framework - macOS ARM64:
build/bin/mac/releaseFramework/KotlinGameBench.framework
The createXCFramework task then combines these into a single XCFramework bundle:
- Output:
KotlinGameBench.xcframework
- Launch Xcode
- Create a new macOS or iOS project
- Choose your preferred template (App, Tool, etc.)
- Locate
KotlinGameBench.xcframework(created in Step 1) - Open your Xcode project
- Select your project in the navigator
- Go to Build Phases โ Link Binary With Libraries
- Click the + button and select Add Files...
- Navigate to and select
KotlinGameBench.xcframework - Verify it's added to your target
To properly link the framework, adjust your build settings:
- Select your project
- Go to Build Settings
- Search for "Validate Built Product"
- Set Validate Built Product to No for Debug configuration
Import and use the framework in your Swift code:
import KotlinGameBench
// Initialize and use the game framework
let board = ConnectBoard()
let gameEngine = ConnectGameEngine()
let game = ConnectGame()
// Play the game
game.runConnectGame()- Framework not found: Ensure the XCFramework was built successfully and is in the correct location
- Linker errors: Verify that Validate Built Product is disabled
- Runtime crashes: Check that all framework dependencies are properly linked
- The game starts with an empty 6x7 ConnectBoard
- White player (โ) goes first (Human)
- Black player (โ) is the AI opponent
- Enter column numbers 1-7 to drop your piece
- First to get 4 pieces in a row (horizontal, vertical, or diagonal) wins!
Welcome to Connect Four!
Players: โ (BLACK - AI) vs โ (WHITE - Human)
Enter column numbers 1-7 to drop your piece when it's your turn
Initial ConnectBoard:
1 2 3 4 5 6 7
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+-+-+-+-+-+-+-+
Player โ's turn (Human)
Enter column (1-7): 4
The AI difficulty can be adjusted by modifying the search depth in ConnectGame.kt:
val aiMove = aiPlayer.getBestMove(board, currentPlayer, depth = 6)| Depth | Difficulty | Performance |
|---|---|---|
| 3-4 | Beginner | Very Fast |
| 5-6 | Intermediate | Fast |
| 7-8 | Advanced | Moderate |
| 9+ | Expert | Slow |
- Search Algorithm: Minimax with alpha-beta pruning
- Evaluation Function: Position-based heuristic + terminal state detection
- Optimization: Early termination, move ordering, transposition-friendly
- Time Complexity: O(b^d) where b=7 (branching factor), d=depth
Column: 1 2 3 4 5 6 7
Value: 10 20 50 70 50 20 10
- Center columns (4,5) are most valuable
- White maximizes positive values
- Black minimizes (creates negative values)
- Terminal states:
Int.MAX_VALUE(White wins) /Int.MIN_VALUE(Black wins)
// White plays column 4: +70 points
// Black plays column 4: -70 points
// White plays column 1: +10 points
// Current heuristic: 70 - 70 + 10 = 10This project is open source. Feel free to use, modify, and distribute.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- Search Depth 6: ~100,000 positions evaluated per move
- Alpha-Beta Pruning: ~10x speedup over basic minimax
- Memory Usage: Minimal - uses move/undo pattern
- Response Time: < 1 second for depth 6 on modern hardware
KotlinGameBench - A Kotlin game development framework ๐ฎ
Formerly "ConnectFourKotlin" - renamed to reflect the broader framework vision


