-
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.
- Loading branch information
Showing
1 changed file
with
125 additions
and
0 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
Retos/Reto #46 - LA CARRERA DE COCHES [Media]/java/Qv1ko.java
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,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; | ||
|
||
} | ||
|
||
} |