https://github.com/singharyan006/battleship
A classic Battleship game built with JavaScript using Test-Driven Development (TDD). Features intelligent AI, complete game logic, and 100% test coverage with Jest.
https://github.com/singharyan006/battleship
ai battleship es6-modules game-development javascript jest tdd terminal-game the-odin-project unit-testing
Last synced: about 2 months ago
JSON representation
A classic Battleship game built with JavaScript using Test-Driven Development (TDD). Features intelligent AI, complete game logic, and 100% test coverage with Jest.
- Host: GitHub
- URL: https://github.com/singharyan006/battleship
- Owner: singharyan006
- License: mit
- Created: 2025-07-17T06:28:24.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-19T07:18:07.000Z (12 months ago)
- Last Synced: 2025-07-19T10:34:36.100Z (12 months ago)
- Topics: ai, battleship, es6-modules, game-development, javascript, jest, tdd, terminal-game, the-odin-project, unit-testing
- Language: JavaScript
- Homepage:
- Size: 63.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ข Battleship Game
A classic Battleship game implemented in JavaScript using **Test-Driven Development (TDD)** methodology. Features a complete game engine with ship placement, attack tracking, player management, and terminal-based gameplay.




## ๐ฎ Features
### Core Game Logic
- โ **Ship Module**: Ship creation, hit detection, and sink status
- ๐ฏ **Gameboard**: 10x10 grid with ship placement and attack tracking
- ๐ค **Player System**: Human and computer players with move validation
- ๐ค **AI Player**: Intelligent computer opponent with move tracking
- ๐ฎ **Game Controller**: Turn-based gameplay logic
- ๐ป **Terminal Interface**: Command-line game runner
### Development Features
- โ
**100% Test Coverage**: 15 comprehensive tests across 4 test suites
- ๐ **TDD Methodology**: Built using test-driven development
- ๐ฆ **ES Modules**: Modern JavaScript module system
- ๐งช **Jest Testing**: Comprehensive unit testing suite
- ๐ง **Babel Integration**: ESM compatibility for Jest
## ๐ Quick Start
### Prerequisites
- Node.js (v14+ recommended)
- npm or yarn
### Installation
```bash
# Clone the repository
git clone https://github.com/singharyan006/battleship.git
cd battleship
# Install dependencies
npm install
# Run tests
npm test
# Play the game (if terminal interface is complete)
npm start
```
## ๐๏ธ Project Structure
```
battleship/
โโโ src/
โ โโโ ship.js # Ship creation and management
โ โโโ gameboard.js # Game board logic and ship placement
โ โโโ player.js # Player factory (human/computer)
โ โโโ ai.js # AI player logic and move generation
โ โโโ game.js # Game controller and turn management
โ โโโ index.js # Terminal game interface
โโโ tests/
โ โโโ ship.test.js # Ship module tests
โ โโโ gameboard.test.js # Gameboard tests
โ โโโ player.test.js # Player system tests
โ โโโ game.test.js # Game logic tests
โโโ .babelrc # Babel configuration for ESM
โโโ jest.config.js # Jest testing configuration
โโโ package.json # Project dependencies and scripts
โโโ README.md # Project documentation
```
## ๐งช Testing
This project was built using **Test-Driven Development (TDD)**, with comprehensive test coverage:
```bash
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
```
### Test Coverage
- **Ship Module**: 3 tests - Creation, hit detection, sink status
- **Gameboard Module**: 5 tests - Placement, attacks, win conditions
- **Player Module**: 4 tests - Human/computer moves, validation
- **Game Module**: 3 tests - Turn management, game flow
**Total: 15/15 tests passing โ
**
## ๐ฏ Game Rules
### Classic Battleship
1. Each player has a 10x10 grid
2. Ships are placed on the grid (various sizes)
3. Players take turns attacking coordinates
4. First player to sink all opponent ships wins
### Ship Types (Standard)
- Carrier (5 spaces)
- Battleship (4 spaces)
- Cruiser (3 spaces)
- Submarine (3 spaces)
- Destroyer (2 spaces)
## ๐ง Development
### Built With TDD
1. **Red**: Write failing test
2. **Green**: Write minimal code to pass
3. **Refactor**: Improve code while keeping tests green
### Git Workflow
- Feature branches for each module
- Comprehensive commit messages
- Merge to main after testing
### Recent Development
- โ
Implemented core Ship module
- โ
Built Gameboard with attack tracking
- โ
Created Player system (human + AI)
- โ
Added dedicated AI module with smart move tracking
- โ
Added Game controller logic
- โ
Built terminal interface
- โ
Fixed coordinate parameter bug
- โ
Enhanced computer player with move validation
- โ
Achieved 100% test coverage
## ๐ Known Issues
- Terminal interface may need refinement for better UX
- Ship placement validation could be enhanced
## ๐ Future Enhancements
- [ ] Enhanced AI strategy (hunt/target mode after hits)
- [ ] Web-based GUI interface
- [ ] Multiplayer support
- [ ] Advanced ship placement validation
- [ ] Game statistics tracking
- [ ] Save/load game functionality
- [ ] AI difficulty levels
## ๐ค Contributing
1. Fork the repository
2. Create a feature branch (`git checkout -b feat/amazing-feature`)
3. Write tests for your feature (TDD approach)
4. Implement the feature
5. Ensure all tests pass (`npm test`)
6. Commit your changes (`git commit -m 'feat: add amazing feature'`)
7. Push to the branch (`git push origin feat/amazing-feature`)
8. Open a Pull Request
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐จโ๐ป Author
**Aryan Singh** - [@singharyan006](https://github.com/singharyan006)
## ๐ Acknowledgments
- Built as part of [The Odin Project](https://www.theodinproject.com/) curriculum
- Test-driven development methodology
- Jest testing framework
- Modern JavaScript ES Modules
---
*"The way to get started is to quit talking and begin doing." - Walt Disney*
๐ข **Happy Sailing!** โ