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

feat: scale arrows in analysis based on winning chances #828

Merged
merged 1 commit into from
Jul 11, 2024
Merged
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
37 changes: 28 additions & 9 deletions lib/src/model/common/eval.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math' as math;

import 'package:collection/collection.dart';
import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
Expand Down Expand Up @@ -47,6 +48,16 @@ class ExternalEval with _$ExternalEval implements Eval {
}
}

double _toWhiteWinningChances(int? cp, int? mate) {
if (mate != null) {
return mateWinningChances(mate);
} else if (cp != null) {
return cpWinningChances(cp);
} else {
return 0;
}
}

/// The eval from the client's own engine, typically stockfish.
@freezed
class ClientEval with _$ClientEval implements Eval {
Expand All @@ -71,10 +82,11 @@ class ClientEval with _$ClientEval implements Eval {
return Move.fromUci(uci);
}

IList<Move?> get bestMoves {
IList<MoveWithWinningChances> get bestMoves {
return pvs
.where((e) => e.moves.isNotEmpty)
.map((e) => Move.fromUci(e.moves.first))
.map((e) => e.firstMoveWithWinningChances(position.turn))
.whereNotNull()
.toIList();
}

Expand All @@ -89,13 +101,7 @@ class ClientEval with _$ClientEval implements Eval {
double winningChances(Side side) => _toPov(side, _whiteWinningChances);

double get _whiteWinningChances {
if (mate != null) {
return mateWinningChances(mate!);
} else if (cp != null) {
return cpWinningChances(cp!);
} else {
return 0;
}
return _toWhiteWinningChances(cp, mate);
}
}

Expand Down Expand Up @@ -136,8 +142,21 @@ class PvData with _$PvData {
}
return res;
}

MoveWithWinningChances? firstMoveWithWinningChances(Side sideToMove) {
final uciMove = (moves.isNotEmpty) ? Move.fromUci(moves.first) : null;
return (uciMove != null)
? (
move: uciMove,
winningChances:
_toPov(sideToMove, _toWhiteWinningChances(cp, mate)),
)
: null;
}
}

typedef MoveWithWinningChances = ({Move move, double winningChances});

double cpToPawns(int cp) => cp / 100;

int cpFromPawns(double pawns) => (pawns * 100).round();
Expand Down
93 changes: 60 additions & 33 deletions lib/src/view/analysis/analysis_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,65 @@ class _Board extends ConsumerStatefulWidget {
class _BoardState extends ConsumerState<_Board> {
ISet<cg.Shape> userShapes = ISet();

ISet<cg.Shape> _computeBestMoveShapes(IList<MoveWithWinningChances> moves) {
// Scale down all moves with index > 0 based on how much worse their winning chances are compared to the best move
// (assume moves are ordered by their winning chances, so index==0 is the best move)
double scaleArrowAgainstBestMove(int index) {
final bestMove = moves[0];
final winningDiffComparedToBestMove =
bestMove.winningChances - moves[index].winningChances;
const minScale = 0.25;
// Force minimum scale if...
// 1) The best move is significantly better than this move
// 2) The signs differ, e.g. the best move is winning while this move draws (or even looses)
if (winningDiffComparedToBestMove > 0.3 ||
bestMove.winningChances.sign != moves[index].winningChances.sign) {
return minScale;
}
const winningDiffToScaleDiff = 5.0;
const maxScale = 1.0;
return math.max(
minScale,
maxScale - winningDiffToScaleDiff * winningDiffComparedToBestMove,
);
}

return ISet(
moves.mapIndexed(
(i, m) {
final move = m.move;
// Same colors as in the Web UI, the best move has a different color than the other moves
final color = Color((i == 0) ? 0x40003088 : 0x404A4A4A);
switch (move) {
case NormalMove(from: _, to: _, promotion: final promRole):
return [
cg.Arrow(
color: color,
orig: move.cg.from,
dest: move.cg.to,
scale: scaleArrowAgainstBestMove(i),
),
if (promRole != null)
cg.PieceShape(
color: color,
orig: move.cg.to,
role: promRole.cg,
),
];
case DropMove(role: final role, to: _):
return [
cg.PieceShape(
color: color,
orig: move.cg.to,
role: role.cg,
),
];
}
},
).expand((e) => e),
);
}

@override
Widget build(BuildContext context) {
final ctrlProvider = analysisControllerProvider(widget.pgn, widget.options);
Expand All @@ -402,39 +461,7 @@ class _BoardState extends ConsumerState<_Board> {
final ISet<cg.Shape> bestMoveShapes = showBestMoveArrow &&
analysisState.isEngineAvailable &&
bestMoves != null
? ISet(
bestMoves.where((move) => move != null).mapIndexed(
(i, move) {
switch (move!) {
case NormalMove(from: _, to: _, promotion: final promRole):
return [
cg.Arrow(
color:
const Color(0x40003088).withOpacity(0.4 - 0.15 * i),
orig: move.cg.from,
dest: move.cg.to,
),
if (promRole != null)
cg.PieceShape(
color: const Color(0x40003088)
.withOpacity(0.4 - 0.15 * i),
orig: move.cg.to,
role: promRole.cg,
),
];
case DropMove(role: final role, to: _):
return [
cg.PieceShape(
color:
const Color(0x40003088).withOpacity(0.4 - 0.15 * i),
orig: move.cg.to,
role: role.cg,
),
];
}
},
).expand((e) => e),
)
? _computeBestMoveShapes(bestMoves)
: ISet();

return cg.Board(
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ packages:
dependency: "direct main"
description:
name: chessground
sha256: "134a6eb0f0eaf9dbd67f32cbf30aa9921401d98797c2274fefafa815f18f54b5"
sha256: "2c2a4de4161a77bb87919f27eff56cb08724a8a240da62d59f56f403ab3a9e27"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
version: "3.1.1"
ci:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
async: ^2.10.0
cached_network_image: ^3.2.2
chessground: ^3.1.0
chessground: ^3.1.1
collection: ^1.17.0
connectivity_plus: ^6.0.2
cronet_http: ^1.3.1
Expand Down