A javascript implementation of the classic Battleship board game, focusing on Test-Driven Development (TDD) principles using Jest.
Live Link: https://codedstrings.github.io/battleship/
This project is an implementation of the classic Battleship game where players strategically place ships on a grid and take turns guessing the location of their opponent's ships. The game features:
- Ship placement with both random and manual with orientation selection (horizontal/vertical)
- Turn-based gameplay against a computer opponent
- Visual feedback for hits and misses
- Game state tracking and win conditions
- JavaScript (ES6+)
- Jest (for TDD)
- Webpack (for bundling)
- HTML5 & CSS3
The Battleship game follows these core rules:
- Players take turns trying to hit their opponent's ships
- Ships are placed on a 10x10 grid
- When all ships of a player are sunk, the game ends
- The game includes ships of different lengths.
- A modern web browser
-
Clone the repository
git clone https://github.com/codedstrings/battleship.git
-
Install NPM packages
npm install
-
Run development server
npm run dev
-
To Build
npm run build
-
To Deploy
- Make sure you have all your work committed. You can use git status to see if there’s anything that needs committing.
- Run
git checkout gh-pages && git merge main --no-edit
to change branch and sync your changes from main so that you’re ready to deploy. - Build the application into Dist using:
npm run build
- Commit the dist folder:
git add dist -f && git commit -m "Deployment commit"
- Deploy using :
npm run deploy
The project implements the following modules:
Ships have properties for length, hit count, and sunk status, with methods to:
- Record hits
- Report sink status
The game board manages the 10x10 grid and ships with functionality to:
- Place ships at specific coordinates (horizontally or vertically)
- Validate ship placement to prevent overlap and out-of-bounds placement
- Receive attacks and record hits/misses
- Report if all ships are sunk
Players manage their own game boards and can:
- Attack enemy boards at specific coordinates
- Automatically place ships randomly
- Track ships and game state
A specialized player type that:
- Makes random attack moves
- Avoids repeating the same move
- Automatically places ships randomly
Manages game flow with functionality to:
- Initialize the game with player and computer
- Handle turn switching
- Process attack logic
- Determine game completion and winners
Handles all DOM interaction including:
- Rendering game boards
- Processing user clicks
- Ship placement interface
- Visual feedback for hits and misses
- Game status updates
- Winner announcement
Potential improvements include:
- Two-player mode
- Drag and drop ship placement
- Enhanced AI strategy with targeted attacks
- Different ship configurations
- Sound effects and animations
- Mobile-responsive design
- The Odin Project for the project requirements and guidance