Skip to content

Commit 68cb4dd

Browse files
author
Miguel Ángel Delgado
committed
1 parent c027c25 commit 68cb4dd

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.*;
2+
3+
public class miguelex {
4+
public static void main(String[] args) {
5+
List<Character> chars = Arrays.asList('A', 'B', 'C', '1', '2', '3');
6+
Collections.shuffle(chars);
7+
String code = chars.subList(0, 4).stream().map(String::valueOf).reduce("", String::concat);
8+
9+
Scanner scanner = new Scanner(System.in);
10+
int attempts = 10;
11+
12+
System.out.println("¡Bienvenido! Tienes 10 intentos para descifrar el código.");
13+
14+
while (attempts > 0) {
15+
System.out.println("Intento #" + attempts + ": Ingresa un código de 4 caracteres:");
16+
String input = scanner.nextLine().toUpperCase();
17+
18+
if (input.length() != 4 || !input.matches("[A-C1-3]{4}")) {
19+
System.out.println("Código inválido. Debe tener 4 caracteres (letras A-C, números 1-3).");
20+
continue;
21+
}
22+
23+
StringBuilder result = new StringBuilder();
24+
for (int i = 0; i < 4; i++) {
25+
if (input.charAt(i) == code.charAt(i)) {
26+
result.append("Correcto, ");
27+
} else if (code.indexOf(input.charAt(i)) != -1) {
28+
result.append("Presente, ");
29+
} else {
30+
result.append("Incorrecto, ");
31+
}
32+
}
33+
34+
System.out.println(result.substring(0, result.length() - 2));
35+
36+
if (input.equals(code)) {
37+
System.out.println("¡Felicidades! Descifraste el código: " + code);
38+
return;
39+
}
40+
41+
attempts--;
42+
}
43+
44+
System.out.println("Lo siento, no lograste descifrar el código: " + code);
45+
}
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const readline = require('readline');
2+
3+
function generateCode() {
4+
const chars = ['A', 'B', 'C', '1', '2', '3'];
5+
return chars.sort(() => Math.random() - 0.5).slice(0, 4).join('');
6+
}
7+
8+
const code = generateCode();
9+
let attempts = 10;
10+
11+
const rl = readline.createInterface({
12+
input: process.stdin,
13+
output: process.stdout,
14+
});
15+
16+
console.log("¡Bienvenido! Tienes 10 intentos para descifrar el código.");
17+
18+
function askForInput() {
19+
if (attempts === 0) {
20+
console.log(`Lo siento, no lograste descifrar el código: ${code}`);
21+
rl.close();
22+
return;
23+
}
24+
25+
rl.question(`Intento #${attempts}: Ingresa un código de 4 caracteres: `, (input) => {
26+
input = input.toUpperCase();
27+
28+
if (input.length !== 4 || !/^[A-C1-3]{4}$/.test(input)) {
29+
console.log("Código inválido. Debe tener 4 caracteres (letras A-C, números 1-3).");
30+
return askForInput();
31+
}
32+
33+
const result = input.split('').map((char, i) => {
34+
if (char === code[i]) return "Correcto";
35+
if (code.includes(char)) return "Presente";
36+
return "Incorrecto";
37+
});
38+
39+
console.log(result.join(", "));
40+
41+
if (input === code) {
42+
console.log(`¡Felicidades! Descifraste el código: ${code}`);
43+
rl.close();
44+
} else {
45+
attempts--;
46+
askForInput();
47+
}
48+
});
49+
}
50+
51+
askForInput();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
function generateCode() {
3+
$characters = array_merge(range('A', 'C'), range(1, 3));
4+
shuffle($characters);
5+
return implode('', array_slice($characters, 0, 4));
6+
}
7+
8+
$code = generateCode();
9+
$attempts = 10;
10+
11+
echo "¡Bienvenido! Tienes 10 intentos para descifrar el código.\n";
12+
13+
while ($attempts > 0) {
14+
echo "Intento #{$attempts}. Ingresa un código de 4 caracteres: ";
15+
$input = trim(fgets(STDIN));
16+
17+
if (strlen($input) != 4 || !preg_match('/^[A-C1-3]{4}$/', $input)) {
18+
echo "Código inválido. Debe tener 4 caracteres (letras A-C, números 1-3).\n";
19+
continue;
20+
}
21+
22+
$result = [];
23+
$used = array_fill(0, 4, false);
24+
25+
for ($i = 0; $i < 4; $i++) {
26+
if ($input[$i] === $code[$i]) {
27+
$result[] = "Correcto";
28+
$used[$i] = true;
29+
} elseif (strpos($code, $input[$i]) !== false && !$used[array_search($input[$i], str_split($code))]) {
30+
$result[] = "Presente";
31+
} else {
32+
$result[] = "Incorrecto";
33+
}
34+
}
35+
36+
echo implode(", ", $result) . "\n";
37+
38+
if ($input === $code) {
39+
echo "¡Felicidades! Descifraste el código: {$code}\n";
40+
exit;
41+
}
42+
43+
$attempts--;
44+
}
45+
46+
echo "Lo siento, no lograste descifrar el código: {$code}\n";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import random
2+
3+
def generate_code():
4+
return ''.join(random.sample('ABC123', 4))
5+
6+
code = generate_code()
7+
attempts = 10
8+
9+
print("¡Bienvenido! Tienes 10 intentos para descifrar el código.")
10+
11+
while attempts > 0:
12+
input_code = input(f"Intento #{attempts}. Ingresa un código de 4 caracteres: ").upper()
13+
14+
if len(input_code) != 4 or not all(c in 'ABC123' for c in input_code):
15+
print("Código inválido. Debe tener 4 caracteres (letras A-C, números 1-3).")
16+
continue
17+
18+
result = []
19+
for i, char in enumerate(input_code):
20+
if char == code[i]:
21+
result.append("Correcto")
22+
elif char in code:
23+
result.append("Presente")
24+
else:
25+
result.append("Incorrecto")
26+
27+
print(", ".join(result))
28+
29+
if input_code == code:
30+
print(f"¡Felicidades! Descifraste el código: {code}")
31+
break
32+
33+
attempts -= 1
34+
35+
if attempts == 0:
36+
print(f"Lo siento, no lograste descifrar el código: {code}")

0 commit comments

Comments
 (0)