Skip to content

Commit

Permalink
So we basically created a file in the folder api
Browse files Browse the repository at this point in the history
called chess-engine.ts and moved the functions from
api.ts and redid them in chess-engine. After that,
we updated api.ts with new code we had in
chess-engine. Then we did the same in the common
folder. We moved the functions that was needed
from common to chessEngine.
  • Loading branch information
adamabsa123 committed Feb 10, 2024
1 parent af3e515 commit e16f70c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
51 changes: 51 additions & 0 deletions src/common/chess-engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// engine.ts

import { Chess, Square } from "chess.js";
import { GameFinishedReason } from "./game-end";

export class ChessEngine {
private chess: Chess;

constructor() {
this.chess = new Chess();
}

reset() {
this.chess.reset();
}

getMoves(passedSquare?: Square) {
return this.chess.moves({ square: passedSquare, verbose: true });
}

get fen() {
return this.chess.fen();
}

makeMove(from: string, to: string) {
this.chess.move({ from, to });
console.log("Chess engine updated:", this.chess.fen());
// Additional logging as needed
}

getGameFinishedReason(): GameFinishedReason {
if (this.chess.isCheckmate()) {
// If it's your turn, you lost
return this.chess.turn() === "w" ?
GameFinishedReason.WHITE_CHECKMATED
: GameFinishedReason.BLACK_CHECKMATED;
} else if (this.chess.isStalemate()) {
return GameFinishedReason.STALEMATE;
} else if (this.chess.isThreefoldRepetition()) {
return GameFinishedReason.THREEFOLD_REPETITION;
} else if (this.chess.isDraw()) {
return this.chess.isInsufficientMaterial() ?
GameFinishedReason.INSUFFICIENT_MATERIAL
: GameFinishedReason.FIFTY_MOVES;
}
throw new Error("Failed to find game over reason.");
}

// Add other methods with logging as necessary
}
// pass them in a square, optional paramater in the square. Properties callmoves
18 changes: 0 additions & 18 deletions src/common/game-end.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ export enum GameFinishedReason {
// FIVEFOLD_REPETITION,
}

export function getGameFinishedReason(chess: Chess): GameFinishedReason {
if (chess.isCheckmate()) {
// If it's your turn, you lost
return chess.turn() === "w" ?
GameFinishedReason.WHITE_CHECKMATED
: GameFinishedReason.BLACK_CHECKMATED;
} else if (chess.isStalemate()) {
return GameFinishedReason.STALEMATE;
} else if (chess.isThreefoldRepetition()) {
return GameFinishedReason.THREEFOLD_REPETITION;
} else if (chess.isDraw()) {
return chess.isInsufficientMaterial() ?
GameFinishedReason.INSUFFICIENT_MATERIAL
: GameFinishedReason.FIFTY_MOVES;
}
throw new Error("Failed to find game over reason.");
}

/**
* A reason for a game to be stopped outside the normal flow of moves.
*/
Expand Down
14 changes: 7 additions & 7 deletions src/server/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WebsocketRequestHandler } from "express-ws";
import { aiMove } from "js-chess-engine";
import { Router } from "express";
import { Chess } from "chess.js";
import { ChessEngine } from "../../common/chess-engine";

import { parseMessage } from "../../common/parse-message";
import { StartGameMessage, StopGameMessage } from "../../common/game-message";
Expand All @@ -17,7 +17,7 @@ const manager = new PieceManager([]);
const executor = new CommandExecutor();
const tcpServer = new TCPServer();

let chess: Chess | null = null;
let chess: ChessEngine | null = null;
let difficulty = 0;

/**
Expand All @@ -39,7 +39,7 @@ export const websocketHandler: WebsocketRequestHandler = (ws, req) => {
console.log(message);

if (message instanceof StartGameMessage) {
chess = new Chess();
chess = new ChessEngine();
// TODO: add message.isWhite so we can determine whether we need to make the first move
if (message.gameType === GameType.COMPUTER) {
difficulty = message.difficulty!;
Expand All @@ -53,18 +53,18 @@ export const websocketHandler: WebsocketRequestHandler = (ws, req) => {
throw new Error("Game must be started first.");
}

chess.move({ from: message.from, to: message.to });
chess.makeMove(message.from, message.to);

if (chess.isGameOver()) {
if (chess.getGameFinishedReason() != undefined) {
// Game is naturally finished; we're done
return;
}

// Absolutely unhinged api design
const val = Object.entries(aiMove(chess.fen(), difficulty))[0];
const val = Object.entries(aiMove(chess.fen, difficulty))[0];
const from = val[0].toLowerCase();
const to = (val[1] as string).toLowerCase();
chess.move({ from, to });
chess.makeMove(from, to);

ws.send(new MoveMessage(from, to).toJson());
} else if (message instanceof DriveRobotMessage) {
Expand Down

0 comments on commit e16f70c

Please sign in to comment.