diff options
author | Joffrey Bion <joffrey.bion@gmail.com> | 2021-06-18 00:18:53 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@gmail.com> | 2021-06-19 19:06:30 +0200 |
commit | f00cb4f968e0fb7050bae4145e1327e206e268e2 (patch) | |
tree | 2a8a0a42944ffa6c69dcbf75c3e152466cac91f3 /sw-common-model/src/commonMain | |
parent | Upgrade setup-java Github action to v2 (diff) | |
download | seven-wonders-f00cb4f968e0fb7050bae4145e1327e206e268e2.tar.gz seven-wonders-f00cb4f968e0fb7050bae4145e1327e206e268e2.tar.bz2 seven-wonders-f00cb4f968e0fb7050bae4145e1327e206e268e2.zip |
Fix ex-aequo ranks
When 2 players are ex-aequo, the rank of the next player is
still supposed to take into account the 2 players in front
of thim. So if 2 players are 2nd ex-aequo, the next guy is
supposed to be 4th, not 3rd.
Diffstat (limited to 'sw-common-model/src/commonMain')
-rw-r--r-- | sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/score/Score.kt | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/score/Score.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/score/Score.kt index 36d0857f..9b0c5fb6 100644 --- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/score/Score.kt +++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/score/Score.kt @@ -4,18 +4,16 @@ import kotlinx.serialization.Serializable @Serializable data class ScoreBoard(val scores: List<PlayerScore>) { + init { + require(scores.sortedDescending() == scores) { "Scores must be sorted highest-to-lowest" } + } @OptIn(ExperimentalStdlibApi::class) val ranks: List<Int> - get() = buildList { - var r = 1 + get() = buildList<Int> { add(1) - for (i in 1..scores.lastIndex) { - if (scores[i] < scores[i - 1]) { - add(++r) - } else { - add(r) - } + scores.zipWithNext { prev, current -> current.compareTo(prev) == 0 }.forEach { exAequoWithPrev -> + add(if (exAequoWithPrev) last() else size + 1) } } } |