-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3939 from KontrolDev/main
Reto #25 - Swift
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
Retos/Reto #25 - EL CÓDIGO KONAMI [Media]/swift/kontroldev.swift
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
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) | ||
} | ||
} | ||
|