diff --git a/backend/chessbot.js b/backend/chessbot.js
index 2d7b93c..988d181 100644
--- a/backend/chessbot.js
+++ b/backend/chessbot.js
@@ -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 };
diff --git a/backend/socket.js b/backend/socket.js
index 341bb4c..4c2739f 100644
--- a/backend/socket.js
+++ b/backend/socket.js
@@ -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");
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index 86566e2..74a3160 100644
--- a/frontend/src/App.jsx
+++ b/frontend/src/App.jsx
@@ -11,10 +11,11 @@ import PlayFriend from './pages/Play/PlayFriend'
import Play from './pages/Play/Play'
import AuthenticationPage from './pages/Authentication/Authentication'
import ChallengeFriend, { playFriendAction } from './pages/Play/ChallengeFriend'
-import ChessGame from './pages/Chess/ChessGame'
import Profile, { action as profileAction } from './pages/Settings/Profile'
-import ChessGameContextProvider from './context/chess-game-context'
import { getAuthToken, getUserData } from './utils/auth'
+import Computer from './pages/Play/Computer'
+import ComputerGame from './pages/Play/ComputerGame'
+import MultiplayerGame from './pages/Play/MultiplayerGame'
const router = createBrowserRouter([{
path: '/',
@@ -28,17 +29,15 @@ const router = createBrowserRouter([{
{ index: true, element: