Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Endgame Analysis #15809

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions project/APIroute.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Route

object ApiRoutes {
val route: Route =
pathPrefix("api") {
path("analyze" / "tablebase") {
parameters('fen) { fen =>
complete {
TablebaseService.getTablebaseEvaluation(fen).map { result =>
HttpEntity(ContentTypes.`application/json`, s"""{
"bestMove": "${result.bestMove}",
"evaluation": ${result.wdl},
"dtz": ${result.dtz}
}""")
}
}
}
} ~
path("practice" / Segment) { theme =>
get {
val position = PracticeService.generatePracticePosition(theme)
complete(HttpEntity(ContentTypes.`application/json`, s"""{"fen": "$position"}"""))
}
}
}
}
9 changes: 9 additions & 0 deletions project/PracticeService.scala.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object PracticeService {
def generatePracticePosition(theme: String): String = {
theme match {
case "basicKingAndPawn" => "8/8/8/8/8/4P3/4K3/8 w - - 0 1" // King and pawn vs. king
case "rookAndPawn" => "8/8/8/4k3/8/4P3/8/4K2R w - - 0 1" // Rook and pawn vs. king
case _ => throw new IllegalArgumentException("Unknown theme")
}
}
}
26 changes: 26 additions & 0 deletions project/TablebaseService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal

// Case class to parse the tablebase result
case class TablebaseResult(
dtz: Int, // Distance to zeroing (moves to convert to a simpler endgame)
wdl: Int, // Win/draw/loss indicator
bestMove: String
)

object TablebaseService {
def getTablebaseEvaluation(fen: String): Future[TablebaseResult] = {
val url = s"https://api.chessdb.cn:81/tablebase?fen=$fen"
val response = Http().singleRequest(HttpRequest(uri = url))

response.flatMap {
case HttpResponse(StatusCodes.OK, _, entity, _) =>
Unmarshal(entity).to[TablebaseResult]
case _ =>
Future.failed(new Exception("Failed to fetch tablebase data"))
}
}
}
19 changes: 19 additions & 0 deletions ui/analyse/src/AnalysisPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import TablebaseAnalysis from "./TablebaseAnalysis";
import PracticeDrill from "./PracticeDrill";

const AnalysisPage: React.FC = () => {
const [fen, setFen] = useState<string>("start_fen_here");
const [theme, setTheme] = useState<string>("basicKingAndPawn");

return (
<div>
<h2>Game Analysis</h2>
<TablebaseAnalysis fen={fen} />
<h2>Practice Drill</h2>
<PracticeDrill theme={theme} />
</div>
);
};

export default AnalysisPage;
25 changes: 25 additions & 0 deletions ui/analyse/src/PracticeDrill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { useState, useEffect } from "react";
import { Chessboard } from "react-chessboard";

interface PracticeProps {
theme: string;
}

const PracticeDrill: React.FC<PracticeProps> = ({ theme }) => {
const [fen, setFen] = useState<string>("");

useEffect(() => {
fetch(`/api/practice/${theme}`)
.then((response) => response.json())
.then((data) => setFen(data.fen));
}, [theme]);

return (
<div>
<h3>Practice Drill: {theme}</h3>
<Chessboard position={fen} />
</div>
);
};

export default PracticeDrill;
36 changes: 36 additions & 0 deletions ui/analyse/src/TablebaseAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useState, useEffect } from "react";

interface AnalysisProps {
fen: string;
}

const TablebaseAnalysis: React.FC<AnalysisProps> = ({ fen }) => {
const [analysis, setAnalysis] = useState<AnalysisResult | null>(null);

useEffect(() => {
fetch(`/api/analyze/tablebase?fen=${encodeURIComponent(fen)}`)
.then((response) => response.json())
.then((data) => setAnalysis(data));
}, [fen]);

if (!analysis) {
return <div>Loading...</div>;
}

return (
<div>
<h3>Tablebase Analysis</h3>
<p>Best Move: {analysis.bestMove}</p>
<p>Evaluation: {analysis.evaluation}</p>
{analysis.dtz !== undefined && <p>Distance to Zeroing: {analysis.dtz}</p>}
</div>
);
};

export default TablebaseAnalysis;

interface AnalysisResult {
bestMove: string;
evaluation: number;
dtz?: number;
}
Loading