Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. new Main().run();
  7. }
  8.  
  9. public void run() {
  10. Scanner myScanner = new Scanner(System.in);
  11. System.out.print("Enter the number of participating horses: ");
  12. int nrOfHorses = myScanner.nextInt();
  13. System.out.print("Enter the length of the field: ");
  14. int fieldLength = myScanner.nextInt();
  15. int[] horsePositions = new int[nrOfHorses];
  16. int round = 1;
  17.  
  18. System.out.println("All horses are ready to go!");
  19. for (int i = 0; i < nrOfHorses; i++) {
  20. drawBoard(fieldLength, horsePositions[i], 0);
  21. }
  22.  
  23. while (!horseHasFinished(horsePositions, fieldLength)) {
  24. System.out.println("Round: " + round);
  25. for (int i = 0; i < nrOfHorses; i++) {
  26. int horseSpeed = (int) (Math.random() * 3) + 2;
  27. horsePositions[i] = horsePositions[i] + horseSpeed;
  28. drawBoard(fieldLength, horsePositions[i], horseSpeed);
  29. }
  30. round++;
  31. }
  32. System.out.println("The final result is in!");
  33. for (int i = 0; i < nrOfHorses; i++) {
  34. drawBoard(fieldLength + 5, horsePositions[i], 0);
  35. }
  36. System.out.println();
  37. for (int i = 0; i < nrOfHorses; i++) {
  38. if (horsePositions[i] > fieldLength) {
  39. System.out.println("Horse " + (i + 1) + " has won!");
  40. }
  41. }
  42.  
  43. }
  44.  
  45. public void drawBoard(int fieldLength, int positionHorse, int speed) {
  46. System.out.print("[");
  47. for (int i = 0; i < fieldLength; i++) {
  48. if (i == positionHorse) {
  49. System.out.print("H");
  50. } else {
  51. System.out.print(".");
  52. }
  53. }
  54. System.out.println("] {+ " + speed + "}");
  55. }
  56.  
  57. public boolean horseHasFinished(int[] positions, int fieldLenght) {
  58. for (int i = 0; i < positions.length; i++) {
  59. if (positions[i] > fieldLenght) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement