Pairs Challenge: boards in a row against one clock, growing from 4x3 to 6x5, its own High Scores

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 09:41:38 +00:00
co-authored by Claude Opus 5.5
parent 9aa8d23e90
commit 85c5e08803
3 changed files with 121 additions and 24 deletions
@@ -15,6 +15,7 @@ import tv.orbit.classic.arcade.HudValue
import tv.orbit.classic.arcade.Mascot
import tv.orbit.classic.arcade.gameKeys
import tv.orbit.classic.arcade.classicPanel
import tv.orbit.classic.arcade.PAIRS_CHALLENGE_SCORES
import tv.orbit.classic.arcade.ArcadePlay
import tv.orbit.classic.arcade.RunEnd
@@ -58,14 +59,17 @@ import kotlinx.coroutines.delay
internal const val PAIRS_COLUMNS = 5
internal const val PAIRS_ROWS = 4
internal data class PairsGame(val cards: List<Int>, val matched: Set<Int> = emptySet(), val open: List<Int> = emptyList(), val cursor: Int = 0, val moves: Int = 0) {
internal data class PairsGame(val cards: List<Int>, val matched: Set<Int> = emptySet(), val open: List<Int> = emptyList(), val cursor: Int = 0, val moves: Int = 0,
/** Cards across: Classic's 5; the Challenge's boards grow. */
val cols: Int = PAIRS_COLUMNS) {
val rows get() = cards.size / cols
val done get() = matched.size == cards.size
/** Two cards up that don't match: waiting to turn back. */
val waiting get() = open.size == 2
fun moveCursor(dx: Int, dy: Int): PairsGame {
val x = (cursor % PAIRS_COLUMNS + dx).coerceIn(0, PAIRS_COLUMNS - 1); val y = (cursor / PAIRS_COLUMNS + dy).coerceIn(0, PAIRS_ROWS - 1)
return copy(cursor = y * PAIRS_COLUMNS + x)
val x = (cursor % cols + dx).coerceIn(0, cols - 1); val y = (cursor / cols + dy).coerceIn(0, rows - 1)
return copy(cursor = y * cols + x)
}
/** Turns the card under the cursor. A second card counts a move; a match stays up. */
@@ -81,8 +85,8 @@ internal data class PairsGame(val cards: List<Int>, val matched: Set<Int> = empt
fun hide() = if (waiting) copy(open = emptyList()) else this
companion object {
fun deal(random: java.util.Random, kinds: Int = PAIRS_COLUMNS * PAIRS_ROWS / 2) =
PairsGame((0 until kinds).flatMap { listOf(it, it) }.toMutableList().also { java.util.Collections.shuffle(it, random) })
fun deal(random: java.util.Random, kinds: Int = PAIRS_COLUMNS * PAIRS_ROWS / 2, cols: Int = PAIRS_COLUMNS) =
PairsGame((0 until kinds).flatMap { listOf(it, it) }.toMutableList().also { java.util.Collections.shuffle(it, random) }, cols = cols)
}
}
@@ -92,6 +96,23 @@ internal data class PairsGame(val cards: List<Int>, val matched: Set<Int> = empt
*/
private val PairsChannels = listOf(101, 201, 301, 401, 601, 612, 0, 1, 2, 4)
/**
* Pairs' Challenge (Phase 2, 9 November 2026): boards in a row against one clock, each a little bigger, from 4 by 3 up to
* 6 by 5 (then 6 by 5 again). The clock starts at 90 seconds; a cleared board adds 30 and every match 2. Scored on boards
* cleared, then pairs matched ([pairsScore]).
*/
internal val PairsBoards = listOf(4 to 3, 4 to 4, 5 to 4, 6 to 4, 6 to 5)
internal fun pairsBoard(board: Int) = PairsBoards[minOf(board, PairsBoards.size - 1)]
internal const val PAIRS_CLOCK_START = 90
internal const val PAIRS_BOARD_BONUS = 30
internal const val PAIRS_MATCH_BONUS = 2
internal fun pairsScore(boards: Int, pairs: Int) = boards * 1_000 + pairs
internal fun pairsLine(score: Int) = "${score / 1_000} boards · ${score % 1_000} pairs"
/** The Challenge's faces: Classic's ten, then more of Orbit's family channels and Drift, enough for a 6 by 5. */
private val PairsChallengeFaces = PairsChannels + listOf(3, 102, 104, 203, 602, 605)
@Composable
private fun PairsCard(channel: Int, up: Boolean, matched: Boolean, selected: Boolean, modifier: Modifier) {
@@ -125,23 +146,59 @@ internal fun PairsScreen(play: ArcadePlay) {
var stage by remember { mutableStateOf(ClassicStage.Title) }
var paused by remember { mutableStateOf(false) }
var howTo by remember { mutableStateOf(false) }
var bestBefore by remember { mutableIntStateOf(play.best) }
// Classic, or the Challenge (Phase 2): chosen on the title, remembered, each with its own High Scores.
var challenge by remember { mutableStateOf(play.level["mode"] == "Challenge") }
val table = if (challenge) PAIRS_CHALLENGE_SCORES else gameDef.id
var bestBefore by remember { mutableIntStateOf(play.bestIn(table)) }
// The Challenge's board, faces, clock and tally.
var board by remember { mutableIntStateOf(0) }
var faces by remember { mutableStateOf(PairsChannels) }
var clock by remember { mutableIntStateOf(PAIRS_CLOCK_START) }
var boards by remember { mutableIntStateOf(0) }
var pairs by remember { mutableIntStateOf(0) }
val focus = remember { FocusRequester() }
val backdrop = ArcadeLook.forGame(gameDef.id)
LaunchedEffect(game.waiting, paused) { if (game.waiting && !paused) { delay(900); game = game.hide() } }
// A cleared board is saved for this profile; leaving part-way saves nothing (fewest moves only counts when it's done).
val run = rememberRun(play.onRunEnd) { RunEnd(game.moves, finished = game.done) }
fun deal() { bestBefore = latestPlay.best; game = PairsGame.deal(random); run.begin(); paused = false; stage = ClassicStage.Play }
fun leave() { run.end(RunEnd(game.moves, finished = game.done)); latestPlay.onLeave() }
LaunchedEffect(game.done) { if (game.done && stage == ClassicStage.Play) { run.end(RunEnd(game.moves, finished = true)); stage = ClassicStage.Result } }
// A Challenge counts however it ends: the boards and pairs it got.
val run = rememberRun({ end -> if (challenge) latestPlay.onRunEndIn(PAIRS_CHALLENGE_SCORES, end) else latestPlay.onRunEnd(end) }) {
if (challenge) RunEnd(pairsScore(boards, pairs), finished = false) else RunEnd(game.moves, finished = game.done)
}
fun dealBoard(b: Int) {
val (cols, rows) = pairsBoard(b)
faces = PairsChallengeFaces.shuffled(random).take(cols * rows / 2)
game = PairsGame.deal(random, cols * rows / 2, cols)
}
fun deal() {
bestBefore = latestPlay.bestIn(table)
if (challenge) { board = 0; boards = 0; pairs = 0; clock = PAIRS_CLOCK_START; dealBoard(0) } else { faces = PairsChannels; game = PairsGame.deal(random) }
run.begin(); paused = false; stage = ClassicStage.Play
}
fun leave() { run.end(if (challenge) RunEnd(pairsScore(boards, pairs), finished = false) else RunEnd(game.moves, finished = game.done)); latestPlay.onLeave() }
LaunchedEffect(game.done) {
if (!game.done || stage != ClassicStage.Play) return@LaunchedEffect
if (challenge) { boards++; clock += PAIRS_BOARD_BONUS; board++; dealBoard(board) }
else { run.end(RunEnd(game.moves, finished = true)); stage = ClassicStage.Result }
}
// The Challenge's clock: down a second at a time while playing; at nought, the run ends.
LaunchedEffect(stage, paused, challenge) {
while (challenge && stage == ClassicStage.Play && !paused) {
delay(1_000); clock--
if (clock <= 0) { clock = 0; run.end(RunEnd(pairsScore(boards, pairs), finished = true)); stage = ClassicStage.Result }
}
}
LaunchedEffect(stage, paused) { if (stage == ClassicStage.Play && !paused) runCatching { focus.requestFocus() } }
// A turned card can't be turned back, so Back always pauses here.
BackHandler(stage == ClassicStage.Play && !paused) { paused = true }
if (stage == ClassicStage.Title) {
ClassicTitle(gameDef, backdrop, ArcadeColours.Cyan, "Remember where we are!", listOf("Your best" to if (play.best > 0) "${play.best} moves" else "Not played yet"),
timed = false, difficulty = play.difficulty, refocus = !howTo, onPlay = ::deal, onHowTo = { howTo = true }, onBack = { latestPlay.onLeave() },
note = "Two cards are one move. The fewer moves, the better.")
val best = play.bestIn(table)
ClassicTitle(gameDef, backdrop, ArcadeColours.Cyan, if (challenge) "Quick! How many boards can you clear?" else "Remember where we are!",
listOf("Your best" to if (best <= 0) "Not played yet" else if (challenge) pairsLine(best) else "$best moves"),
timed = challenge, difficulty = play.difficulty, refocus = !howTo, onPlay = ::deal, onHowTo = { howTo = true }, onBack = { latestPlay.onLeave() },
mode = if (challenge) "Challenge" else "Classic", modes = 2, onMode = { challenge = !challenge; latestPlay.setLevel("mode", if (challenge) "Challenge" else "Classic") },
note = if (challenge) "Clear boards in a row before the clock runs out, each a little bigger, up to 6 by 5. Every match adds 2 seconds, every board 30."
else "Two cards are one move. The fewer moves, the better.")
if (howTo) HowToPlayCard(gameDef, firstRun = false, everyTime = null, onEveryTime = {}, onPlay = { howTo = false }, onBack = { howTo = false })
return
}
@@ -155,32 +212,50 @@ internal fun PairsScreen(play: ArcadePlay) {
ArcadeKey.Ok -> if (native.repeatCount > 0) game else game.flip()
else -> null
}
// Each match in the Challenge: a pair counted and two seconds back, at the moment it's made.
if (next != null && challenge && next.matched.size > game.matched.size) { pairs++; clock += PAIRS_MATCH_BONUS }
if (next != null) game = next
next != null
}
Box(Modifier.fillMaxSize()) {
ClassicPlayFrame(backdrop, keys, footer = listOf(Control("◀▶▲▼", "Move"), Control("OK", "Turn a card"), Control("Back", "Pause")), hud = {
HudLabel("MOVES"); HudValue("${game.moves}")
HudLabel("BEST"); HudValue(if (bestBefore == 0) "—" else "$bestBefore")
Spacer(Modifier.height(6.dp))
HudLabel("PAIRS"); HudValue("${game.matched.size / 2} of ${game.cards.size / 2}")
if (challenge) {
HudLabel("TIME LEFT"); HudValue("%d:%02d".format(clock / 60, clock % 60), if (clock <= 10) ArcadeColours.Red else ArcadeColours.Ink)
HudLabel("BOARDS"); HudValue("$boards")
Spacer(Modifier.height(6.dp))
HudLabel("PAIRS"); HudValue("$pairs")
Text("Board ${board + 1}: ${game.cols} by ${game.rows}", color = ArcadeColours.Muted, fontSize = 14.sp)
} else {
HudLabel("MOVES"); HudValue("${game.moves}")
HudLabel("BEST"); HudValue(if (bestBefore == 0) "—" else "$bestBefore")
Spacer(Modifier.height(6.dp))
HudLabel("PAIRS"); HudValue("${game.matched.size / 2} of ${game.cards.size / 2}")
}
}) {
// Classic's cards are 120 by 80; bigger boards' shrink to fit (6 by 5 at 100 by 70).
val cw = if (game.cols <= 5 && game.rows <= 4) 120 else 100
val ch = if (game.rows <= 4) 80 else 70
Column(Modifier.classicPanel().padding(14.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
for (row in 0 until PAIRS_ROWS) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
for (col in 0 until PAIRS_COLUMNS) {
val i = row * PAIRS_COLUMNS + col
PairsCard(PairsChannels[game.cards[i]], i in game.matched || i in game.open, i in game.matched, i == game.cursor && !game.done, Modifier.width(120.dp).height(80.dp))
for (row in 0 until game.rows) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
for (col in 0 until game.cols) {
val i = row * game.cols + col
PairsCard(faces[game.cards[i]], i in game.matched || i in game.open, i in game.matched, i == game.cursor && !game.done, Modifier.width(cw.dp).height(ch.dp))
}
}
}
}
if (stage == ClassicStage.Result) {
if (stage == ClassicStage.Result && challenge) {
val score = pairsScore(boards, pairs)
ClassicResult(gameDef, "PAIRS · CHALLENGE", "BOARDS", "$boards", newBest = score > bestBefore && score > 0,
previous = "$pairs pairs matched", detail = if (bestBefore > 0) "Previous best ${pairsLine(bestBefore)}" else "Your first Pairs Challenge", heading = "TIME!",
againLabel = "Go again", cheer = Mascot.Blaze, onAgain = ::deal, onBack = ::leave)
} else if (stage == ClassicStage.Result) {
val newBest = bestBefore == 0 || game.moves < bestBefore
ClassicResult(gameDef, "PAIRS · ALL ${game.cards.size / 2} PAIRED", "MOVES", "${game.moves}", newBest = newBest,
previous = if (bestBefore > 0) "Previous best $bestBefore moves" else "Your first Pairs score", detail = null, heading = "ALL PAIRED",
cheer = Mascot.Luna, onAgain = ::deal, onBack = ::leave)
}
if (paused && stage == ClassicStage.Play) ArcadePauseMenu(gameDef, "Pairs · ${game.moves} moves · ${game.matched.size / 2} of ${game.cards.size / 2} pairs",
onResume = { paused = false }, onRestart = { run.end(RunEnd(game.moves, finished = false)); deal() }, onExit = ::leave)
if (paused && stage == ClassicStage.Play) ArcadePauseMenu(gameDef, if (challenge) "Pairs Challenge · $boards boards · ${clock}s left" else "Pairs · ${game.moves} moves · ${game.matched.size / 2} of ${game.cards.size / 2} pairs",
onResume = { paused = false }, onRestart = { run.end(if (challenge) RunEnd(pairsScore(boards, pairs), finished = false) else RunEnd(game.moves, finished = false)); deal() }, onExit = ::leave)
}
}
@@ -106,6 +106,9 @@ internal const val BLOCKS_SPRINT_SCORES = "BlocksSprint"
internal const val PATIENCE_DAILY_SCORES = "PatienceDaily"
internal const val PATIENCE_DAILY3_SCORES = "PatienceDaily3"
/** Pairs' Challenge: boards cleared then pairs matched, in one number ([tv.orbit.classic.pairsScore]). */
internal const val PAIRS_CHALLENGE_SCORES = "PairsChallenge"
/**
* One of a game's High Scores tables: the game's own (its id, its score kind), or a mode's, kept under its own record id so
* the two never mix (Paul: "preserve existing scores and make new modes separate"). [format] writes a score for the page.
@@ -119,6 +122,7 @@ internal val modeTables: Map<String, List<ScoreTable>> = mapOf(
"Blocks" to listOf(ScoreTable(BLOCKS_SPRINT_SCORES, "40-Line Sprint", ScoreKind.FewestMoves) { tv.orbit.classic.sprintText(it) }),
"Patience" to listOf(ScoreTable(PATIENCE_DAILY_SCORES, "Daily", ScoreKind.FewestMoves) { tv.orbit.classic.dailyLine(it) },
ScoreTable(PATIENCE_DAILY3_SCORES, "Daily Draw 3", ScoreKind.FewestMoves) { tv.orbit.classic.dailyLine(it) }),
"Pairs" to listOf(ScoreTable(PAIRS_CHALLENGE_SCORES, "Challenge", ScoreKind.Points) { tv.orbit.classic.pairsLine(it) }),
"Sky" to listOf(ScoreTable(SKY_CHALLENGE_SCORES, "Challenge", ScoreKind.Points) { tv.orbit.classic.challengeLine(it) }),
)
@@ -112,4 +112,22 @@ class PairsPatienceTest {
assertTrue(dailyScore(90, 100) < dailyScore(90, 200))
assertEquals("90 moves · 5:21", dailyLine(dailyScore(90, 321)))
}
// ---------- Pairs' Challenge (Phase 2) ----------
@Test fun theChallengesBoardsGrowAndTheCursorStaysOnThem() {
assertEquals(listOf(4 to 3, 4 to 4, 5 to 4, 6 to 4, 6 to 5), PairsBoards)
assertEquals(6 to 5, pairsBoard(9))
val g = PairsGame.deal(java.util.Random(2), 15, 6)
assertEquals(30, g.cards.size); assertEquals(5, g.rows)
assertEquals(29, g.moveCursor(9, 9).cursor); assertEquals(0, g.moveCursor(-3, -3).cursor)
// Classic is still five by four.
val c = PairsGame.deal(java.util.Random(2))
assertEquals(20, c.cards.size); assertEquals(5, c.cols); assertEquals(19, c.moveCursor(9, 9).cursor)
}
@Test fun pairsChallengeScoresBoardsFirst() {
assertTrue(pairsScore(3, 1) > pairsScore(2, 40))
assertEquals("3 boards · 31 pairs", pairsLine(pairsScore(3, 31)))
}
}