diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..74cd95c --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.vscode/* +.haxelib/ +.github/ +export/* +*.code-workspace +# Local history which shouldn't be shared. +.history +.ionide +# ! means a file should be added regardless of it it gets ignored prior. +# Including to allow others to use already set-up configuration tweaked for the project. +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..89e20ed --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "openfl.lime-vscode-extension", + "redhat.vscode-xml" + ] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5e9a7a1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Build + Debug", + "type": "lime", + "request": "launch" + }, + { + "name": "Debug", + "type": "lime", + "request": "launch", + "preLaunchTask": null + }, + { + "name": "Macro", + "type": "haxe-eval", + "request": "launch" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..03434a8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "search.exclude": { + "export/**/*.hx": true + }, + "[haxe]": { + "editor.formatOnSave": true, + "editor.formatOnPaste": true, + "editor.codeActionsOnSave": { + "source.sortImports": "explicit" + } + }, + "haxe.enableExtendedIndentation": true +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..16a7764 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,13 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "lime", + "command": "test", + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} diff --git a/Project.xml b/Project.xml new file mode 100644 index 0000000..5dd139a --- /dev/null +++ b/Project.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c599ca1 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Simulacion BlackJack + diff --git a/assets/data/data-goes-here.txt b/assets/data/data-goes-here.txt new file mode 100644 index 0000000..e69de29 diff --git a/assets/images/Card_default.png b/assets/images/Card_default.png new file mode 100644 index 0000000..2aefdf6 Binary files /dev/null and b/assets/images/Card_default.png differ diff --git a/assets/images/Cartas.png b/assets/images/Cartas.png new file mode 100644 index 0000000..50d35a5 Binary files /dev/null and b/assets/images/Cartas.png differ diff --git a/assets/images/Cartas.xml b/assets/images/Cartas.xml new file mode 100644 index 0000000..7398d07 --- /dev/null +++ b/assets/images/Cartas.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/assets/images/images-go-here.txt b/assets/images/images-go-here.txt new file mode 100644 index 0000000..e69de29 diff --git a/assets/music/music-goes-here.txt b/assets/music/music-goes-here.txt new file mode 100644 index 0000000..e69de29 diff --git a/assets/sounds/sounds-go-here.txt b/assets/sounds/sounds-go-here.txt new file mode 100644 index 0000000..e69de29 diff --git a/hxformat.json b/hxformat.json new file mode 100644 index 0000000..66cb386 --- /dev/null +++ b/hxformat.json @@ -0,0 +1,15 @@ +{ + "lineEnds": { + "leftCurly": "both", + "rightCurly": "both", + "objectLiteralCurly": { + "leftCurly": "after" + } + }, + "sameLine": { + "ifElse": "next", + "doWhile": "next", + "tryBody": "next", + "tryCatch": "next" + } +} diff --git a/source/AssetPaths.hx b/source/AssetPaths.hx new file mode 100644 index 0000000..db7ef44 --- /dev/null +++ b/source/AssetPaths.hx @@ -0,0 +1,4 @@ +package; + +@:build(flixel.system.FlxAssets.buildFileReferences("assets", true)) +class AssetPaths {} diff --git a/source/Cartas.hx b/source/Cartas.hx new file mode 100644 index 0000000..4fc9955 --- /dev/null +++ b/source/Cartas.hx @@ -0,0 +1,126 @@ +import flixel.FlxSprite; +import flixel.group.FlxGroup.FlxTypedGroup; +import haxe.ds.Map; +import objects.Carta; + +class Cartas extends FlxTypedGroup +{ + public var cartas:Map; + + var cartasGeneradas:Array; + var indexNumero:Map = [ + "A" => "0012", "2" => "0011", "3" => "0010", "4" => "0009", "5" => "0008", "6" => "0007", "7" => "0006", "8" => "0005", "9" => "0004", "10" => "0003", + "J" => "0002", "Q" => "0001", "K" => "0000" + ]; + var indexNumeroCarta:Array = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + var indexPalo:Array = ["Corazones", "Diamantes", "Picas", "Treboles"]; + + public function new() + { + super(52); + cartas = new Map(); + // generarCartas(); + } + + public function repartirCarta() + { + var cartaActual = cartas.get(cartasGeneradas[cartasGeneradas.length - 1]); + cartas.remove(cartasGeneradas.pop()); // Retira la carta repartida + return cartaActual; + } + /* + function generarCartas() + { + while (cartasGeneradas.length < 53) + { + var ranPalo = Math.random(); + var palo:String; // Nombre del palo + var ranNumero = Math.random(); + var numero:String; + var numeroSprite:String; + var numeroCarta:String; + var carta:FlxSprite; + var cartaGenerada:String; + // Palo + if (ranPalo <= 1 / 4) + palo = indexPalo[0]; + else if (ranPalo <= 2 / 4) + palo = indexPalo[1]; + else if (ranPalo <= 3 / 4) + palo = indexPalo[2]; + else + palo = indexPalo[3]; + + // Numero + if (ranNumero > 0 && ranNumero < 1 / 13) + { + numero = indexNumeroCarta[0]; + } + else if (ranNumero > 1 / 13 && ranNumero < 2 / 13) + { + numero = indexNumeroCarta[1]; + } + else if (ranNumero > 2 / 13 && ranNumero < 3 / 13) + { + numero = indexNumeroCarta[2]; + } + else if (ranNumero > 3 / 13 && ranNumero < 4 / 13) + { + numero = indexNumeroCarta[3]; + } + else if (ranNumero > 4 / 13 && ranNumero < 5 / 13) + { + numero = indexNumeroCarta[4]; + } + else if (ranNumero > 5 / 13 && ranNumero < 6 / 13) + { + numero = indexNumeroCarta[5]; + } + else if (ranNumero > 6 / 13 && ranNumero < 7 / 13) + { + numero = indexNumeroCarta[6]; + } + else if (ranNumero > 7 / 13 && ranNumero < 8 / 13) + { + numero = indexNumeroCarta[7]; + } + else if (ranNumero > 8 / 13 && ranNumero < 9 / 13) + { + numero = indexNumeroCarta[8]; + } + else if (ranNumero > 9 / 13 && ranNumero < 10 / 13) + { + numero = indexNumeroCarta[9]; + } + else if (ranNumero > 10 / 13 && ranNumero < 11 / 13) + { + numero = indexNumeroCarta[10]; + } + else if (ranNumero > 11 / 13 && ranNumero < 12 / 13) + { + numero = indexNumeroCarta[11]; + } + else if (ranNumero > 12 / 13 && ranNumero < 13 / 13) + { + numero = indexNumeroCarta[12]; + } + numeroSprite = indexNumero.get(numero); + + cartaGenerada = palo + numero; // 'DiamanteA' | 'Trebol10' | ... + numeroCarta = palo + numeroSprite; // 'Diamante0000' + + if (cartasGeneradas.contains(cartaGenerada)) + continue; + + carta = new FlxSprite().loadGraphic("assets/images/Cartas.png"); + carta.animation.addByNames(numero, [numeroCarta]); + add(carta); + carta.animation.play(numero); + + cartas.set(numero, carta); + cartasGeneradas.push(cartaGenerada); + // carta.animation.play(numero); + } + } + */ +} diff --git a/source/Main.hx b/source/Main.hx new file mode 100644 index 0000000..98cdef9 --- /dev/null +++ b/source/Main.hx @@ -0,0 +1,13 @@ +package; + +import flixel.FlxGame; +import openfl.display.Sprite; + +class Main extends Sprite +{ + public function new() + { + super(); + addChild(new FlxGame(960, 720, PlayState, 60, 60, false, false)); + } +} diff --git a/source/PlayState.hx b/source/PlayState.hx new file mode 100644 index 0000000..fb48ee1 --- /dev/null +++ b/source/PlayState.hx @@ -0,0 +1,515 @@ +package; + +import flixel.FlxG; +import flixel.FlxSprite; +import flixel.FlxState; +import flixel.addons.plugin.taskManager.FlxTask; +import flixel.addons.ui.FlxInputText; +import flixel.graphics.frames.FlxAtlasFrames; +import flixel.group.FlxGroup.FlxTypedGroup; +import flixel.text.FlxText; +import flixel.tweens.FlxEase; +import flixel.tweens.FlxTween; +import flixel.ui.FlxButton; +import flixel.util.FlxAxes; +import flixel.util.FlxColor; +import flixel.util.FlxDestroyUtil; +import flixel.util.FlxTimer; +import lime.app.Application; +import objects.Carta; +import objects.Player; +import utils.ArrayUtil; +#if cpp +import cpp.vm.Gc; +#elseif hl +import hl.Gc; +#end + +class PlayState extends FlxState +{ + var first:Bool = true; + var title:FlxText; + var playerInput:FlxInputText; + var text1:FlxText; + var timesInput:FlxInputText; + var text2:FlxText; + var start:FlxButton; + var mazoDisponible:FlxSprite; + var timer:FlxTimer; + var info:FlxText; + var offs = 30; + + /** + * Jugadores + */ + var players:Array; + + var playersLabel:Array; + var playersPoints:Array; + + /** + * El crupier de la partida + */ + var dealer:Player; + + var dealerLabel:FlxText; + var dealerPoints:FlxText; + + /** + * Mazo de cartas actual + */ + var mazo:Array; + + var excludeCard:Array = []; + + /** + * Vista de las cartas actuales + */ + var vistaMazo:FlxTypedGroup; + + public function new() + { + super(); + vistaMazo = new FlxTypedGroup(52); + players = new Array(); + timer = new FlxTimer(); + } + + function Simulacion(n:Int, p:Int) + { + var ganados = [for (i in 0...p) 0]; + var blackjacks = 0; + var empates = 0; + + dealer = new Player("Crupier"); + for (i in 0...p) + { + players.push(new Player('Jugador ${i + 1}')); + } + mazo = generarMazo(); + for (i in mazo) + vistaMazo.add(i); + + function juego(t:FlxTimer) + { + var curPlayerPoints = [for (i in 0...p) 0]; + // var termina:Bool = false; + if (t.elapsedLoops > 1) + { + reset(); + for (i in playersLabel) + i.color = FlxColor.WHITE; + dealerPoints.text = ""; + info.text = ""; + } + var j = 0; + repartirCartas(); + repartirCartas2da(); + t.active = false; + new FlxTimer().start(0.7, function(tp:FlxTimer) + { + if (j > (p - 1)) + { + for (i in 0...players.length) + { + if (players[i].canPlay) + curPlayerPoints[i] = players[i].points; + } + trace("Crupier"); + dealer.mano.push(mazo.pop()); + FlxTween.tween(dealer.mano[dealer.mano.length - 1], { + x: dealer.mano[dealer.mano.length - 2].x + offs, + y: dealer.mano[dealer.mano.length - 2].y + }, 0.5, {ease: FlxEase.sineInOut}); + dealerPoints.text = ' -> ${dealer.points}'; + tp.active = false; + new FlxTimer().start(0.5, function(cur:FlxTimer) + { + if (ArrayUtil.compareIntInArray(curPlayerPoints.filter(value -> value != 0), dealer.points)) + { + // Pide carta + dealer.mano.push(mazo.pop()); + FlxTween.tween(dealer.mano[dealer.mano.length - 1], { + x: dealer.mano[dealer.mano.length - 2].x + offs, + y: dealer.mano[dealer.mano.length - 2].y + }, 0.5, {ease: FlxEase.sineInOut}); + trace('${dealer.ID} - ${dealer.points}'); + dealerPoints.text = ' -> ${dealer.points}'; + // playersPoints[j].text = '${players[j].points}'; + } + else + { + // Se planta + if (dealer.points >= 22) + { + // players[j].canPlay = false; + // playersLabel[j].color = FlxColor.RED; + dealerLabel.color = FlxColor.RED; + var w = calcularPuntos(curPlayerPoints); + ganados[w - 1] += 1; + trace('Jugador ${w}'); + info.text = 'Jugador ${w}'; + } + else if (players[calcularPuntos(curPlayerPoints) - 1].points < dealer.points || everyLose() == p) + { + dealerLabel.color = FlxColor.GREEN; + info.text = 'El Crupier ganó'; + trace('El Crupier ganó'); + } + else if (ArrayUtil.arrayDuplicates(curPlayerPoints)) + { + info.text = 'Empate'; + trace('Empate'); + empates++; + } + else + { + var w = calcularPuntos(curPlayerPoints); + ganados[w - 1] += 1; + trace('Jugador ${w}'); + info.text = 'Jugador ${w}'; + } + tp.active = true; + cur.cancel(); + } + tp.active = true; + cur.cancel(); + if (t.finished && tp.finished) + openResultState(ganados, blackjacks, n, empates); + }, 0); + t.active = true; + tp.active = true; + tp.cancel(); + return; + } + // for (i in playersLabel) + // i.color = FlxColor.WHITE; + playersLabel[j].color = FlxColor.LIME; + tp.active = false; + + new FlxTimer().start(0.5, function(cur:FlxTimer) + { + if (players[j].hasBlackjack && cur.elapsedLoops == 1) + { + trace("BlackJack!"); + // players[j].points = 21; + playersPoints[j].text = '${players[j].points}'; + blackjacks++; + j++; + tp.active = true; + cur.cancel(); + } + else if (FlxG.random.bool() && players[j].points < 22) + { + // Pide carta + players[j].mano.push(mazo.pop()); + FlxTween.tween(players[j].mano[players[j].mano.length - 1], { + x: players[j].mano[players[j].mano.length - 2].x + offs, + y: players[j].mano[players[j].mano.length - 2].y + }, 0.5, {ease: FlxEase.sineInOut}); + trace('${players[j].ID} - ${players[j].points}'); + playersPoints[j].text = '${players[j].points}'; + } + else + { + // Se planta + if (players[j].points >= 22) + { + players[j].canPlay = false; + playersLabel[j].color = FlxColor.RED; + } + j++; + tp.active = true; + cur.cancel(); + } + // trace('${players[j].ID} - ${players[j].points}'); + }, 0); + }, p + 1); + trace("End"); + } + + timer.start(1.8, juego, n); + } + + function repartirCartas2da() + { + if (mazo.length <= 0) + return; + for (i in 0...players.length) + { + var curCard = mazo.pop(); + excludeCard.push(curCard); + players[i].mano.push(curCard); + switch ((i + 1)) + { + case 1: + // players[i].mano[0].setPosition(80, 530); + FlxTween.tween(players[i].mano[1], {x: 80 + offs, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + case 2: + // players[i].mano[0].setPosition(280, 530); + FlxTween.tween(players[i].mano[1], {x: 280 + offs, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + case 3: + // players[i].mano[0].setPosition(480, 530); + FlxTween.tween(players[i].mano[1], {x: 480 + offs, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + case 4: + // players[i].mano[0].setPosition(680, 530); + FlxTween.tween(players[i].mano[1], {x: 680 + offs, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + } + playersPoints[i].text = '${players[i].points}'; + } + } + + function repartirCartas() + { + trace(mazo.length); + if (mazo.length <= 0) + return; + for (i in 0...players.length) + { + var curCard = mazo.pop(); + excludeCard.push(curCard); + players[i].mano.push(curCard); + switch ((i + 1)) + { + case 1: + // players[i].mano[0].setPosition(80, 530); + FlxTween.tween(players[i].mano[0], {x: 80, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + case 2: + // players[i].mano[0].setPosition(280, 530); + FlxTween.tween(players[i].mano[0], {x: 280, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + case 3: + // players[i].mano[0].setPosition(480, 530); + FlxTween.tween(players[i].mano[0], {x: 480, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + case 4: + // players[i].mano[0].setPosition(680, 530); + FlxTween.tween(players[i].mano[0], {x: 680, y: 530}, 0.5, {ease: FlxEase.sineInOut}); + } + + playersLabel[i].text = '${players[i].ID}'; + } + var curCard = mazo.pop(); + excludeCard.push(curCard); + dealer.mano.push(curCard); + // dealer.mano[0].setPosition(376, 131); + FlxTween.tween(dealer.mano[0], {x: 376, y: 131}, 0.5, {ease: FlxEase.sineInOut}); + } + + function generarMazo():Array + { + var exclude:Array = []; + var _mazo:Array = []; + var j = 0; + while (j < 52) + { + var valor = {palo: "", numero: ""}; + var nPalo = Math.random(); + var nNumero = Math.random(); + // Palo + if (nPalo < 0.25) + valor.palo = "Corazones"; + else if (nPalo > 0.25 && nPalo < 0.5) + valor.palo = "Diamantes"; + else if (nPalo > 0.5 && nPalo < 0.75) + valor.palo = "Picas"; + else + valor.palo = "Treboles"; + // Numero + if (nNumero < 0.077) + valor.numero = "A"; + else if (nNumero > 0.077 && nNumero < 0.154) + valor.numero = "2"; + else if (nNumero > 0.154 && nNumero < 0.231) + valor.numero = "3"; + else if (nNumero > 0.231 && nNumero < 0.308) + valor.numero = "4"; + else if (nNumero > 0.308 && nNumero < 0.385) + valor.numero = "5"; + else if (nNumero > 0.385 && nNumero < 0.462) + valor.numero = "6"; + else if (nNumero > 0.462 && nNumero < 0.538) + valor.numero = "7"; + else if (nNumero > 0.538 && nNumero < 0.615) + valor.numero = "8"; + else if (nNumero > 0.615 && nNumero < 0.691) + valor.numero = "9"; + else if (nNumero > 0.692 && nNumero < 0.769) + valor.numero = "10"; + else if (nNumero > 0.769 && nNumero < 0.846) + valor.numero = "J"; + else if (nNumero > 0.846 && nNumero < 0.923) + valor.numero = "Q"; + else + valor.numero = "K"; + if (exclude.contains('${valor.palo}${valor.numero}')) + continue; + + exclude.push('${valor.palo}${valor.numero}'); + + _mazo.push(new Carta(mazoDisponible.x, mazoDisponible.y, valor)); + // vistaMazo.add(_mazo[j]); + j++; + } + return _mazo; + } + + function reset() + { + #if hl + Gc.enable(true); + #end + for (i in players) + { + FlxDestroyUtil.destroyArray(i.mano); + i.mano.splice(0, i.mano.length); + i.setDefault(); + } + FlxDestroyUtil.destroyArray(dealer.mano); + // players.splice(0, players.length); + dealer.mano.splice(0, dealer.mano.length); + dealer.mano = []; + #if cpp + Gc.run(true); + #elseif hl + Gc.major(); + #end + } + + function calcularPuntos(arr:Array) + { + var l = arr[0]; + var p = 1; + for (k in 1...arr.length) + { + if (l < arr[k]) + { + l = arr[k]; + p = k + 1; + } + } + return p; + } + + function everyLose() + { + var a = 0; + for (i in players) + { + if (!i.canPlay) + a++; + } + return a; + } + + function isBlackJack(arr:Array) + { + var carta1 = arr[0]; + var carta2 = arr[1]; + trace('${carta1.curName} - ${carta2.curName}'); + if ((carta1.isAs && carta2.points == 10) || (carta2.isAs && carta1.points == 10)) + return true; + return false; + } + + function onClick() + { + var n = Std.parseInt(timesInput.text); + var p = Std.parseInt(playerInput.text); + if (p < 2 || p > 4) + { + Application.current.window.alert("El numero de jugadores debe entre 2, 3 y 4", "No. de Jugadores Invalido"); + return; + } + else + { + trace("Starting..."); + if (!first) + { + players.splice(0, players.length); + } + Simulacion(n, p); + /* + new FlxTimer().start(2, function(t:FlxTimer) + { + reset(); + }); + */ + } + } + + function openResultState(w:Array, b:Int, j:Int, e:Int) + { + openSubState(new ResultState(w, b, j, e)); + } + + override public function create() + { + super.create(); + title = new FlxText(0, 0, 0, "BlackJack", 48); + title.color = FlxColor.GRAY; + title.screenCenter(); + add(title); + + text1 = new FlxText(10, 10, 0, "¿Cuantos juegos quieres jugar?", 16); + add(text1); + timesInput = new FlxInputText(10, 35, 150, '', 16); + add(timesInput); + + text2 = new FlxText(text1.x + text1.width + 40, text1.y, 0, "No. Jugadores", 16); + add(text2); + playerInput = new FlxInputText(text2.x, timesInput.y, 50, '4', 16); + add(playerInput); + + start = new FlxButton(text2.x + text2.width + 40, playerInput.y, "Iniciar", onClick); + // start.screenCenter(FlxAxes.X); + add(start); + + add(vistaMazo); + + mazoDisponible = new FlxSprite(text1.x, text1.y + text1.height + 50).loadGraphic("assets/images/Card_default.png"); + mazoDisponible.scale.set(0.5, 0.5); + mazoDisponible.updateHitbox(); + add(mazoDisponible); + + playersLabel = [ + new FlxText(80, 665, 0, "", 16), + new FlxText(280, 665, 0, "", 16), + new FlxText(480, 665, 0, "", 16), + new FlxText(680, 665, 0, "", 16) + ]; + for (i in playersLabel) + add(i); + + playersPoints = [ + new FlxText(80, 665 + playersLabel[0].height, 0, "", 16), + new FlxText(280, 665 + playersLabel[1].height, 0, "", 16), + new FlxText(480, 665 + playersLabel[2].height, 0, "", 16), + new FlxText(680, 665 + playersLabel[3].height, 0, "", 16) + ]; + + for (i in playersPoints) + add(i); + + dealerLabel = new FlxText(410, 104, 0, "Crupier/Dealer", 16); + add(dealerLabel); + dealerPoints = new FlxText(dealerLabel.x + dealerLabel.width + 20, dealerLabel.y, 0, "", 16); + add(dealerPoints); + + info = new FlxText(10, 400, 0, "", 16); + add(info); + } + + override public function update(elapsed:Float) + { + super.update(elapsed); + if (timesInput.hasFocus || playerInput.hasFocus) + { + FlxG.sound.volumeDownKeys = FlxG.sound.volumeUpKeys = FlxG.sound.muteKeys = null; + } + else + { + FlxG.sound.volumeDownKeys = [MINUS, NUMPADMINUS]; + FlxG.sound.volumeUpKeys = [MINUS, NUMPADPLUS]; + FlxG.sound.muteKeys = [ZERO, NUMPADZERO]; + } + } +} diff --git a/source/ResultState.hx b/source/ResultState.hx new file mode 100644 index 0000000..5f97149 --- /dev/null +++ b/source/ResultState.hx @@ -0,0 +1,59 @@ +import flixel.FlxG; +import flixel.FlxSprite; +import flixel.FlxSubState; +import flixel.text.FlxText; +import flixel.ui.FlxButton; +import flixel.util.FlxAxes; +import flixel.util.FlxColor; + +class ResultState extends FlxSubState +{ + var info:FlxText; + + var winners:Array; + var blackjacks:Int; + var emp:Int; + var juegos:Int; + + public function new(winners:Array, blackjacks:Int, juegos:Int, emp:Int) + { + super(FlxColor.fromRGB(0, 0, 0, 180)); + this.winners = winners; + this.blackjacks = blackjacks; + this.emp = emp; + this.juegos = juegos; + } + + override function create() + { + super.create(); + + var text = new FlxText(0, 20); + text.text = "Resultados Finales"; + text.color = FlxColor.BLUE; + text.size = 36; + text.setBorderStyle(FlxTextBorderStyle.SHADOW, FlxColor.CYAN, 3); + text.screenCenter(FlxAxes.X); + + info = new FlxText(); + info.text = "Jugadas ganadas: "; + for (i in 0...winners.length) + { + info.text += '\nJugador ${i + 1} - ${winners[i]}'; + } + info.text += '\nBlackjacks ${blackjacks} (${blackjacks / juegos * 100}%)'; + info.size = 24; + info.setBorderStyle(FlxTextBorderStyle.OUTLINE, FlxColor.BLACK, 3); + info.screenCenter(); + + var closebtn = new FlxButton(0, 680, "Cerrar", () -> + { + close(); + }); + closebtn.x = text.x + text.height; + + add(text); + add(info); + add(closebtn); + } +} diff --git a/source/objects/Carta.hx b/source/objects/Carta.hx new file mode 100644 index 0000000..6155604 --- /dev/null +++ b/source/objects/Carta.hx @@ -0,0 +1,75 @@ +package objects; + +import flixel.FlxSprite; +import flixel.graphics.frames.FlxAtlasFrames; +import haxe.ds.Map; + +class Carta extends FlxSprite +{ + public var valor(get, null):ValorCarta; + public var points(get, null):Int; + public var curName(get, null):String; + public var isAs(get, null):Bool; + + var indexNumero:Map = [ + "A" => "0012", "2" => "0011", "3" => "0010", "4" => "0009", "5" => "0008", "6" => "0007", "7" => "0006", "8" => "0005", "9" => "0004", "10" => "0003", + "J" => "0002", "Q" => "0001", "K" => "0000" + ]; + + public function new(x:Float, y:Float, valor:ValorCarta) + { + super(x, y); + this.valor = valor; + setSprite(valor); + } + + function setSprite(valor:ValorCarta) + { + this.loadGraphic("assets/images/Cartas.png", true); + this.frames = FlxAtlasFrames.fromSparrow("assets/images/Cartas.png", "assets/images/Cartas.xml"); + this.animation.addByStringIndices(curName, valor.palo, [indexNumero[valor.numero]], ".png", 1, false); + this.animation.play(curName); + this.scale.set(0.5, 0.5); + this.updateHitbox(); + } + + function get_valor():ValorCarta + { + return valor; + } + + function get_curName():String + { + return '${valor.palo}${valor.numero}'; + } + + function get_points():Int + { + switch (valor.numero) + { + case "A": + return 1; + case "J" | "Q" | "K": + return 10; + case _: + return Std.parseInt(valor.numero); + } + return 0; + } + + function get_isAs():Bool + { + return valor.numero == "A"; + } + + override function destroy() + { + super.destroy(); + } +} + +typedef ValorCarta = +{ + var palo:String; + var numero:String; +} diff --git a/source/objects/Player.hx b/source/objects/Player.hx new file mode 100644 index 0000000..5f24ff8 --- /dev/null +++ b/source/objects/Player.hx @@ -0,0 +1,76 @@ +package objects; + +class Player +{ + public var ID(get, null):String; + public var mano(get, set):Array; + public var canPlay(get, set):Bool; + public var points(get, default):Int; + public var hasBlackjack(get, null):Bool; + + var _canPlay:Bool = true; + var _mano:Array; + + public function new(ID:String) + { + this.ID = ID; + _mano = new Array(); + } + + function get_ID():String + { + return ID; + } + + function get_mano():Array + { + return _mano; + } + + function set_mano(value:Array):Array + { + return _mano = value; + } + + function get_canPlay():Bool + { + return _canPlay; + } + + function set_canPlay(value:Bool):Bool + { + return _canPlay = value; + } + + function get_points():Int + { + if (hasBlackjack) + return 21; + var sum = 0; + for (i in _mano) + { + if (i != null) + sum += i.points; + } + return sum; + } + + public function setDefault() + { + _canPlay = true; + } + + function isBlackJack(arr:Array) + { + var carta1 = arr[0]; + var carta2 = arr[1]; + if ((carta1.isAs && carta2.points == 10) || (carta2.isAs && carta1.points == 10)) + return true; + return false; + } + + function get_hasBlackjack():Bool + { + return isBlackJack(_mano); + } +} diff --git a/source/utils/ArrayUtil.hx b/source/utils/ArrayUtil.hx new file mode 100644 index 0000000..141e892 --- /dev/null +++ b/source/utils/ArrayUtil.hx @@ -0,0 +1,40 @@ +package utils; + +class ArrayUtil +{ + public static inline function last(arr:Array) + { + return arr[arr.length - 1]; + } + + public static inline function lastElement(arr:Array) + { + return arr.indexOf(last(arr)); + } + + public static function compareIntInArray(arr:Array, value:Int) + { + trace(arr); + for (i in arr) + { + if (i > value) + return true; + } + return false; + } + + public static function arrayDuplicates(arr:Array) + { + for (i in 0...arr.length) + { + for (j in (i + 1)...arr.length) + { + if (arr[i] == arr[j]) + { + return true; + } + } + } + return false; + } +}