Created
September 8, 2023 18:08
-
-
Save SebastianCastilloDev/83a24afeb2b8ecd614ea37b9da49331e to your computer and use it in GitHub Desktop.
Solución ejercicio maquina tragamonedas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Console.WriteLine("Maquina tragamonedas"); | |
Console.Write("Ingrese su nombre: "); | |
string? nombre = Console.ReadLine(); | |
bool existeNombre = false; | |
TragaMonedas tragaMonedas; | |
if (nombre != "") { | |
existeNombre = true; | |
} | |
if (existeNombre) { | |
tragaMonedas = new TragaMonedas(nombre); | |
} else { | |
tragaMonedas = new TragaMonedas(); | |
} | |
tragaMonedas.Bienvenida(); | |
do { | |
string menu = "Que deseas hacer?\n"; | |
menu += "1. Tirar la palanca\n"; | |
menu += "2. Consultar Saldo\n"; | |
menu += "3. Agregar Creditos\n"; | |
menu += "4. Retirarse"; | |
Console.WriteLine(menu); | |
Console.Write("Ingrese una opción: "); | |
string? opcion = Console.ReadLine(); | |
switch (opcion) { | |
case "1": | |
tragaMonedas.TirarLaPalanca(); | |
break; | |
case "2": | |
tragaMonedas.IndicarSaldo(); | |
break; | |
case "3": | |
tragaMonedas.CargarCreditos(); | |
break; | |
case "4": | |
return; | |
} | |
} while(true); | |
public class TragaMonedas { | |
public int credito; | |
public string nombreJugador; | |
public TragaMonedas() { | |
credito = 200; | |
nombreJugador = "Anónimo"; | |
} | |
public TragaMonedas(string _nombreJugador) { | |
credito = 200; | |
nombreJugador = _nombreJugador; | |
} | |
public void Bienvenida() { | |
Console.WriteLine("Bienvenido " + nombreJugador); | |
} | |
public void TirarLaPalanca(){ | |
int numero = GenerarNumeroAleatorio(); | |
int[] numerosDeLaSuerte = {1,2,3,4}; | |
if (credito > 0) { | |
ModificarSaldo(numerosDeLaSuerte.Contains(numero)); | |
} else { | |
credito = 0; | |
} | |
IndicarSaldo(); | |
} | |
public void ModificarSaldo(bool tirada){ | |
if(tirada) credito += 60; | |
else credito -= 50; | |
} | |
public void CargarCreditos(){ | |
credito += 200; | |
IndicarSaldo(); | |
} | |
public void IndicarSaldo(){ | |
string mensaje = "- - - - - - - - - - - - - -\n"; | |
mensaje += nombreJugador + " tu saldo final es " + credito + "\n"; | |
mensaje += "- - - - - - - - - - - - - -\n"; | |
Console.WriteLine(mensaje); | |
} | |
private int GenerarNumeroAleatorio(){ | |
Random random = new Random(); | |
return random.Next(1,20); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment