An open API service indexing awesome lists of open source software.

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.

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.

![Tests](https://img.shields.io/badge/tests-15%2F15%20passing-brightgreen)
![JavaScript](https://img.shields.io/badge/language-JavaScript-yellow)
![Jest](https://img.shields.io/badge/testing-Jest-red)
![TDD](https://img.shields.io/badge/methodology-TDD-blue)

## ๐ŸŽฎ 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!** โš“