Files
KnockOutWhist-Web/knockoutwhistweb/app/util/GameUtil.scala
Janis 6e17328846 feat: GameState to Title Mapping BAC-1 (#92)
Reviewed-on: #92
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
2025-12-01 19:13:32 +01:00

44 lines
1.2 KiB
Scala

package util
import de.knockoutwhist.control.GameState
import de.knockoutwhist.control.GameState.{FinishedMatch, InGame, Lobby, MainMenu, SelectTrump, TieBreak}
import scala.util.Random
object GameUtil {
private val CharPool: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
private val CodeLength: Int = 6
private val MaxRepetition: Int = 2
private val random = new Random()
def generateCode(): String = {
val freq = Array.fill(CharPool.length)(0)
val code = new StringBuilder(CodeLength)
for (_ <- 0 until CodeLength) {
var index = random.nextInt(CharPool.length)
// Pick a new character if it's already used twice
while (freq(index) >= MaxRepetition) {
index = random.nextInt(CharPool.length)
}
freq(index) += 1
code.append(CharPool.charAt(index))
}
code.toString()
}
def stateToTitle(gameState: GameState): String = {
gameState match {
case Lobby => "Lobby"
case MainMenu => "Main Menu"
case InGame => "In Game"
case SelectTrump => "Select Trump"
case TieBreak => "Tie Break"
case FinishedMatch => "Finished Match"
}
}
}