added computer mode
This commit is contained in:
+11
-5
@@ -6,16 +6,22 @@ const engine = new Engine(
|
||||
"C:\\Users\\MOON\\Downloads\\stockfish-windows-x86-64-avx2\\stockfish\\stockfish-windows-x86-64-avx2.exe"
|
||||
);
|
||||
|
||||
engine.init().then(() => {
|
||||
nextMove({ position: "r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3" });
|
||||
});
|
||||
engine
|
||||
.init()
|
||||
.then((eng) => {
|
||||
return eng.setoption("UCI_LimitStrength", true);
|
||||
})
|
||||
.then((eng) => {
|
||||
eng.setoption("UCI_Elo");
|
||||
});
|
||||
|
||||
const nextMove = async ({ position }) => {
|
||||
await engine.isready();
|
||||
console.log("Chess engine ready");
|
||||
engine.position(position);
|
||||
const result = await engine.go({ depth: 22 });
|
||||
const result = await engine.go({ depth: 10 });
|
||||
console.log("Best move or position", position, "is", result.bestmove);
|
||||
return result.bestmove;
|
||||
};
|
||||
|
||||
module.exports = {};
|
||||
module.exports = { nextMove };
|
||||
|
||||
@@ -3,6 +3,7 @@ const { SOCKET_EVENTS } = require("./constants");
|
||||
const { Chess } = require("chess.js");
|
||||
const { User } = require("./models/user");
|
||||
const { Game } = require("./models/game");
|
||||
const { nextMove } = require("./chessbot");
|
||||
const {
|
||||
CHESS_MOVE,
|
||||
CHESS_OPPONENT_MOVE,
|
||||
@@ -61,6 +62,33 @@ function socketIOServerInit(server) {
|
||||
},
|
||||
});
|
||||
|
||||
const ioBot = io.of("/chessbot");
|
||||
|
||||
ioBot.on("connection", (socket) => {
|
||||
let id = socket.id;
|
||||
console.log(id, "connected on /chessbot");
|
||||
const chess = new Chess();
|
||||
|
||||
socket.onAny((evt) => {
|
||||
console.log(evt);
|
||||
});
|
||||
|
||||
socket.on("disconnect", (reason) => {
|
||||
console.log(id, "disconnected due to", reason);
|
||||
});
|
||||
|
||||
socket.on(CHESS_MOVE, async (roomID, moveData) => {
|
||||
console.log("CHESS_MOVE");
|
||||
chess.move(moveData);
|
||||
let move = moveData.from + moveData.to;
|
||||
console.log(move);
|
||||
const botMove = await nextMove({ position: chess.fen() });
|
||||
console.log({ from: botMove.substring(0, 2), to: botMove.substring(2) });
|
||||
chess.move({ from: botMove.substring(0, 2), to: botMove.substring(2) });
|
||||
socket.emit("CHESS_BOT_MOVE", { from: botMove.substring(0, 2), to: botMove.substring(2) });
|
||||
});
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
let id = socket.id;
|
||||
console.log(socket.id, "connected");
|
||||
|
||||
Reference in New Issue
Block a user