我将完成我为解决这个问题所做的工作。
够用吗?
我做了什么来解决这个问题?
我创建了一个控制器和一个棋盘类
此代码不起作用,但显示模拟我想要实现的目标:
(技术上我是业余爱好者)。
我的控制器:
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
});
我的董事会成员:
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};
我的问题:
问题是:
board.placeFirstPosition('a1');
您的建议:
答案 0 :(得分:0)
假设我说得对,你的条件是:
阶段将是
创建一个匹配逻辑的算法
function findBestPath(initPosX, initPosY, destPosX, destPosY)
创建使用此功能的路线
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!'))
完成后,您可以使用任何您能想到的界面(包括邮递员)的POST HTTP请求来调用此函数。