added cmd playing mode

This commit is contained in:
2024-07-22 00:48:10 +02:00
parent 4a167bf3b4
commit 05300c1153
15 changed files with 434 additions and 137 deletions

View File

@@ -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
}
}
}

View File

@@ -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()