Skip to content

Commit

Permalink
Merge pull request #87 from Marvin-Brouwer/86-large-staight-is-also-i…
Browse files Browse the repository at this point in the history
…ncorrect

86 large staight is also incorrect
  • Loading branch information
Marvin-Brouwer authored Oct 27, 2023
2 parents 1797ee8 + 52a2f97 commit 6936b65
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
with:
path: './src/app'
package-manager: pnpm@latest
node-version: 19
node-version: 20

deploy:
needs: build
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"undici@<5.19.1": ">=5.19.1",
"undici@>=2.0.0 <5.19.1": ">=5.19.1"
}
},
"volta": {
"node": "20.9.0"
}
}
10 changes: 6 additions & 4 deletions src/app/src/game/score/scoreFieldValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ function isLargeStraight(score: ValidScore): boolean {
const largeStraightSize = 5
const distinct = new Set(score)

// Because a large straight needs all dice to be different, a distinct check is good enough
return distinct.size >= largeStraightSize
// In the case of a large straight, we can just check all numbers to be unique and then if 1 and 6 are exclusively included
const isStraight = distinct.size >= largeStraightSize
const isConsecutive = !(distinct.has(1) && distinct.has(6))

return isStraight && isConsecutive
}

function isSmallStraight(score: ValidScore): boolean {
Expand All @@ -68,7 +70,7 @@ function isSmallStraight(score: ValidScore): boolean {
// First check if there's at least 4 distinct numbers
// If that's the case, check if the numbers are consecutive, which is expensive

const isStraight = () => distinct.size >= smallStraightSize
const isStraight = distinct.size >= smallStraightSize
const countConsecutive = () => Array.from(distinct)
.sort()
.reduce((previousCount, currentValue, index, all) => {
Expand All @@ -86,7 +88,7 @@ function isSmallStraight(score: ValidScore): boolean {
}, [0])
const hasFourConsecutive = () => countConsecutive().some(count => count >= smallStraightSize)

const result = isStraight() && hasFourConsecutive()
const result = isStraight && hasFourConsecutive()

return result

Expand Down
19 changes: 18 additions & 1 deletion src/app/tests/gameState/gameScore.largeStraight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@ import { expect, test, describe } from 'vitest'

import { isScoreApplicableToField } from '../../src/game/score/scoreFieldValidator'
import { generateRandomScores } from './gameScore.mjs'
import { score } from '../../src/game/score/score'

const [pattern, allowedScores, disallowedScores] = generateRandomScores(
'abcde'
'12345', '23456'
)

describe('scoreValidator', () => {

describe('test-cases', () => {

test.concurrent('86-large-staight-is-also-incorrect [13456]', () => {

// Arrange
const testScore = score([1, 3, 4, 5, 6])
const sut = () => isScoreApplicableToField(testScore, 'largeStraight')

// Act
const result = sut()

// Assert
expect(result).toBeFalsy()
})
})

describe(`largeStraight ${pattern}`, () => {

for(const score of allowedScores) {
Expand Down

0 comments on commit 6936b65

Please sign in to comment.