A browser-based implementation of the classic Battleship game, built as a project for The Odin Project JavaScript curriculum. This project showcases modern JavaScript development techniques, including object-oriented programming, test-driven development (TDD), and asynchronous programming.
- Interactive Gameplay: Players can place ships on their board and take turns attacking the opponent's board, with visual feedback for hits, misses, and sunken ships.
- Single-Player Mode: Play against a computer opponent that randomly places ships and makes attacks when "Computer" is entered as Player 2's name.
- Responsive UI: A clean, intuitive interface with dynamic DOM manipulation for rendering game boards, ship placement UI, and attack animations.
- Player Privacy: Includes a "Hide Screen" feature to conceal the current player's board during multiplayer sessions, ensuring fair play.
- Form Validation: Ensures valid player names are entered before starting the game.
Play the game online at https://nikemman.github.io/battleship/.
The codebase is modular and organized for maintainability:
- Classes:
Ship: Represents a ship with properties like type, size, and hit status.Gameboard: Manages the game board, including ship placement, attacks, and tracking of hits and misses.Player: Handles player-specific data and interactions, such as name and board state.
- Utility Files:
painter.js: Handles all DOM rendering, including game boards, ship placement UI, and attack animations.event.js: Manages user interactions, such as click events for ship placement and attacks.
- Tests: Each class has corresponding test files written using Jest, ensuring robust functionality through TDD.
-
Clone the repository:
git clone https://github.com/nikemman/battleship.git
-
Navigate to the project directory:
cd battleship -
Install dependencies for testing:
npm install
-
Install jest:
npm install --save-dev jest
-
Run the tests:
npm test -
Open
index.htmlin a browser to play the game locally, or visit the live demo.
- Start the Game:
- Click the "New Game" button.
- Enter names for Player 1 and Player 2 in the form. Leave Player 2 blank or enter "Computer" for single-player mode against a random AI opponent.
- Place Ships:
- Each player takes turns placing their ships on their 10x10 grid.
- Select a ship and choose its orientation (horizontal or vertical) before clicking a cell to place it.
- Gameplay:
- Players alternate attacking the opponent's board by clicking a cell.
- After Player 1's attack, a "Hide Screen" button appears. Click it to conceal the board, then click "Switch Player" to allow Player 2 to attack.
- In single-player mode, the computer automatically places ships and attacks randomly.
- Winning:
- The game ends when one player's fleet is completely sunk. A victory screen displays the winner and offers a "New Battle" button to restart.
- Test-Driven Development (TDD): Classes were developed using TDD, with tests written before implementation to ensure robust and reliable code.
- Separation of Concerns: Logic is modularized, with DOM manipulation in
painter.js, event handling inevent.js, and game logic in class-specific files. - Asynchronous Programming: The attack animation in
painter.jsusesasync/awaitto provide smooth visual feedback. - Object-Oriented Programming: Encapsulates game entities (ships, gameboards, players) in classes for clean and maintainable code.
- Form Input Validation: Ensures valid player inputs to prevent errors during gameplay.
- JavaScript (ES6+): Core language for game logic and interactivity.
- HTML5/CSS3: For the game interface and styling.
- Jest: For unit testing the core game logic.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Make your changes and commit them (
git commit -m "Add your feature"). - Push to your branch (
git push origin feature/your-feature). - Open a pull request with a detailed description of your changes.
- The Odin Project for providing the project inspiration and curriculum.
- Icons used in the game are sourced from Unicode emoji characters.