package codigos import kotlin.random.Random data class CartaJugada(val valor: Char, val palo: Char) fun main() { println(" A 2 3 4 5 6 7 8 9 T J Q K") jugadaMesa() val jugada1 = decodificar(1) val jugada2 = decodificar(2) /*********Analiza Jugada************/ val jugada1Ordenada = jugada1.sortedWith(compareBy {it.valor}) val jugada2Ordenada = jugada2.sortedWith(compareBy {it.valor}) val resultado1 = analizajugada1Ordenada(jugada1Ordenada) val resultado2 = analizajugada1Ordenada(jugada2Ordenada) println("\nJugada 1 tiene -> $resultado1") jugada1Ordenada.forEach{data -> print("${data.valor}${data.palo} ")} println("\nJugada 2 tiene -> $resultado2") jugada2Ordenada.forEach{data -> print("${data.valor}${data.palo} ")} println("\nLa mano ganadora es: "+ jugadaGanadora(jugada1Ordenada,resultado1, jugada2Ordenada,resultado2) ) } fun analizajugada1Ordenada(jugada1Ordenada : List): String{ val palosJugados = arrayOf(0,0,0,0) val valoresJugados = arrayOf(0,0,0,0,0,0,0,0,0,0,0,0,0) var result = "NADA" for (palos in jugada1Ordenada){ when(palos.palo){ 'S' -> palosJugados[0]++ 'C' -> palosJugados[1]++ 'D' -> palosJugados[2]++ 'H' -> palosJugados[3]++ } } for (valores in jugada1Ordenada){ when(valores.valor){ 'A' -> valoresJugados[0]++ '2' -> valoresJugados[1]++ '3' -> valoresJugados[2]++ '4' -> valoresJugados[3]++ '5' -> valoresJugados[4]++ '6' -> valoresJugados[5]++ '7' -> valoresJugados[6]++ '8' -> valoresJugados[7]++ '9' -> valoresJugados[8]++ 'T' -> valoresJugados[9]++ 'J' -> valoresJugados[10]++ 'Q' -> valoresJugados[11]++ 'K' -> valoresJugados[12]++ } } var noEsColor =true for (palo in palosJugados){ if (palo == 5){ result = "COLOR" noEsColor = false for (position in valoresJugados.indices){ result = if (position < 9){ if (1 == valoresJugados[position] && 1 == valoresJugados[position+1] && 1 == valoresJugados[position+2] && 1 == valoresJugados[position+3] && 1 == valoresJugados[position+4]) "ESCALERA COLOR" else continue }else if (position == 9){ if (1 == valoresJugados[position] && 1 == valoresJugados[position+1] && 1 == valoresJugados[position+2] && 1 == valoresJugados[position+3] && 1 == valoresJugados[0]) "ESCALERA REAL" else break }else break } } } // jugadas posibles para 5 palos iguales if (noEsColor) { var tieneTrio = false var cantDobles = 0 for (position in valoresJugados.indices) { if (valoresJugados[position] == 4) { result = "POKER" break } else if (valoresJugados[position] == 1 && position <= 9) { if (position < 9) { if (1 == valoresJugados[position] && 1 == valoresJugados[position + 1] && 1 == valoresJugados[position + 2] && 1 == valoresJugados[position + 3] && 1 == valoresJugados[position + 4]) { result = "ESCALERA" break } else { continue } } else if (position == 9) { if (1 == valoresJugados[position] && 1 == valoresJugados[position + 1] && 1 == valoresJugados[position + 2] && 1 == valoresJugados[position + 3] && 1 == valoresJugados[0]) { result = "ESCALERA FIGURAS" break } else continue } else continue } // Controla para ESCALERA else { if (valoresJugados[position] == 3) tieneTrio = true if (valoresJugados[position] == 2) cantDobles++ result = if ((cantDobles == 1) && tieneTrio) "FULL HOUSE" else if ((cantDobles == 0) && tieneTrio) "TRIO" else if (cantDobles == 1) "PAREJA" else if (cantDobles == 2) "DOBLE PAREJA" else "CARTA ALTA" } } } return result } fun cargarPalos(jugada : List) : Array{ val result = arrayOf(0,0,0,0) for (palos in jugada){ when(palos.palo){ 'S' -> result[0]++ 'C' -> result[1]++ 'D' -> result[2]++ 'H' -> result[3]++ } } return result } fun cargarValores(jugada : List) : Array{ val result = arrayOf(0,0,0,0,0,0,0,0,0,0,0,0,0) for (valores in jugada){ when(valores.valor){ 'A' -> result[0]++ '2' -> result[1]++ '3' -> result[2]++ '4' -> result[3]++ '5' -> result[4]++ '6' -> result[5]++ '7' -> result[6]++ '8' -> result[7]++ '9' -> result[8]++ 'T' -> result[9]++ 'J' -> result[10]++ 'Q' -> result[11]++ 'K' -> result[12]++ } } return result } fun jugadaGanadora(jugada1 : List, tipo1 :String, jugada2 : List, tipo2 :String) : String{ var ganador = "NINGUNO" val palosJugados1 = cargarPalos(jugada1) val valoresJugados1 = cargarValores(jugada1) val palosJugados2 = cargarPalos(jugada2) val valoresJugados2 = cargarValores(jugada2) val jugadas: ArrayList = arrayListOf(0,0,0,0,0,0,0,0,0,0) when(tipo1){ "ESCALERA REAL" -> jugadas[0]++ "ESCALERA COLOR" -> jugadas[1]++ "POKER" -> jugadas[2]++ "FULL HOUSE" -> jugadas[3]++ "COLOR" -> jugadas[4]++ "ESCALERA" -> jugadas[5]++ "TRIO" -> jugadas[6]++ "DOBLE PAREJA" -> jugadas[7]++ "PAREJA" -> jugadas[8]++ "CARTA ALTA" -> jugadas[9]++ } when(tipo2){ "ESCALERA REAL" -> jugadas[0]++ "ESCALERA COLOR" -> jugadas[1]++ "POKER" -> jugadas[2]++ "FULL HOUSE" -> jugadas[3]++ "COLOR" -> jugadas[4]++ "ESCALERA" -> jugadas[5]++ "TRIO" -> jugadas[6]++ "DOBLE PAREJA" -> jugadas[7]++ "PAREJA" -> jugadas[8]++ "CARTA ALTA" -> jugadas[9]++ } val posGanador: Int var segundo = 0 loop@ for(casos in jugadas.indices){ when(jugadas[casos]) { 1->{ posGanador = casos ganador = when(posGanador){ 0 -> "ESCALERA REAL" 1 -> "ESCALERA COLOR" 2 -> "POKER" 3 -> "FULL HOUSE" 4 -> "COLOR" 5 -> "ESCALERA" 6 -> "TRIO" 7 -> "DOBLE PAREJA" 8 -> "PAREJA" 9 -> "CARTA ALTA" else -> "NADA" } break@loop } 2->{ //"NADA" when(casos){ 0 -> { //"ESCALERA REAL" ganador = "EMPATE" } 1 ->{ //"ESCALERA COLOR" var ultimo = 1 var kicker1 = 0 var kicker2 = 0 loopjugado1@ for (indice in valoresJugados1.indices) { print("${indice+1} ${valoresJugados1[indice]}, ") if (valoresJugados1[indice] == 1) { if (ultimo == 5) { kicker1 = indice break@loopjugado1 } ultimo++ } } ultimo = 1 println() loopjugado2@ for (indice in valoresJugados2.indices){ print("${indice+1} ${valoresJugados2[indice]}, ") if (valoresJugados2[indice] == 1) { if (ultimo == 5){ kicker2 = indice break@loopjugado2 } ultimo++ } } println("kicker1 = $kicker1 kicker2 = $kicker2") ganador = if (kicker1 == 0 && kicker2 != 0){ "JUGADOR 1" }else if (kicker2 == 0 && kicker1 != 0) { "JUGADOR 2" }else if (kicker1 == kicker2){ "EMPATE" }else if (kicker1 > kicker2) { "JUGADOR 1" } else "JUGADOR 2" } 2 ->{ //"POKER" var ultimo = 0 var kicker1 = 0 var kicker2 = 0 for (indice in valoresJugados1.indices) { if (valoresJugados1[indice] == 1) { if (ultimo == 5) { kicker1 = indice } ultimo++ } } ultimo = 0 for (indice in valoresJugados2.indices){ if (valoresJugados2[indice] == 1) { if (ultimo == 5){ kicker2 = indice } ultimo++ } } ganador = if (kicker1 == 0 && kicker2 != 0){ "JUGADOR 1" }else if (kicker2 == 0 && kicker1 != 0) { "JUGADOR 2" }else if (kicker1 == kicker2){ "EMPATE" }else if (kicker1 > kicker2) { "JUGADOR 1" } else "JUGADOR 2" } 3 -> { //"FULL HOUSE" var trio1 = -1 for (indice in valoresJugados1.indices) { if (valoresJugados1[indice] == 3) { trio1 = indice } } var trio2 = -1 for (indice in valoresJugados2.indices) { if (valoresJugados2[indice] == 3) { trio2 = indice } } ganador = if (trio1 == 0 && trio2 != 0) { "JUGADOR 1" } else if (trio2 == 0 && trio1 != 0) { "JUGADOR 2" } else if (trio1 == trio2) { "EMPATE" } else if (trio1 > trio2) { "JUGADOR 1" } else "JUGADOR 2" } 4 ->{ //"COLOR" var ultimo = 1 var kicker1 = 0 var kicker2 = 0 loopjugado1@ for (indice in valoresJugados1.indices) { print("${indice+1} ${valoresJugados1[indice]}, ") if (valoresJugados1[indice] == 1) { if (ultimo == 5 || indice == 0) { kicker1 = indice break@loopjugado1 } ultimo++ } } ultimo = 1 println() loopjugado2@ for (indice in valoresJugados2.indices){ print("${indice+1} ${valoresJugados2[indice]}, ") if (valoresJugados2[indice] == 1) { if (ultimo == 5 || indice == 0){ kicker2 = indice break@loopjugado2 } ultimo++ } } println("kicker1 = $kicker1 kicker2 = $kicker2") ganador = if (kicker1 == 0 && kicker2 != 0){ "JUGADOR 1" }else if (kicker2 == 0 && kicker1 != 0) { "JUGADOR 2" }else if (kicker1 == kicker2){ "EMPATE" }else if (kicker1 > kicker2) { "JUGADOR 1" } else "JUGADOR 2" } 5 ->{ //"ESCALERA" var ultimo = 1 var kicker1 = 0 var kicker2 = 0 loopjugado1@ for (indice in valoresJugados1.indices) { print("${indice+1} ${valoresJugados1[indice]}, ") if (valoresJugados1[indice] == 1) { if (ultimo == 5 || (indice == 0 && valoresJugados1[9] == 1 && valoresJugados1[10] == 1 && valoresJugados1[11] == 1 && valoresJugados1[12] == 1)){ kicker1 = indice break@loopjugado1 } ultimo++ } } ultimo = 1 println() loopjugado2@ for (indice in valoresJugados2.indices){ print("${indice+1} ${valoresJugados2[indice]}, ") if (valoresJugados2[indice] == 1) { if (ultimo == 5 || (indice == 0 && valoresJugados1[9] == 1 && valoresJugados1[10] == 1 && valoresJugados1[11] == 1 && valoresJugados1[12] == 1)){ kicker2 = indice break@loopjugado2 } ultimo++ } } println("kicker1 = $kicker1 kicker2 = $kicker2") ganador = if (kicker1 == 0 && kicker2 != 0){ "JUGADOR 1" }else if (kicker2 == 0 && kicker1 != 0) { "JUGADOR 2" }else if (kicker1 == kicker2){ "EMPATE" }else if (kicker1 > kicker2) { "JUGADOR 1" } else "JUGADOR 2" } 6 ->{ //"TRIO" var trio1 = -1 for (indice in valoresJugados1.indices) { if (valoresJugados1[indice] == 3) { trio1 = indice } } var trio2 = -1 for (indice in valoresJugados2.indices) { if (valoresJugados2[indice] == 3) { trio2 = indice } } ganador = if (trio1 == 0 && trio2 != 0) { "JUGADOR 1" } else if (trio2 == 0 && trio1 != 0) { "JUGADOR 2" } else if (trio1 == trio2) { "EMPATE" } else if (trio1 > trio2) { "JUGADOR 1" } else "JUGADOR 2" } 7 ->{ //"DOBLE PAREJA" var primerDoble = -1 var segundoDoble = -1 loopintern@for (indice in valoresJugados1.indices){ if (segundo == 1){ if (valoresJugados1[indice] == 2){ segundoDoble = indice break@loopintern } } if (valoresJugados1[indice] == 2 && segundo == 0){ primerDoble = indice segundo = 1 } } var primerDoble2 = -1 var segundoDoble2 = -1 segundo = 0 loopintern@for (indice in valoresJugados2.indices){ if (segundo == 1){ if (valoresJugados2[indice] == 2){ segundoDoble2 = indice segundo = 0 break@loopintern } } if (valoresJugados2[indice] == 2 && segundo == 0){ primerDoble2 = indice segundo = 1 } } ganador = if (primerDoble == 0 && primerDoble2 != 0) { "JUGADOR 1" } else if (primerDoble2 == 0 && primerDoble != 0) { "JUGADOR 2" } else if (primerDoble == primerDoble2) { when { segundoDoble == segundoDoble2 -> { "EMPATE" } segundoDoble > segundoDoble2 -> { "JUGADOR 1" } else -> { "JUGADOR 2" } } } else if (primerDoble > primerDoble2) { "JUGADOR 1" } else "JUGADOR 2" } 8 ->{ //"PAREJA" var ultimo = 1 var kicker1 = 0 var kicker2 = 0 var primerDoble = -1 for (indice in valoresJugados1.indices){ if (valoresJugados1[indice] == 2){ primerDoble = indice ultimo = 2 } } loopjugado1@ for (indice in valoresJugados1.indices) { if (valoresJugados1[indice] == 1) { if (ultimo == 4 || (indice == 0)){ kicker1 = if (indice == 0) 14 else indice break@loopjugado1 } ultimo++ } } ultimo = 1 var primerDoble2 = -1 for (indice in valoresJugados2.indices){ if (valoresJugados2[indice] == 2){ primerDoble2 = indice ultimo = 2 } } loopjugado2@ for (indice in valoresJugados2.indices){ if (valoresJugados2[indice] == 1) { if (ultimo == 4 || (indice == 0)){ kicker2 = if (indice == 0) 14 else indice break@loopjugado2 } ultimo++ } } ganador = if (primerDoble == 0 && primerDoble2 != 0) { "JUGADOR 1" } else if (primerDoble2 == 0 && primerDoble != 0) { "JUGADOR 2" } else if (primerDoble == primerDoble2) { when { kicker1 > kicker2 -> "JUGADOR 1 Por Kicker mas alto" kicker1 < kicker2 -> "JUGADOR 2 Por Kicker mas alto" else -> "EMPATE" } } else if (primerDoble > primerDoble2) { "JUGADOR 1" } else "JUGADOR 2" } 9 ->{ //"CARTA ALTA" var ultimo = 1 var kicker1 = 0 var kicker2 = 0 loopjugado1@ for (indice in valoresJugados1.indices) { if (valoresJugados1[indice] == 1) { if (ultimo == 5 || (indice == 0)){ kicker1 = indice break@loopjugado1 } ultimo++ } } ultimo = 1 println() loopjugado2@ for (indice in valoresJugados2.indices){ if (valoresJugados2[indice] == 1) { if (ultimo == 5 || (indice == 0)){ kicker2 = indice break@loopjugado2 } ultimo++ } } ganador = if (kicker1 == 0 && kicker2 != 0){ "JUGADOR 1" }else if (kicker2 == 0 && kicker1 != 0) { "JUGADOR 2" }else if (kicker1 == kicker2){ "EMPATE" }else if (kicker1 > kicker2) { "JUGADOR 1" } else "JUGADOR 2" } else -> ganador = "ERROR 5: JUGADA NO ENCONTRADA" } } } } return ganador } fun jugadaMesa(){ var core = 1 do { val tipoPalo = Random.nextInt(0, 3) val cartaSelect = Random.nextInt(0, 12) when(core) { in 1..5 -> if (Const.MESA[tipoPalo][cartaSelect] == 0) { val column = Const.MESA[tipoPalo] column[cartaSelect] = 1 Const.MESA[tipoPalo] = column core++ } 6, 7 ->if (Const.MESA[tipoPalo][cartaSelect] == 0) { val column = Const.MESA[tipoPalo] column[cartaSelect] = 2 Const.MESA[tipoPalo] = column core++ } 8, 9 ->if (Const.MESA[tipoPalo][cartaSelect] == 0) { val column = Const.MESA[tipoPalo] column[cartaSelect] = 3 Const.MESA[tipoPalo] = column core++ } } }while (core <= 9) //imprime jugadas Poker for (fila in Const.MESA.indices){ when(fila){ 0 -> print("S ") 1 -> print("D ") 2 -> print("H ") 3 -> print("C ") } for (columnas in Const.MESA[fila].indices) print("${Const.MESA[fila][columnas]}, ") println() } } fun decodificar(jugador: Int): ArrayList{ val jugadordecodif = jugador +1 val jugada = ArrayList() for (fila in Const.MESA.indices){ for (columnas in Const.MESA[fila].indices) if (Const.MESA[fila][columnas] == jugadordecodif || Const.MESA[fila][columnas] == 1) { val caracterPalo =when(fila){ 0->'S' 1->'C' 2->'D' 3->'H' else -> 'x' } val caracterValor =when(columnas){ 0 -> 'A' 1 -> '2' 2 -> '3' 3 -> '4' 4 -> '5' 5 -> '6' 6 -> '7' 7 -> '8' 8 -> '9' 9 -> 'T' 10 -> 'J' 11 -> 'Q' 12 -> 'K' else -> 'x' } jugada.add(CartaJugada(valor = caracterValor, palo = caracterPalo)) } } return jugada }