-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Efeturi Money <[email protected]>
- Loading branch information
Showing
28 changed files
with
179 additions
and
268 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
alias(libs.plugins.kotlin.jvm) | ||
} | ||
|
||
dependencies { | ||
implementation(libs.kotlin.stdlib) | ||
implementation(libs.kotlinx.immutable) | ||
implementation(libs.kotlinx.coroutines.core) | ||
implementation(libs.jetbrains.compose.runtime) | ||
implementation(libs.jetbrains.compose.ui.util) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dev.efemoney.lexiko.engine.api | ||
|
||
import androidx.compose.runtime.Stable | ||
import androidx.compose.ui.util.fastForEach | ||
|
||
@Stable | ||
interface BagOfTiles { | ||
val remainingTilesCount: Int | ||
val isEmpty get() = remainingTilesCount == 0 | ||
|
||
fun pickRandomTile(): Tile? | ||
fun pickRandomTiles(max: Int): List<Tile> = | ||
if (isEmpty) emptyList() else buildList { for (i in 0..<max) add(pickRandomTile() ?: break) } | ||
|
||
fun returnTile(tile: Tile) | ||
fun returnTiles(tiles: List<Tile>) = tiles.fastForEach(::returnTile) | ||
} | ||
|
||
interface InitialBagOfTiles : BagOfTiles { | ||
fun pickTile(char: TileChar): Tile? | ||
fun pickTiles(chars: List<TileChar>): List<Tile> = | ||
chars.mapNotNull(::pickTile).also { require(it.size == chars.size) { "Some tiles are not available" } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dev.efemoney.lexiko.engine.api | ||
|
||
internal inline fun Int.requireIn(range: IntRange, lazyMessage: () -> Any): Int { | ||
require(this in range, lazyMessage) | ||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.efemoney.lexiko.engine.api | ||
|
||
@JvmInline | ||
value class Word(private val tiles: Sequence<Tile>) | ||
|
||
@JvmInline | ||
value class Letter(private val tile: Tile) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dev.efemoney.lexiko.engine.api | ||
|
||
import dev.efemoney.lexiko.engine.impl.DefaultBagOfTiles | ||
|
||
fun BagOfTiles(): BagOfTiles = DefaultBagOfTiles() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dev.efemoney.lexiko.engine.api | ||
|
||
import dev.efemoney.lexiko.engine.impl.BoardImpl | ||
|
||
fun Board(tilePlacement: TilePlacement = TilePlacement.None): Board = | ||
BoardImpl(initialTilePlacement = tilePlacement) | ||
|
||
context(InitialBagOfTiles) | ||
fun Board(placeTiles: context(InitialBagOfTiles) MutableBoard.() -> Unit): Board { | ||
return BoardImpl().also { placeTiles(self, it) } | ||
} | ||
|
||
context(MutableBoard) | ||
fun Tile.placeAt(position: TilePosition) = internal.place(this, at = position) | ||
|
||
@JvmInline | ||
value class TilePlacement private constructor(private val placement: Map<TilePosition, Tile>) { | ||
|
||
constructor(build: Builder.() -> Unit) : this(buildMap { Builder(this).apply(build) }) | ||
|
||
operator fun get(position: TilePosition) = placement[position] | ||
|
||
@JvmInline | ||
value class Builder internal constructor(private val map: MutableMap<TilePosition, Tile>) { | ||
|
||
infix fun Tile.at(position: TilePosition) = map.put(position, this) | ||
} | ||
|
||
companion object { | ||
val None = TilePlacement(emptyMap()) | ||
} | ||
} | ||
|
||
// region Implementation | ||
|
||
private val InitialBagOfTiles.self get() = this | ||
|
||
internal interface BoardInternal : MutableBoard { | ||
fun place(tile: Tile, at: TilePosition) | ||
fun place(tiles: List<Tile>, startAt: TilePosition, direction: Direction) | ||
} | ||
|
||
internal val Board.internal get() = this as BoardInternal | ||
|
||
internal fun interface OnConflict { | ||
operator fun invoke(slot: TileSlot, old: Tile, new: Tile) | ||
|
||
companion object { | ||
val DoNothing = OnConflict { _, _, _ -> } | ||
} | ||
} | ||
|
||
// endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.