Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- new Main().run();
- }
- public void run() {
- Scanner myScanner = new Scanner(System.in);
- System.out.print("Enter the number of participating horses: ");
- int nrOfHorses = myScanner.nextInt();
- System.out.print("Enter the length of the field: ");
- int fieldLength = myScanner.nextInt();
- int[] horsePositions = new int[nrOfHorses];
- int round = 1;
- System.out.println("All horses are ready to go!");
- for (int i = 0; i < nrOfHorses; i++) {
- drawBoard(fieldLength, horsePositions[i], 0);
- }
- while (!horseHasFinished(horsePositions, fieldLength)) {
- System.out.println("Round: " + round);
- for (int i = 0; i < nrOfHorses; i++) {
- int horseSpeed = (int) (Math.random() * 3) + 2;
- horsePositions[i] = horsePositions[i] + horseSpeed;
- drawBoard(fieldLength, horsePositions[i], horseSpeed);
- }
- round++;
- }
- System.out.println("The final result is in!");
- for (int i = 0; i < nrOfHorses; i++) {
- drawBoard(fieldLength + 5, horsePositions[i], 0);
- }
- System.out.println();
- for (int i = 0; i < nrOfHorses; i++) {
- if (horsePositions[i] > fieldLength) {
- System.out.println("Horse " + (i + 1) + " has won!");
- }
- }
- }
- public void drawBoard(int fieldLength, int positionHorse, int speed) {
- System.out.print("[");
- for (int i = 0; i < fieldLength; i++) {
- if (i == positionHorse) {
- System.out.print("H");
- } else {
- System.out.print(".");
- }
- }
- System.out.println("] {+ " + speed + "}");
- }
- public boolean horseHasFinished(int[] positions, int fieldLenght) {
- for (int i = 0; i < positions.length; i++) {
- if (positions[i] > fieldLenght) {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement