-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So we basically created a file in the folder api
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
1 parent
af3e515
commit e16f70c
Showing
3 changed files
with
58 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters