Giter Site home page Giter Site logo

battleships-minigame's Introduction

Battleships

This is a small battleships mini-game (Touché-Coulé in 🇫🇷)

Demo

Get started

I took the opportunity to test pnpm and Vite.js. To install pnpm, run npm install -g pnpm or swap it for a more vanilla npm.

npm install -g pnpm

To run the application:

pnpm run preview

Architecture

The game has roughly two parts: useBattleshipMiniGame that encapsulates all the logic and components/Board for the UI.

useBattleshipMiniGame

This is the main hook implementing the logic of the game. It will generate the board, place the ships at random position/orientation and let the user take shots.

  • cells: this is the board, each cell has a name and an id , i.e. {name: "D7", id: 64};
  • fireShot: function that takes a string such as "A7", parses it into coordinate/cell index and store the shot;
  • fleet: array of ships that have been placed on the board and which cells they occupy;
  • hasGameEnded: are all the ship destroyed?
  • restart: function to reset the board, shots taken and shuffle the ships;
  • shots: array of shots taken, which cells have been played by the user;
  • validate: function that validates the user input;

Board component

This is the main UI component that will render the board.

For each cells, it will check the state of the cell checking if a shot as be taken at that position and if a ship has then been hit (or if it is a miss).

A note on using a state reducer pattern

Another approach would be to use a state reducer pattern. This would have the benefit to be more scalable, move the logic outside of the UI component and add a few other features like keeping a score or implement a history/rollback feature and more.

battleships-minigame's People

Contributors

adrienrn avatar

Stargazers

Roman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.