mirror of
https://github.com/Vale54321/schafkopf-bot.git
synced 2025-12-16 11:49:33 +01:00
added cmd playing mode
This commit is contained in:
122
Backend/schafkopf-client/src/main/kotlin/CmdSchafkopfMessager.kt
Normal file
122
Backend/schafkopf-client/src/main/kotlin/CmdSchafkopfMessager.kt
Normal file
@@ -0,0 +1,122 @@
|
||||
import de.heiserer.CmdPlayer
|
||||
import de.heiserer.SchafkopfMessager
|
||||
import de.heiserer.cards.Card
|
||||
import de.heiserer.cards.CardColor
|
||||
import de.heiserer.cards.CardSymbol
|
||||
import de.heiserer.player.Player
|
||||
|
||||
class CmdSchafkopfMessager: SchafkopfMessager {
|
||||
override fun sendPlayerTurn(player: Player) {
|
||||
if(player !is CmdPlayer){
|
||||
clearConsole()
|
||||
}
|
||||
|
||||
println("${player.getName()} ist am Zug")
|
||||
Thread.sleep(1000)
|
||||
}
|
||||
|
||||
override fun sendPlayerWonTrick(player: Player) {
|
||||
println("${player.getName()} hat den Stich gewonnen.")
|
||||
println()
|
||||
}
|
||||
|
||||
override fun sendPlayerWonGame(player: Player) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun sendCardPlayed(player: Player, card: Card) {
|
||||
println("${player.getName()} hat ${card.displayName} gespielt.")
|
||||
}
|
||||
|
||||
override fun sendTableCards(cards: List<Card>) {
|
||||
println("Tischkarten:")
|
||||
CmdCard.printCards(cards)
|
||||
|
||||
Thread.sleep(3000)
|
||||
}
|
||||
|
||||
|
||||
|
||||
class CmdCard(card: Card){
|
||||
private val symbolMap = mapOf(
|
||||
CardSymbol.SIEBEN to "7",
|
||||
CardSymbol.ACHT to "8",
|
||||
CardSymbol.NEUN to "9",
|
||||
CardSymbol.ZEHN to "10",
|
||||
CardSymbol.OBER to "O",
|
||||
CardSymbol.UNTER to "U",
|
||||
CardSymbol.KOENIG to "K",
|
||||
CardSymbol.ASS to "A"
|
||||
)
|
||||
|
||||
private val colorMap = mapOf(
|
||||
CardColor.HERZ to "♥",
|
||||
CardColor.SCHELL to "♦",
|
||||
CardColor.EICHEL to "♣",
|
||||
CardColor.BLATT to "♠"
|
||||
)
|
||||
|
||||
private val symbol = symbolMap[card.symbol]?: "?"
|
||||
private val color = colorMap[card.color]?: "?"
|
||||
private val colorName = card.color.displayName
|
||||
private val symbolName = card.symbol.displayName
|
||||
|
||||
private val cardWidth = 13
|
||||
|
||||
private val symbolLine = "│ ${symbol.padEnd(cardWidth-5)}$symbol │"
|
||||
private val colorLine = "│ ${color.padEnd(cardWidth-5)}$color │"
|
||||
|
||||
private val cardLines = listOf(
|
||||
"┌───────────┐",
|
||||
symbolLine,
|
||||
colorLine,
|
||||
"│${colorName.padCenter(cardWidth-2)}│",
|
||||
"│${symbolName.padCenter(cardWidth-2)}│",
|
||||
colorLine,
|
||||
symbolLine,
|
||||
"└───────────┘"
|
||||
|
||||
)
|
||||
|
||||
fun print(){
|
||||
cardLines.forEach {
|
||||
println(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun getCardLines(): List<String> {
|
||||
return cardLines
|
||||
}
|
||||
|
||||
private fun String.padCenter(totalWidth: Int, padChar: Char = ' '): String {
|
||||
if (this.length >= totalWidth) return this
|
||||
val padding = totalWidth - this.length
|
||||
val padStart = this.length + padding / 2
|
||||
return this.padStart(padStart, padChar).padEnd(totalWidth, padChar)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun printCards(cards: List<Card>) {
|
||||
val lastCardLines = CmdCard(cards.last()).getCardLines()
|
||||
|
||||
val cardLines = cards.map { CmdCard(it).getCardLines() }
|
||||
|
||||
// Loop through the indices of the lines that are present in all cards
|
||||
for (i in lastCardLines.indices) {
|
||||
// For other lines, print up to the first 5 characters
|
||||
var line = cardLines.joinToString(" ") { it[i].take(5) }
|
||||
line += lastCardLines[i].drop(5)
|
||||
println(line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun clearConsole() {
|
||||
for (i in 1..100) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package de.heiserer
|
||||
|
||||
import de.heiserer.cards.*
|
||||
import de.heiserer.player.Player
|
||||
import java.util.*
|
||||
|
||||
|
||||
class CmdPlayer(name: String) : Player(name) {
|
||||
private val scanner = Scanner(System.`in`)
|
||||
|
||||
override fun playCard(tableCards: UnsortedCardList, gameType: GameType): Card {
|
||||
val cardsCopy = cards.getCopyOfCards()
|
||||
|
||||
println("Available cards:")
|
||||
CmdSchafkopfMessager.CmdCard.printCards(cardsCopy)
|
||||
|
||||
// Print cards with their respective index
|
||||
cardsCopy.forEachIndexed { index, card ->
|
||||
println("(${index + 1}) ${card.name}") // Prints card with index starting from 1
|
||||
}
|
||||
|
||||
// Prompt the user to enter a card number
|
||||
println("Please enter the number of the card you want to play:")
|
||||
|
||||
val userInput = scanner.nextLine()
|
||||
val cardToPlay = getCardByUserInput(userInput, tableCards, gameType)
|
||||
|
||||
return cards.remove(cardToPlay)
|
||||
}
|
||||
|
||||
private fun getCardByUserInput(
|
||||
userInput: String,
|
||||
tableCards: UnsortedCardList,
|
||||
gameType: GameType
|
||||
): Card {
|
||||
val cardsCopy = cards.getCopyOfCards()
|
||||
return try {
|
||||
// Convert user input to an integer and adjust for zero-based index
|
||||
val cardIndex = userInput.toInt() - 1
|
||||
|
||||
// Ensure the index is within bounds
|
||||
if (cardIndex in cardsCopy.indices) {
|
||||
cardsCopy[cardIndex] // Return the selected card
|
||||
if(validateCard(cardsCopy[cardIndex], tableCards, gameType)){
|
||||
cardsCopy[cardIndex]
|
||||
} else {
|
||||
println("Invalid card. Please try again.")
|
||||
playCard(tableCards, gameType) // Retry if the card is invalid
|
||||
}
|
||||
} else {
|
||||
println("Invalid card number. Please try again.")
|
||||
playCard(tableCards, gameType) // Retry if the input was invalid
|
||||
}
|
||||
} catch (e: NumberFormatException) {
|
||||
println("Invalid input. Please enter a number.")
|
||||
playCard(tableCards, gameType) // Retry on invalid input
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package de.heiserer
|
||||
|
||||
import CmdSchafkopfMessager
|
||||
import de.heiserer.player.NPCPlayer
|
||||
import de.heiserer.plugins.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.netty.*
|
||||
|
||||
fun main() {
|
||||
val test = SchafkopfGameController()
|
||||
val test = SchafkopfGameController(listOf(NPCPlayer("NPC 1"), NPCPlayer("NPC 2"), CmdPlayer("Dev"), NPCPlayer("NPC 4")), CmdSchafkopfMessager())
|
||||
|
||||
test.playRound()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user