Skip to content

Commit e0c40af

Browse files
committed
Determine weakest score
1 parent 22191c4 commit e0c40af

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

Stockfish/SFMWindowController.m

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,34 +208,58 @@ - (BOOL)textView:(NSTextView *)textView clickedOnLink:(id)link atIndex:(NSUInteg
208208
}
209209

210210
/**
211-
Maps scores from [minScore, maxScore] to [kMinWeight, kMaxWeight]
211+
Maps scores from [weakestScore, strongestScore] to [kMinWeight, kMaxWeight].
212+
Agnostic about Black vs White scores; Black's strongest score is more negative, whereas White's strongest score is more positive, etc.
212213
*/
213-
- (CGFloat)weightFromScore:(const CGFloat)score minScore:(const CGFloat)minScore maxScore:(const CGFloat)maxScore {
214-
if(maxScore == minScore) {
214+
- (CGFloat)weightFromScore:(const CGFloat)score weakestScore:(const CGFloat)weakestScore strongestScore:(const CGFloat)strongestScore {
215+
if(strongestScore == weakestScore) {
215216
return kMaxWeight;
216217
}
217218

218-
const CGFloat scoreProportion = (score - minScore) / (maxScore - minScore); // maps [minScore, maxScore] -> [0, 1]
219+
const CGFloat scoreProportion = (score - weakestScore) / (strongestScore - weakestScore); // maps [weakestScore, strongestScore] -> [0, 1]
219220
const CGFloat weight = (kMaxWeight - kMinWeight) * scoreProportion + kMinWeight; // [0, 1] -> [minWeight, maxWeight]
220221

221222
return weight;
222223
}
223224

225+
- (CGFloat)weakestScoreFromLines:(NSDictionary<NSNumber *, SFMUCILine *> *const)linesDict {
226+
const CGFloat strongestScore = linesDict[@(1)].score;
227+
const CGFloat maybeWeakestScore = linesDict[@(linesDict.count)].score; // not necessarily the weakest score
228+
229+
CGFloat weakestScore = maybeWeakestScore;
230+
// White's favor
231+
if(maybeWeakestScore < strongestScore) {
232+
for(SFMUCILine *const line in [linesDict allValues]) {
233+
if(line.score < weakestScore) {
234+
weakestScore = line.score;
235+
}
236+
}
237+
} else { // Black's favor
238+
for(SFMUCILine *const line in [linesDict allValues]) {
239+
if(line.score > weakestScore) {
240+
weakestScore = line.score;
241+
}
242+
}
243+
}
244+
245+
return weakestScore;
246+
}
247+
224248
- (void)updateArrowsFromLines:(NSDictionary<NSNumber *, SFMUCILine *> *const)linesDict {
225249
if(![SFMUserDefaults arrowsEnabled]) {
226250
return;
227251
}
228252

229253
NSMutableArray<SFMArrowMove *> *const arrowMoves = [NSMutableArray new];
230254

231-
const CGFloat maxScore = linesDict[@(1)].score;
232-
const CGFloat minScore = linesDict[@(linesDict.count)].score; // bug: not necessarily the min score
255+
const CGFloat strongestScore = linesDict[@(1)].score;
256+
const CGFloat weakestScore = [self weakestScoreFromLines:linesDict];
233257

234258
for (SFMUCILine *const line in [linesDict allValues]) {
235259
if(line.moves.count) {
236260
const CGFloat weight = [self weightFromScore:line.score
237-
minScore:minScore
238-
maxScore:maxScore];
261+
weakestScore:weakestScore
262+
strongestScore:strongestScore];
239263
SFMArrowMove *const arrowMove = [[SFMArrowMove alloc]
240264
initWithMove:[line.moves firstObject]
241265
weight:weight];

0 commit comments

Comments
 (0)