From 73a58f5893e12da25acda2466576a579663271b6 Mon Sep 17 00:00:00 2001 From: "Joey@macstudio" <4296411@qq.com> Date: Fri, 17 Jul 2026 00:03:57 +0800 Subject: [PATCH] fix: score duplicate ranks in Four Fingers straights --- balatro-sim.js | 3 ++- breakdown.js | 3 ++- tests/four-fingers.test.js | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/four-fingers.test.js diff --git a/balatro-sim.js b/balatro-sim.js index ab5cc9b..a093925 100644 --- a/balatro-sim.js +++ b/balatro-sim.js @@ -1097,7 +1097,8 @@ class Hand { } if(lines[lines.length - 1].length >= (this.FourFingers ? 4 : 5)) { - straight = lines[lines.length - 1]; + const straightRanks = new Set(lines[lines.length - 1].map(card => card[RANK])); + straight = sortedCards.filter(card => straightRanks.has(card[RANK])); } } diff --git a/breakdown.js b/breakdown.js index 9b64f7b..e50ceaf 100644 --- a/breakdown.js +++ b/breakdown.js @@ -1514,7 +1514,8 @@ class Hand { } if(lines[lines.length - 1].length >= (this.FourFingers ? 4 : 5)) { - straight = lines[lines.length - 1]; + const straightRanks = new Set(lines[lines.length - 1].map(card => card[RANK])); + straight = sortedCards.filter(card => straightRanks.has(card[RANK])); } } diff --git a/tests/four-fingers.test.js b/tests/four-fingers.test.js new file mode 100644 index 0000000..9cc7ef8 --- /dev/null +++ b/tests/four-fingers.test.js @@ -0,0 +1,39 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const path = require("node:path"); +const test = require("node:test"); +const vm = require("node:vm"); + +function loadHand(sourceFile) { + const context = vm.createContext({}); + const source = fs.readFileSync(path.join(__dirname, "..", sourceFile), "utf8"); + vm.runInContext(source, context, { filename: sourceFile }); + return vm.runInContext("Hand", context); +} + +function card(rank, suit) { + return [rank, suit, 0, 0, 0, 0, false, 0, 0]; +} + +for(const sourceFile of ["balatro-sim.js", "breakdown.js"]) { + test(`${sourceFile} scores duplicate ranks in a Four Fingers straight`, () => { + const Hand = loadHand(sourceFile); + const cards = [ + card(0, 0), + card(1, 1), + card(1, 2), + card(2, 3), + card(3, 0) + ]; + const hand = new Hand({ cards }); + hand.FourFingers = true; + + const [handType, involvedCards] = hand.getTypeOfHand(); + const involvedRanks = Array.from(involvedCards, involvedCard => involvedCard[0]).sort((a, b) => a - b); + + assert.equal(handType, 7); + assert.deepEqual(involvedRanks, [0, 1, 1, 2, 3]); + }); +}