Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/mano-08/battleship

Play battleship online with friends!
https://github.com/mano-08/battleship

game game-development next-js nextjs nextjs14 real-time realtime socket-io socket-programming tailwindcss typescript

Last synced: about 1 month ago
JSON representation

Play battleship online with friends!

Awesome Lists containing this project

README

        

# **BATTLESHIP**


> Dominate the seas, one battle at a time
Sink your enemies before they sink you!


- [x] 2 Player online game: Create your own game rooms and invite your friends
- [x] Choose your strategy: randomize your ship placement or carefully position your fleet manually!
- [x] Compete for glory: track your performance and rise through the ranks on the leaderboard!


![Main Page of BattleShip](https://github.com/user-attachments/assets/148cd638-0392-45d3-aa22-1e47aef3fd96)

![Gameplay screenshot](https://github.com/user-attachments/assets/a6ca17ad-3f78-4fce-ac68-478c815a65da)

### Tech stack

---

- Next.js
- Socket.io
- Node.js
- Express.js
- Firebase

### Bot's Coordinate Guesser Logic

---

An intelligent coordinate guesser algorithm has been designed for the robot to predict coordinates that leverages on the previous HIT
coordinates along with predicted direction of ship alignment (vertical or horizontal) where the ship was HIT but not WRECKED.




Data Structure of the HIT Stack:

```
{
row: number;
col: number;
shipId: string;
direction: "vertical" | "horizontal" | null;
}[]
```



The Algorithm is designed based on the following ideology:





  • First guess is random, if it is a HIT, then push it into HIT Stack with direction set to null.


  • If there is data on HIT Stack, the next guess is around the most recent hit, i.e. the top element of stack.


  • While dropping torpedo around valid coordinates of most recent HIT, if
    we miss the guess then we would swap predicted direction from vertical to horizontal or vice versa.


  • In case if there are no valid coordinates around the most recent HIT, there again are 2 cases:


    1. Case 1: If there are successive hits on my
      HIT Stack, then the next guess is along the direction of
      Successive hits, this way we reach the other end of the
      ship.


    2. Case 2: Else we just swap our predicted
      direction from horizontal to vertical or vertical to
      horizontal.




  • When a ship is wrecked completely, remove it's coordinates
    from the HIT Stack.


  • If a ship is wrecked and no more coordinates are present inside
    HIT Stack, just go with random guess.




You can find the code of the Coordinate guesser 

here

.


Learn more about this project on 
github
 and find me on 
twitter

.