Skip to content

Commit

Permalink
Merge pull request #140 from noxone/noxone/138/fix-computation
Browse files Browse the repository at this point in the history
fix computation
  • Loading branch information
noxone authored Jul 15, 2024
2 parents 70c3e21 + d85a035 commit 5ee4e4c
Show file tree
Hide file tree
Showing 9 changed files with 548 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/main/kotlin/org/olafneumann/mahjong/points/game/Combination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ data class Combination(
}
}

override fun equals(other: Any?): Boolean {
if (other == null || other !is Combination) {
return false
}

return type == other.type
&& tile == other.tile
&& visibility == other.visibility
}

companion object {
private val idGenerator = IdGenerator()
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/org/olafneumann/mahjong/points/game/Tile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ enum class Tile(
private val isFlower: Boolean get() = this in flowers
private val isSeason: Boolean get() = this in seasons
val isTrump: Boolean get() = isWind || isDragon
val isBonusTile: Boolean get() = isFlower ||isSeason
val isBonusTile: Boolean get() = isFlower || isSeason
val numberOfTilesInSet: Int get() = if (isBonusTile) 1 else MAX_NUMBER_OF_TILES_PER_TYPE

companion object {
Expand All @@ -79,6 +79,13 @@ enum class Tile(
val bamboos: Collection<Tile> = allOfColor(Color.Bamboo)
val characters: Collection<Tile> = allOfColor(Color.Character)
val circles: Collection<Tile> = allOfColor(Color.Circle)
val windBonusTiles: Map<Wind, Collection<Tile>> =
mapOf(
East to setOf(Flower1, Season1),
South to setOf(Flower2, Season2),
West to setOf(Flower3, Season3),
North to setOf(Flower4, Season4)
)

private fun allOfColor(color: Color) = values().filter { it.color == color }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Language(

StringKeys.ERR_NO_TILES_LEFT_FOR_PAIR to "Not enough tiles left for a pair.",
StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW to "Not enough tiles left for a Chow.",
StringKeys.ERR_NO_TILES_LEFT_FOR_PUNG to "Not enough tiles left for a Pung.",
StringKeys.ERR_TILES_DOES_NOT_FIT_TO_CHOW_OR_PONG to "Tile cannot be used for Pong or Chow in the selected set.",
StringKeys.ERR_TILE_DOES_NOT_FIT_TO_SET to "Tile does not fit to selected set.",
StringKeys.ERR_NO_TILE_LEFT_FOR_KANG to "No tile left for Kang.",
Expand Down Expand Up @@ -234,7 +235,8 @@ class Language(
StringKeys.WINNING_TILE_IS_LAST_TILE_FROM_WALL to "Schlussziegel ist letzter Ziegel der Mauer",

StringKeys.ERR_NO_TILES_LEFT_FOR_PAIR to "Nicht genug Ziegel für Paar übrig.",
StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW to "Nicht genug Ziegel für Pong übrig.",
StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW to "Nicht genug Ziegel für Chi übrig.",
StringKeys.ERR_NO_TILES_LEFT_FOR_PUNG to "Nicht genug Ziegel für Pong übrig.",
StringKeys.ERR_TILES_DOES_NOT_FIT_TO_CHOW_OR_PONG to "Ziegel kann nicht für Chi oder Pong in der markierten Figur genutzt werden.",
StringKeys.ERR_TILE_DOES_NOT_FIT_TO_SET to "Ziegel passt nicht zur markierten Figur.",
StringKeys.ERR_NO_TILE_LEFT_FOR_KANG to "Nicht genug Ziegel für Kang.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ object StringKeys {

const val ERR_NO_TILES_LEFT_FOR_PAIR = "Not enough tiles left for a pair."
const val ERR_NO_TILES_LEFT_FOR_CHOW = "Not enough tiles left for a Chow."
const val ERR_NO_TILES_LEFT_FOR_PUNG = "Not enough tiles left for a Pung."
const val ERR_TILES_DOES_NOT_FIT_TO_CHOW_OR_PONG = "Tile cannot be used for Pong or Chow in the selected set."
const val ERR_TILE_DOES_NOT_FIT_TO_SET = "Tile does not fit to selected set."
const val ERR_NO_TILE_LEFT_FOR_KANG = "No tile left for Kang."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ data class CalculatorModel(
if (combination == null) {
if (tile.isTrump) {
if (!canConsume(tile, tile, tile)) {
return withError(tile, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
return withError(tile, StringKeys.ERR_NO_TILES_LEFT_FOR_PUNG)
}
return evolve(
hand = hand.setCombination(selectedFigure, Combination(Pung, tile, Open)),
Expand All @@ -118,7 +118,7 @@ data class CalculatorModel(
Unfinished0 -> {
if (tile == combination.tile) {
if (!canConsume(tile, tile)) {
return withError(tile, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
return withError(tile, StringKeys.ERR_NO_TILES_LEFT_FOR_PUNG)
}
return evolve(
hand = combination.replace(
Expand All @@ -127,22 +127,35 @@ data class CalculatorModel(
),
)
}
if (!canConsume(tile)) {
return withError(tile, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}

// nächste Tile am Anfang
if (tile == combination.tile.next && combination.tile.number == FIRST_OF_COLOR) {
if (!canConsume(tile.next!!)) {
return withError(tile.next!!, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}
return evolve(
hand = combination.replace(hand = hand, type = Chow),
selectedFigure = selectedFigure.next,
)
}
// vorige Tile am Ende
if (tile == combination.tile.previous && combination.tile.number == LAST_OF_COLOR) {
if (!canConsume(tile.previous!!)) {
return withError(tile.previous!!, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}
return evolve(
hand = combination.replace(hand = hand, tile = tile.previous!!, type = Chow),
selectedFigure = selectedFigure.next,
)
}
// übernächste Tile
if (tile == combination.tile.next?.next) {
if (!canConsume(tile.previous!!)) {
return withError(tile.previous!!, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}
return evolve(
hand = combination.replace(hand = hand, type = Chow),
selectedFigure = selectedFigure.next,
Expand All @@ -151,6 +164,9 @@ data class CalculatorModel(
// nächste Tile
if (tile == combination.tile.next) {
if (tile.number == LAST_OF_COLOR) {
if (!canConsume(tile.previous!!)) {
return withError(tile.previous!!, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}
return evolve(
hand = combination.replace(hand = hand, type = Chow, tile = combination.tile.previous!!),
selectedFigure = selectedFigure.next
Expand All @@ -163,6 +179,9 @@ data class CalculatorModel(
// vorige Tile
if (tile == combination.tile.previous) {
if (tile.number == FIRST_OF_COLOR) {
if (!canConsume(tile.next!!.next!!)) {
return withError(tile.next!!.next!!, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}
return evolve(
hand = combination.replace(hand = hand, type = Chow, tile = tile),
selectedFigure = selectedFigure.next
Expand All @@ -174,6 +193,9 @@ data class CalculatorModel(
}
// vor-vorige Tile
if (tile == combination.tile.previous?.previous) {
if (!canConsume(tile.next!!)) {
return withError(tile.next!!, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}
return evolve(
hand = combination.replace(hand = hand, type = Chow, tile = tile),
selectedFigure = selectedFigure.next,
Expand All @@ -183,6 +205,9 @@ data class CalculatorModel(
}

UnfinishedPlus1 -> {
if (!canConsume(tile)) {
return withError(tile, StringKeys.ERR_NO_TILES_LEFT_FOR_CHOW)
}
if (tile == combination.tile.next?.next) {
return evolve(
hand = combination.replace(hand = hand, type = Chow),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class ClassicRulesResultComputer : ResultComputer {
)

private fun checkMahjongPoints(hand: Hand, gameModifiers: GameModifiers): List<Line> {
console.log(gameModifiers)
return listOf(
// Mahjong
Line(description = StringKeys.MAHJONG, points = 20),
Expand All @@ -123,7 +122,7 @@ class ClassicRulesResultComputer : ResultComputer {
listOf(
listOf(
// Beide Bonusziegel des Platzwindes
(hand.pair?.getTiles()?.all { it.wind == seatWind } ?: false)
hand.bonusTiles.containsAll(Tile.windBonusTiles[seatWind]!!)
.map { Line(description = StringKeys.PAIR_WIND_SEAT, doublings = 1) },
// alle Blumenziegel
hand.bonusTiles.containsAll(Tile.flowers)
Expand Down
Loading

0 comments on commit 5ee4e4c

Please sign in to comment.