Search code examples
node.jschessboard.js

Creating a chess board that calculates moves in backend in NodeJS


This is my first time using NodeJS so I apologize in advance if this is a noob question.

I want to build a NodeJS chess app where the user can play the computer, but the computer's moves are calculated on the backend. I've been trying to get chessboardjs to work, but I can't get much further than displaying a completely static start position. I'm using EJS to render the view. Here's what I've got so far:

  <%- include('_header') %>

  <link rel="stylesheet" href="css/chessboard-1.0.0.min.css">
  <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script src="js/chessboard-1.0.0.min.js"></script>

  <center>
    <h1>chess app</h1>

    <div id="board" style="width: 400px"></div>
    <script src='js/board.js'></script>

  </center>

<%- include('_footer') %>

and board.js contains exactly the code from https://chessboardjs.com/examples#5001 (for now, random moves will suffice, I just want to figure out how to get this to work on the backend). Also, how exactly should I go about integrating chess.js? The tutorial completely skips it but I have no clue where to start on that.

Thank you in advance for your help! I really really appreciate it.


Solution

  • Generally, games have a certain number of things you need to manage like current state, game loops, etc. This state ideally live on the client side and persisted on the serverside periodically. So i feel since there is no backend code and there's isn't much info here, i suggest you start with a simpler game, react website had a tic-tac-toe tutorial if you are familiar with react that would be a good starting point to understand game development in general. Then i would recommend this book it has an excellent chapter on developing a browser game. https://eloquentjavascript.net/

    Now specifically for chess, we have something called a chess engine which processes the current state and suggests moves like stockfish which is a very complex and not something a newbie can implement