vendredi 1 mai 2020

Don't know how to finish Poker game on Koltin

I'm trying to build Poker, and not sure How I can implement All poker rounds (PreflopRound(), FlopRound(), TradeRound(), TurnRound(), TradeRound(), RiverRound(), TradeRound()). Also will be glad to see your comments. My current goal: is to build all rounds when players will have to make bets while each of them don't pass or skip raise

fun main() {

    println("Enter First player Name")
    val firstPlayerName: String = readLine()!!
    println("Enter First player Balance")
    val firstPlayerMoney: Long = readLine()!!.toLong()
    println("Enter Second player Name")
    val secondPlayerName: String = readLine()!!
    println("Enter Second player Balance")
    val secondPlayerMoney: Long = readLine()!!.toLong()

    val playerList = mutableListOf<Player>(
            Player(firstPlayerName, firstPlayerMoney),
            Player(secondPlayerName, secondPlayerMoney)
    )

    Game(players = playerList).startGame()
}

data class Game(val players: List<Player>,
                val cardDistributions: List<CardDistribution> = mutableListOf()) {
    var dealerPosition = 0

    fun startGame() {
        CardDistribution(players, calculateCurrentDealer()).startDistribution()
    }

    private fun calculateCurrentDealer(): Player {
        var counter = dealerPosition
        dealerPosition++
        if (dealerPosition > players.size) {
            dealerPosition = 0
            counter = dealerPosition
        }
        return players[counter]
    }
}
data class CardDistribution(val players: List<Player>,
                            val dealer: Player,
                            val hands: MutableList<Hand> = mutableListOf(),
                            val deck: Deck = Deck()) {
    fun startDistribution() {
        //Future Add
//        , TradeRound(), TurnRound(), TradeRound(), RiverRound(), TradeRound()
        val rounds = listOf<Round>(PreflopRound(), FlopRound())
        for(round in rounds) {
            val shouldRunNextRound = round.startRound(this)
            if (!shouldRunNextRound) {
                break
            }
        }
    }

    class PreflopRound: Round {
        override fun startRound(cardDistribution: CardDistribution): Boolean {
            val handList = cardDistribution.deck.distributeCards(cardDistribution.players)
            cardDistribution.hands.addAll(handList)

            for (player in cardDistribution.players) {
                player.makeBet()
            }

            return true
        }

    }
}


data class Player(val name: String, val money: Long) {
    fun makeBet(): Long {
        val bet = readLine()!!.toLong()
        return bet
    }
}

class Deck {
    val cards = mutableListOf<Card>()

    init {
        CardSuit.values().forEach {
            val suit = it
            CardRank.values().forEach {
                val card = Card(it, suit)
                cards.add(card)
            }
        }
    }

    fun shuffleDeck() {
        cards.shuffle()
    }

    fun distributeCards(players: List<Player>): List<Hand> {
        return players.map {
            val hand = Hand(it, getCard(), getCard())
            hand
        }
    }

    fun getCard(): Card {
        val card = cards.random()
        cards.remove(card)
        return card
    }
}

class FlopRound : Round {
    override fun startRound(cardDistribution: CardDistribution): Boolean {
        return true
    }
}
data class Hand(val player: Player, val firstCard: Card, val secondCard: Card)
data class Bet(val player: Player, val amount: Long)
data class Card(val rank: CardRank, val suit: CardSuit)
interface Round {
    fun startRound(cardDistribution: CardDistribution): Boolean
}

enum class CardRank {
    ACE,
    KING,
    QUEEN,
    JACK,
    CARD_10,
    CARD_9,
    CARD_8,
    CARD_7,
    CARD_6,
    CARD_5,
    CARD_4,
    CARD_3,
    CARD_2
}

enum class CardSuit {
    HEART,
    DIAMOND,
    SPADE,
    CLUB
}

Aucun commentaire:

Enregistrer un commentaire