Coder Social home page Coder Social logo

battleship's Introduction

battleship

This is a browser-based version of Battleship. It can be played here: https://ken-ux.github.io/battleship/.

How to Play

  1. Click any space on your board to place a ship horizontally. The player is provided 5 ships of the following unit lengths: 5, 4, 3, 3, 2.
  2. After placing ships, click on the computer's board to begin playing.
  3. Win by hitting all the computer's ships before they get yours.

Development Methodology

The main focus for this project was using principles of test-driven development (TDD) to efficiently develop the structure of the code base. This process meant:

  • Writing unit tests with Jest to outline specific goals for discrete modules.
  • Adding code to modules that pass all tests.
  • Revising tests or adding more to test suites as the complexity of the code base increased.

All tests can be found in the tests folder of this project.

Lessons Learned

  • Running Jest with ECMAScript (aka ES6) syntax.
  • Using triangulation to iteratively produce robust unit tests.
  • Handling distinct syntax for specific Jest matchers, such as wrapping functions to ensure thrown errors are recorded through the toThrow() matcher.
  • When pushing instances of my Ship class to an array, its methods wouldn't be available when the instance was referenced from the array. To fix this, I had to dive deeper into Javascript's execution context. I learned that initializing methods in the class as arrow functions binds them, preventing loss of reference to the class.
  • Breaking down tests into smaller units and repeating setups using hooks provided by Jest, such as beforeEach().
  • I was having an issue displaying only the player's ships on their board. By conditionally styling each square on the gameboards with a styleSquare() function, it would spoil the location of the computer's ships too. After trying to write a solution in JavaScript, I realized a simple CSS selector targeting the player's board was the most elegant solution.

Future Considerations

These are further ideas for consideration if this project is picked up again in the future:

  • Let users place ships vertically.
  • Improve the computer's intelligence e.g. having it hit adjacent spaces after it successfully hits a ship.
  • Let two users play against each other.

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.