Skip to content

Commit

Permalink
Merge pull request #6003 from Qv1ko/main
Browse files Browse the repository at this point in the history
Reto #44 - Java
  • Loading branch information
kontroldev authored Dec 8, 2023
2 parents 8d71302 + f75084c commit a1d1c6a
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Retos/Reto #44 - ADIVINANZAS MATEMÁTICAS [Media]/java/Qv1ko.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Qv1ko {

static boolean end = false;

public static void main(String[] args) {
riddle();
}

private static void riddle() {

int x = 1, y = 1, ope1 = 0, ope2 = 0, operation = 0, correct = 0;
String[] operators = new String[] {"+", "-", "*", "/"};
double result = 0;
String answer = "";

while (!end) {

ope1 = (int)(Math.random() * (int)Math.pow(10, x));
ope2 = (int)(Math.random() * (int)Math.pow(10, y));

operation = (int)(Math.random() * 4);

if (operation == 0) {
result = ope1 + ope2;
} else if (operation == 1) {
result = ope1 - ope2;
} else if (operation == 2) {
result = ope1 * ope2;
} else if (operation == 3) {

while (ope2 == 0) {
ope2 = (int)(Math.random() * (int)Math.pow(10, y));
}

result = ope1 / ope2;

}

answer = question(ope1, ope2, operators[operation]);

if (end) {
break;
} else if (Double.parseDouble(answer) == result) {

correct++;

if (correct % 5 == 0) {
if (x > y) {
y++;
} else {
x++;
}
}

} else {
end = true;
}

System.out.println(ope1 + " " + operators[operation] + " " + ope2 + " = " + result);

}

System.out.println("You got " + correct + " questions correct");

}

private static String question(int num1, int num2, String operation) {

Scanner sc = new Scanner(System.in);
String answer = "";

Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("\nThe time is over");
end = true;
}
}, 3000);

try {
System.out.print(num1 + " " + operation + " " + num2 + " = ");
answer = sc.nextLine();
} finally {
timer.cancel();
}

return answer;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class Qv1ko {

public static void main(String[] args) {
calendar();
}

private static void calendar() {

ArrayList<String> participants = new ArrayList<String>();
String participant = "";
int position = 0;
boolean on = true, exit = false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while (!exit) {

System.out.print("\n1) Add participant | 2) Show participants | 3) Delete participant | 4) Carry out the draw | 5) Exit\nSelect one option: ");

try {
switch(br.readLine()) {

case "1":

System.out.println("\nAdd participant\n");

System.out.print("Name: ");
participant = br.readLine();

for (String p : participants) {
if (participant.equalsIgnoreCase(p)) {
System.out.println("\nThe participant already exists\n");
on = false;
break;
}
}

if (on) {
participants.add(participant);
}

break;

case "2":

System.out.println("\nList of participants\n");

for (int i = 0; i < participants.size(); i++) {
System.out.println("- " + participants.get(i));
}

break;

case "3":

if (participants.size() > 0) {

System.out.println("\nDelete participant\n");

System.out.print("Name: ");
participant = br.readLine();

for (int i = 0; i < participants.size(); i++) {
if (participant.equalsIgnoreCase(participants.get(i))) {
participants.remove(i);
on = false;
break;
}
}

if (on) {
System.out.println("\nParticipant does not exist");
} else {
System.out.println("\nThe participant has been eliminated");
}

} else {
System.out.println("\nNo participants");
}

break;

case "4":

if (participants.size() > 0) {

position = (int)(Math.random() * participants.size());
System.out.println("\nThe winner is " + participants.get(position));

participants.remove(position);

} else {
System.out.println("\nNo participants");
}

break;

case "5":

System.out.println("\nLeaving...\n");
exit = true;
break;

default:
System.out.println("\nSelect a number");

}

on = true;

} catch (IOException exc) {
System.out.println("\nInput error\n");
}

}

}

}
125 changes: 125 additions & 0 deletions Retos/Reto #46 - LA CARRERA DE COCHES [Media]/java/Qv1ko.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
public class Qv1ko {

private static char END = '#';
private static char ROAD = '_';
private static char TREE = 'T';
private static char CAR = '<';
private static char SHOCK = 'x';

public static void main(String[] args) {
race();
}

private static void race() {

int turn = 1;
boolean shockCar1 = false, shockCar2 = false;

String track1 = trackGenerator();
String track2 = trackGenerator();

System.out.println("\nSTART\n");

System.out.println(track1);
System.out.println(track2);

while (track1.toCharArray()[0] != CAR && track2.toCharArray()[0] != CAR) {

try {
Thread.sleep(10000);
} catch (InterruptedException exc) {
exc.printStackTrace();
}

System.out.println("\nTurn " + turn + "\n");

if (!shockCar1) {

track1 = advance(track1, (int)(Math.random() * 3) + 1);

for (char element : track1.toCharArray()) {
if (element == SHOCK) {
shockCar1 = true;
break;
}
}

} else {
shockCar1 = false;
}

if (!shockCar2) {

track2 = advance(track2, (int)(Math.random() * 3) + 1);

for (char element : track2.toCharArray()) {
if (element == SHOCK) {
shockCar2 = true;
break;
}
}

} else {
shockCar2 = false;
}

System.out.println(track1);
System.out.println(track2);

turn++;

}

if (track1.toCharArray()[0] == track2.toCharArray()[0]) {
System.out.println("\nThe race ended in a tie");
} else {
System.out.println("\nThe race winner is " + ((track1.toCharArray()[0] == CAR) ? "car 1" : "car 2"));
}

}

private static String trackGenerator() {

String road = "";
int trees = 0;

for (int i = 0; i < 10; i++) {
if (trees < 3 && (int)(Math.random() * 10) < 3) {
road += TREE;
trees++;
} else {
road += ROAD;
}
}

return END + road + CAR;

}

private static String advance(String track, int positions) {

String clearTrack = "", newTrack = "";
int newPosition = 0;

for (int i = 0; i < track.length(); i++) {
if (track.toCharArray()[i] == CAR || track.toCharArray()[i] == SHOCK) {
newPosition = (i - positions < 0) ? 0 : i - positions;
clearTrack += '_';
} else {
clearTrack += track.toCharArray()[i];
}
}

for (int i = 0; i < clearTrack.length(); i++) {
if (i == newPosition) {
newTrack += (clearTrack.toCharArray()[newPosition] == TREE) ? SHOCK : CAR;
} else {
newTrack += clearTrack.toCharArray()[i];
}
}

return newTrack;

}

}
37 changes: 37 additions & 0 deletions Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [Fácil]/java/Qv1ko.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.HashMap;
import java.util.Scanner;

class Qv1ko {

public static void main(String[] args) {
wordPoints();
}

private static void wordPoints() {

HashMap<Character, Integer> letterPoints = new HashMap<Character, Integer>() {{ put('a', 1);put('b', 2); put('c', 3); put('d', 4); put('e', 5); put('f', 6); put('g', 7); put('h', 8); put('i', 9); put('j', 10); put('k', 11); put('l', 12); put('m', 13); put('n', 14); put('ñ', 15); put('o', 16); put('p', 17); put('q', 18); put('r', 19); put('s', 20); put('t', 21); put('u', 22); put('v', 23); put('w', 24); put('x', 25); put('y', 26); put('z', 27); }};
String word = "";
int points = 0;
Scanner sc = new Scanner(System.in);

while (points != 100) {

points = 0;

System.out.print("Type a word: ");
word = sc.nextLine().toLowerCase();

for (char letter : word.toCharArray()) {
points += letterPoints.get(letter);
}

System.out.println("The word " + word + " has " + points + " points");

}


sc.close();

}

}

0 comments on commit a1d1c6a

Please sign in to comment.