Improve parse() and toString()
This commit is contained in:
@@ -1,42 +1,5 @@
|
|||||||
package ch.dissem.yaep.domain
|
package ch.dissem.yaep.domain
|
||||||
|
|
||||||
import ch.dissem.yaep.domain.Animal.ANT
|
|
||||||
import ch.dissem.yaep.domain.Animal.DOG
|
|
||||||
import ch.dissem.yaep.domain.Animal.GOAT
|
|
||||||
import ch.dissem.yaep.domain.Animal.SLOTH
|
|
||||||
import ch.dissem.yaep.domain.Animal.SNAIL
|
|
||||||
import ch.dissem.yaep.domain.Animal.ZEBRA
|
|
||||||
import ch.dissem.yaep.domain.Dessert.CAKE
|
|
||||||
import ch.dissem.yaep.domain.Dessert.CUPCAKE
|
|
||||||
import ch.dissem.yaep.domain.Dessert.CUSTARD
|
|
||||||
import ch.dissem.yaep.domain.Dessert.DOUGHNUT
|
|
||||||
import ch.dissem.yaep.domain.Dessert.ICE_CREAM
|
|
||||||
import ch.dissem.yaep.domain.Dessert.PIE
|
|
||||||
import ch.dissem.yaep.domain.Drink.BEER
|
|
||||||
import ch.dissem.yaep.domain.Drink.BEVERAGE
|
|
||||||
import ch.dissem.yaep.domain.Drink.COFFEE
|
|
||||||
import ch.dissem.yaep.domain.Drink.MILK
|
|
||||||
import ch.dissem.yaep.domain.Drink.TEA
|
|
||||||
import ch.dissem.yaep.domain.Drink.WINE
|
|
||||||
import ch.dissem.yaep.domain.Fruit.BANANA
|
|
||||||
import ch.dissem.yaep.domain.Fruit.GRAPES
|
|
||||||
import ch.dissem.yaep.domain.Fruit.MANGO
|
|
||||||
import ch.dissem.yaep.domain.Fruit.PEAR
|
|
||||||
import ch.dissem.yaep.domain.Fruit.PINEAPPLE
|
|
||||||
import ch.dissem.yaep.domain.Fruit.WATERMELON
|
|
||||||
import ch.dissem.yaep.domain.Nationality.CANADA
|
|
||||||
import ch.dissem.yaep.domain.Nationality.UNITED_KINGDOM
|
|
||||||
import ch.dissem.yaep.domain.Nationality.JAPAN
|
|
||||||
import ch.dissem.yaep.domain.Nationality.SPAIN
|
|
||||||
import ch.dissem.yaep.domain.Nationality.SWITZERLAND
|
|
||||||
import ch.dissem.yaep.domain.Nationality.UKRAINE
|
|
||||||
import ch.dissem.yaep.domain.Profession.ASTRONAUT
|
|
||||||
import ch.dissem.yaep.domain.Profession.HEALTH_WORKER
|
|
||||||
import ch.dissem.yaep.domain.Profession.ROCK_STAR
|
|
||||||
import ch.dissem.yaep.domain.Profession.SCIENTIST
|
|
||||||
import ch.dissem.yaep.domain.Profession.SOFTWARE_DEV
|
|
||||||
import ch.dissem.yaep.domain.Profession.TEACHER
|
|
||||||
|
|
||||||
class Game(
|
class Game(
|
||||||
val grid: Grid,
|
val grid: Grid,
|
||||||
val clues: Collection<Clue>
|
val clues: Collection<Clue>
|
||||||
@@ -75,23 +38,29 @@ class Game(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun parse(description: String): Game {
|
fun parse(description: String): Game {
|
||||||
// TODO get options from string
|
val optionsRegex = Regex("[A-Z]+(_[A-Z]+)*")
|
||||||
val rowOptions = listOf<List<Item<*>>>(
|
val options = optionsRegex.findAll(description)
|
||||||
listOf(
|
.map { it.value }
|
||||||
HEALTH_WORKER,
|
.distinct()
|
||||||
ROCK_STAR,
|
.map { Item(ItemClass.parse(it)) }
|
||||||
SCIENTIST,
|
.groupBy { it.itemType.companion }
|
||||||
SOFTWARE_DEV,
|
.toMutableMap()
|
||||||
ASTRONAUT,
|
val size = options.size
|
||||||
TEACHER
|
// Fill up missing items (one item per row might not appear in the clues)
|
||||||
).map { Item(it) },
|
// (This could be improved by looking which items are in the grid part, but this will
|
||||||
listOf(ANT, DOG, GOAT, SLOTH, SNAIL, ZEBRA).map { Item(it) },
|
// not always work.)
|
||||||
listOf(WATERMELON, MANGO, PEAR, GRAPES, PINEAPPLE, BANANA).map { Item(it) },
|
options
|
||||||
listOf(CUPCAKE, ICE_CREAM, DOUGHNUT, CAKE, PIE, CUSTARD).map { Item(it) },
|
.filter { it.value.size < size }
|
||||||
listOf(SWITZERLAND, UNITED_KINGDOM, JAPAN, UKRAINE, CANADA, SPAIN).map { Item(it) },
|
.forEach { entry ->
|
||||||
listOf(WINE, BEVERAGE, BEER, COFFEE, TEA, MILK).map { Item(it) }
|
val missing = size - entry.value.size
|
||||||
)
|
val used = entry.value.map { it.itemType }.toSet()
|
||||||
val optionMap = rowOptions.flatten().associateBy(
|
val fillers = entry.key.items
|
||||||
|
.filter { !used.contains(it) }
|
||||||
|
.take(missing)
|
||||||
|
.map { Item(it) }
|
||||||
|
options[entry.key] = entry.value + fillers
|
||||||
|
}
|
||||||
|
val optionMap = options.values.flatten().associateBy(
|
||||||
keySelector = { it.itemType },
|
keySelector = { it.itemType },
|
||||||
valueTransform = { it }
|
valueTransform = { it }
|
||||||
)
|
)
|
||||||
@@ -105,8 +74,8 @@ class Game(
|
|||||||
|
|
||||||
return Game(
|
return Game(
|
||||||
grid = Grid(
|
grid = Grid(
|
||||||
rows = rowOptions
|
rows = options.values
|
||||||
.map { options -> createRow<ItemClass<*>>(options) }
|
.map { createRow<ItemClass<*>>(it) }
|
||||||
.toList()
|
.toList()
|
||||||
),
|
),
|
||||||
clues = clues
|
clues = clues
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class Grid(
|
|||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return joinToString("\n") { row ->
|
return joinToString("\n") { row ->
|
||||||
row.joinToString("") { it.selection?.symbol ?: " " }
|
row.joinToString("") { it.selection?.symbol ?: "⬛" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user