Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #25 - Swift #3939

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Retos/Reto #25 - EL CÓDIGO KONAMI [Media]/swift/kontroldev.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Crea un programa que detecte cuando el famoso "Código Konami" se ha introducido correctamente
* desde el teclado. Si sucede esto, debe notificarse mostrando un mensaje en la terminal.
*/

import Foundation

// Secuencia del código Konami
let konamiCode: [String] = ["⬆️", "⬆️", "⬇️", "⬇️", "⬅️", "➡️", "⬅️", "➡️", "B", "A"]

// Variables para realizar el seguimiento del código ingresado
var currentInput: [String] = []
var currentIndex = 0

// Función para verificar si se ha ingresado el código completo
func checkKonamiCode() {
if currentInput == konamiCode {
print("¡Código Konami introducido correctamente!")
} else {
print("Código incorrecto. Inténtalo de nuevo.")
currentInput.removeAll()
currentIndex = 0
}
}

// Función para procesar las teclas ingresadas por el usuario
func processKey(_ key: String) {
if key == konamiCode[currentIndex] {
currentInput.append(key)
currentIndex += 1

if currentIndex == konamiCode.count {
checkKonamiCode()
}
} else {
print("Código incorrecto. Inténtalo de nuevo.")
currentInput.removeAll()
currentIndex = 0
}
}

// Iniciar la lectura de teclas
print("Introduce el Código Konami:")

while currentIndex < konamiCode.count {
if let key = readLine(strippingNewline: true) {
processKey(key)
}
}