A classic Hex board game implementation supporting 1-2 players with AI opponent.
Hex is a strategic connection game for two players played on a hexagonal grid. The goal is to form an unbroken chain of your pieces linking two opposite sides of the board.
- Player 1 (X): Connect the top edge to the bottom edge
- Player 2 (O): Connect the left edge to the right edge
- โ Support for 1-2 players
- ๐ค AI opponent with multiple difficulty levels
- ๐ Multiple board sizes (7x7, 9x9, 11x11, 13x13)
- ๐ฎ Interactive command-line interface
- ๐ NEW! Web-based graphical interface
- ๐งช Comprehensive test suite
- ๐ Clear game rules and help system
- Clone the repository:
git clone https://github.com/AlexShadow3/HexGame.git
cd HexGame- Install dependencies:
npm installnpm start
# or
npm run start:clinpm run start:guiThen open your browser and go to: http://localhost:3000
The web interface provides:
- ๐จ Beautiful visual hexagonal board
- ๐ฑ๏ธ Click-to-play interaction
- ๐ฑ Responsive design for mobile and desktop
- ๐ฏ Visual game state indicators
- ๐ค Seamless AI integration
- Human vs Computer - Play against AI opponent
- Human vs Human - Two players on same computer
- AI vs AI - Watch computer players battle
- Use column letter + row number format:
A1,B5,K11 - Columns are labeled A, B, C, ... (left to right)
- Rows are numbered 1, 2, 3, ... (top to bottom)
help- Show move helpmenu- Return to main menu
- Player 1 (X): Create a connected path from the top edge to the bottom edge
- Player 2 (O): Create a connected path from the left edge to the right edge
- Players alternate placing their pieces on empty hexagonal cells
- Once placed, pieces cannot be moved or removed
- A winning path must be continuous through adjacent cells
- Corner cells count as part of both adjacent edges
- The first player to complete their connection wins
- Hex cannot end in a draw (mathematical proof exists)
- Control the center of the board
- Block your opponent's potential connections
- Create multiple threatening paths
- Plan several moves ahead
HexGame/
โโโ src/
โ โโโ HexBoard.js # Board representation and game logic
โ โโโ HexGame.js # Main game controller
โ โโโ Player.js # Player classes (Human & AI)
โ โโโ HexGameCLI.js # Command-line interface
โ โโโ HexGameGUI.js # Web-based graphical interface (NEW!)
โ โโโ gui/ # Frontend assets (NEW!)
โ โ โโโ index.html # Main HTML page
โ โ โโโ style.css # CSS styling
โ โ โโโ script.js # Frontend JavaScript
โ โโโ index.js # Entry point
โโโ tests/
โ โโโ HexBoard.test.js # Board logic tests
โ โโโ HexGame.test.js # Game controller tests
โ โโโ Player.test.js # Player logic tests
โโโ package.json
โโโ README.md
Run the complete test suite:
npm testRun tests in watch mode:
npm run test:watchGenerate coverage report:
npm run test:coverageThe AI player includes two difficulty levels:
- Makes random valid moves
- Good for beginners learning the game
- Prioritizes winning moves when available
- Attempts to block opponent's winning moves
- Makes strategic moves toward game objectives
- Prefers center positions for better board control
The game displays a hexagonal board with clear visual indicators:
A B C D E F G H I J K
1 . . . . . . . . . . .
2 . . . . . . . . . . .
3 . . . . . . . . . . .
4 . . . X . . . . . . .
5 . . . . . . . . . . .
6 . . . . . O . . . . .
7 . . . . . . . . . . .
8 . . . . . . . . . . .
9 . . . . . . . . . . .
10 . . . . . . . . . . .
11 . . . . . . . . . . .
.= Empty cellX= Player 1 pieceO= Player 2 piece
npm startThe project follows standard JavaScript conventions with:
- Clear class and method names
- Comprehensive JSDoc comments
- Modular design with separation of concerns
- Extensive test coverage
makeMove(row, col, player)- Place a piece on the boardcheckWin(player)- Check if player has wongetNeighbors(row, col)- Get adjacent cellsgetEmptyCells()- Get all available moves
makeMove(row, col)- Make a move and update game statemakeMoveFromString(moveString)- Make move using notation like "A1"getAIMove()- Get AI's chosen movereset()- Reset game to initial state
start()- Start the web serverhandleNewGame()- Create new game sessionhandleMakeMove()- Process player moveshandleGetAIMove()- Get AI move via APIgetGameStateData()- Get complete game state
Player- Human player representationAIPlayer- AI player with strategic move selection
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run the test suite:
npm test - Commit your changes:
git commit -m 'Add feature' - Push to the branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
-
Web-based UI interfaceโ COMPLETED! - Online multiplayer support
- Advanced AI with minimax algorithm
- Game replay and analysis
- Tournament mode
- Custom board shapes
- Move hints and analysis
- Hex game invented by Piet Hein and independently by John Nash
- Inspired by classic board game implementations
- Built with Node.js and Jest for testing