Skip to content

Commit c1f491c

Browse files
authored
Merge pull request #7620 from raulG91/main
#32 - Javascript
2 parents 52b261e + 8d1e65c commit c1f491c

File tree

2 files changed

+193
-0
lines changed
  • Roadmap
    • 32 - BATALLA DEADPOOL Y WOLVERINE/javascript
    • 49 - EL ALMACÉN DE PAPÁ NOEL/javascript

2 files changed

+193
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
const prompt = require('prompt-sync')();
3+
4+
function getRandomNumber(min,max){
5+
6+
const minCeiled = Math.ceil(min);
7+
const maxFloored = Math.floor(max);
8+
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled);
9+
}
10+
11+
let deadpool_life = prompt('Introduce vida maxima para Deadpool ')
12+
if(deadpool_life < 0) {
13+
14+
}
15+
let wolverine_life = prompt('Introduce vida maxima para wolverine ')
16+
17+
if(deadpool_life > 0 && wolverine_life > 0){
18+
let turn = 1;
19+
let skip_turn = false;
20+
while (deadpool_life > 0 && wolverine_life > 0) {
21+
console.log(`Turno: ${turn}`)
22+
//Determine who is attacking this turn
23+
24+
if (turn % 2 != 0){
25+
//Attack Deadpool
26+
if(skip_turn){
27+
console.log("Deadpool no atacara en este turno")
28+
skip_turn = false
29+
}
30+
else{
31+
let deadpool_attack = getRandomNumber(10,100);
32+
//Let's see if Wolverine avoid attack
33+
console.log("Deadpool atacara con: " + String(deadpool_attack))
34+
let skip = getRandomNumber(0,100);
35+
if(skip <= 20){
36+
console.log("Wolverine esquiva el ataque de Deadpool")
37+
}
38+
else{
39+
// Check if the damage is the max attack
40+
if(deadpool_attack == 100){
41+
skip_turn = true
42+
console.log("Ataque maximo de Deadpool!")
43+
}
44+
wolverine_life -= deadpool_attack;
45+
46+
}
47+
}
48+
49+
}
50+
else{
51+
if(skip_turn){
52+
console.log("Wolverine no atacara en este turno")
53+
skip_turn = false;
54+
}
55+
else{
56+
let wolverine_attack = getRandomNumber(10,120)
57+
console.log(`Wolverine atacara con ${wolverine_attack}`)
58+
let skip = getRandomNumber(0,100);
59+
if(skip <= 25){
60+
console.log("Deadpool esquiva el ataque de Wolverine")
61+
}
62+
else{
63+
if(wolverine_attack == 120){
64+
skip_turn = true;
65+
console.log("Ataque maximo de Wolverine")
66+
}
67+
deadpool_life -= wolverine_attack;
68+
}
69+
}
70+
}
71+
if(deadpool_life> 0 && wolverine_life > 0){
72+
console.log(`Vida de Deadpool tras turno ${turn} es ${deadpool_life}`)
73+
console.log(`Vida de Wolverine tras turno ${turn} es ${wolverine_life}`)
74+
turn ++;
75+
}
76+
else{
77+
// One character has died
78+
if(turn % 2 == 0){
79+
console.log("Wolverine ha ganado el combate")
80+
}
81+
else{
82+
console.log("Deadpool ha ganado el combate")
83+
}
84+
}
85+
}
86+
}
87+
else{
88+
console.log("Vida maxima debe ser mayor que 0")
89+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const prompt = require('prompt-sync')();
2+
3+
const CHAR_SET = ['A', 'B', 'C', "1", "2", "3"];
4+
5+
function getRandomNumber(min, max) {
6+
7+
const minCeiled = Math.ceil(min);
8+
const maxFloored = Math.floor(max);
9+
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled);
10+
}
11+
12+
function generateCode() {
13+
14+
let char_set_string = CHAR_SET.join("")
15+
let i = 0;
16+
let final_code = ""
17+
18+
while (i < 4) {
19+
let pos = getRandomNumber(0, char_set_string.length - 1)
20+
final_code += char_set_string.charAt(pos)
21+
char_set_string = char_set_string.slice(0, pos) + char_set_string.slice(pos + 1)
22+
i++
23+
}
24+
return final_code
25+
}
26+
27+
function check_input(input) {
28+
29+
let correct = true
30+
31+
//First check if the length of the input is 4
32+
if (input.length != 4) {
33+
console.log("El codigo debe tener una longitud de 4 caracteres")
34+
correct = false
35+
}
36+
else {
37+
//Check if the input contains non allowed characters
38+
for (let char of input) {
39+
if (!(CHAR_SET.includes(char))) {
40+
console.log(`Caracter ${char} no esta permitido`)
41+
correct = false
42+
break
43+
44+
}
45+
else {
46+
//Count if the char appears more than 1
47+
let count = (input.match(new RegExp(char, "g")) || []).length;
48+
if (count > 1) {
49+
console.log(`Caracter ${char} esta duplicado`)
50+
correct = false
51+
break
52+
53+
}
54+
}
55+
}
56+
}
57+
return correct
58+
}
59+
60+
function main() {
61+
let code = generateCode()
62+
let tries = 0;
63+
let exit = false
64+
65+
console.log(code)
66+
while (tries < 10 && !exit) {
67+
console.log(`Numero de intentos restantes ${10 - tries}`)
68+
//User imput
69+
let input = prompt('Introduce codigo de entrada ')
70+
//If length is different to 4 throw an error
71+
if (check_input(input)) {
72+
// Check if the code entered by the user is the one that we are looking for
73+
if (input === code) {
74+
exit = true
75+
console.log("El codigo es correcto ")
76+
}
77+
else {
78+
// Checks how many characters the user input correctly
79+
for (let i = 0; i < input.length; i++) {
80+
if (input.at(i) == code.at(i)) {
81+
//Char is in the same position
82+
console.log(`Caracter ${input.at(i)} es Correcto`)
83+
}
84+
else {
85+
let current_char = input.at(i)
86+
if (code.includes(current_char)) {
87+
//Char exist in the code but the position is not correct
88+
console.log(`Caracter ${current_char} esta presente`)
89+
}
90+
else {
91+
console.log(`Caracter ${current_char} es Incorrecto`)
92+
}
93+
}
94+
}
95+
96+
}
97+
}
98+
99+
tries++
100+
101+
}
102+
}
103+
104+
main()

0 commit comments

Comments
 (0)