I'll be going through what I have done to solve this problem.
Easy Enough?
What have I done to solve this?
I created a controller and a board class
This code does not work but is shown to simulate what I want to achieve:
(I am an amateur technically).
My Controller:
app.post("/", (req, res) => {
var board = new Board(chess = new Chess());
// The post request creates a board
board.clearBoard();
// This clears the board of existing pieces
board.placeFirstPosition(position);
// This adds the knight to the board
board.endPosition(position);
// This method in the class here will handle the logic of moving the knight to the end position
// This logic needs to be passed into the model
var move = new Move({
coordinate: (all_the_coordinates)
})
// This data (coordinates) will be passed into the model (via mongoose)
move.save().then((doc) => {
res.send(doc);
}, (e) => {
res.status(400).send(e);
});
// Finally these moves will be saved to the database and will be returned to the server
});
My Board Class:
var Chess = require('../../node_modules/chess.js').Chess;
function Board(chess = new Chess()) {
this._chess = chess;
}
Board.prototype.clearBoard = function() {
this._chess.clear();
}
Board.prototype.placeFirstPosition = function(position) {
this._chess.put({ type: 'k', color: 'w' }, position)
}
Board.prototype.endPosition = function(position) {
// logic calculating the sequence of coordinates
}
Board.prototype.showBoard = function() {
return this._chess.ascii();
}
module.exports = {Board};
My Problem:
The questions are:
board.placeFirstPosition('a1');
Your Advice:
Assuming I am getting this correct, your conditions are:
Stages would be to
Create an algorithm to match your logic
function findBestPath(initPosX, initPosY, destPosX, destPosY)
Create a route to use this function
const express = require('express');
const app = express();
const bodyParser = require('bodyParser');
app.use(bodyParser.json());
app.post('/getBestPath', (req, res) => {
let initPosX = req.body.initPosX,
initPosY = req.body.initPosY,
destPosX = req.body.destPosX,
destPosY = req.body.destPosY;
res.send(findBestPath(initPosX, initPosY, destPosX, destPoxY));
};
app.listen(3000, () => console.log('Example app listening on port 3000!'))
Once you're done with that you can can call this function using a POST HTTP request from any interface you can think of (including postman).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.