A browser-based implementation of the classic Battleship game, built as part of The Odin Project's JavaScript curriculum. This project demonstrates object-oriented programming principles, test-driven development, and DOM manipulation.
- Single Player vs AI: Play against a computer opponent with random move generation
- Two Player Mode: Pass-and-play functionality for local multiplayer
- Interactive Ship Placement: Click to place ships manually or use random placement
- Visual Feedback: Animations and color-coded feedback for hits, misses, and sunk ships
- Responsive Design: Clean, ocean-themed interface that works on various screen sizes
Play the game here (Update with your actual GitHub Pages URL)
- JavaScript (ES6+): Core game logic and DOM manipulation
- HTML5: Semantic markup structure
- CSS3: Styling with animations and responsive design
- Webpack: Module bundling and build process
- Jest: Unit testing framework
- ESLint: Code linting and style enforcement
- Babel: JavaScript transpilation
- Each player has a 10x10 grid to place their fleet of ships
- Fleet consists of:
- 1 Carrier (5 squares)
- 1 Battleship (4 squares)
- 2 Cruisers (3 squares each)
- 1 Destroyer (2 squares)
- Ships can be placed horizontally or vertically
- Players take turns attacking coordinates on the opponent's grid
- First player to sink all enemy ships wins
- Node.js (v14 or higher)
- npm
-
Clone the repository
git clone https://github.com/your-username/battle-ship.git cd battle-ship
-
Install dependencies
npm install
-
Start development server
npm start
The game will open in your browser at
http://localhost:8080
-
Run tests
npm test
-
Build for production
npm run build
battle-ship/
├── src/
│ └── myComponents/
│ ├── classes/
│ │ ├── shipClass.js # Ship object with hit/sunk logic
│ │ ├── gameBoardClass.js # Game board management
│ │ └── playerClass.js # Player and AI logic
│ ├── display/
│ │ └── display.js # DOM manipulation and UI
│ ├── test/
│ │ ├── shipClass.test.js # Ship class tests
│ │ ├── gameBoardClass.test.js # GameBoard tests
│ │ └── playerClass.test.js # Player class tests
│ ├── images/ # Game assets
│ ├── style.css # Main stylesheet
│ ├── gameLoop.js # Game flow control
│ ├── index.html # HTML template
│ └── index.js # Entry point
├── dist/ # Built files
├── webpack.common.js # Webpack base config
├── webpack.dev.js # Development config
├── webpack.prod.js # Production config
└── package.json
The project uses Test-Driven Development (TDD) with Jest. Tests cover:
- Ship Class: Hit detection, sinking logic
- GameBoard Class: Ship placement, attack handling, win conditions
- Player Class: Turn management, AI behavior
Run tests with:
npm test
- Ship Class: Manages individual ship state (hits, sunk status)
- GameBoard Class: Handles ship placement, attack processing, and board state
- Player Class: Manages player actions, turns, and AI logic
- Random coordinate selection from available moves
- Prevents duplicate attacks by maintaining a list of possible coordinates
- Simple but effective opponent for single-player mode
- Dynamic board generation and rendering
- Event-driven gameplay with custom events
- Smooth animations for game state changes
- Responsive design adapting to different screen sizes
npm start
: Start development server with hot reloadnpm test
: Run test suite in watch modenpm run build
: Create production buildnpm run push
: Deploy to GitHub Pages
This project demonstrates mastery of:
- ✅ Test-Driven Development: Comprehensive test coverage for core logic
- ✅ Object-Oriented Programming: Clean class structure and encapsulation
- ✅ DOM Manipulation: Dynamic UI updates without frameworks
- ✅ Event-Driven Programming: Custom events and user interactions
- ✅ Module Bundling: Webpack configuration and optimization
- ✅ Code Quality: ESLint configuration and consistent style
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is part of The Odin Project curriculum and is available under the ISC License.
- The Odin Project for the excellent curriculum
- Classic Battleship game for inspiration
- Background image generated with DALL-E
Note: This project was built as a learning exercise. The focus is on demonstrating JavaScript fundamentals, testing practices, and clean code architecture rather than advanced game features.