University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MinOppgave1.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. //A 2D vector, with an x and a y property
  5. class Vec2 {
  6. public int x;
  7. public int y;
  8. public Vec2(int vx, int vy) {
  9. x = vx;
  10. y = vy;
  11. }
  12. }
  13. //A ball, with a velocity and position
  14. class Ball {
  15. public Vec2 pos;
  16. public Vec2 vel;
  17. public final String img = "0";
  18. public Ball(int x, int y, int vx, int vy) {
  19. pos = new Vec2(x, y);
  20. vel = new Vec2(vx, vy);
  21. }
  22. //Update the ball's position, according to its velocicty
  23. public void update() {
  24. pos.x += vel.x;
  25. pos.y += vel.y;
  26. }
  27. }
  28. //An arena full of balls
  29. class Arena {
  30. private Vec2 dimensions;
  31. private ArrayList<Ball> balls;
  32. private final String ANSI_CLS = "\u001b[2J";
  33. private final String ANSI_HOME = "\u001b[H";
  34. public Arena(int w, int h) {
  35. dimensions = new Vec2(w, h);
  36. balls = new ArrayList<Ball>();
  37. }
  38. public void addBall(Ball b) {
  39. balls.add(b);
  40. }
  41. //Update all balls, reversing their velocity if they collide with walls
  42. public void update() {
  43. for (Ball b: balls) {
  44. if (b.pos.x + b.vel.x <= 0 || b.pos.x + b.vel.x >= dimensions.x) {
  45. b.vel.x *= -1;
  46. }
  47. if (b.pos.y + b.vel.y < 0 || b.pos.y + b.vel.y >= dimensions.y) {
  48. b.vel.y *= -1;
  49. }
  50. b.update();
  51. }
  52. }
  53. public void draw() {
  54. //Buffer to avoid drawing to the screen a billion times
  55. String str = "";
  56. //Draw top part of frame
  57. str += " ";
  58. for (int i = 0; i < dimensions.x; ++i) {
  59. str += "-";
  60. }
  61. str += "\n";
  62. //Draw all balls
  63. for (int y = 0; y < dimensions.y; ++y) {
  64. str += "|";
  65. for (int x = 0; x < dimensions.x; ++x) {
  66. //Draw a ball if there's a ball in this square
  67. boolean isBall = false;
  68. for (Ball b: balls) {
  69. if (b.pos.x == x && b.pos.y == y) {
  70. isBall = true;
  71. str += b.img;
  72. break;
  73. }
  74. }
  75. //Draw blank if there's no ball here
  76. if (!isBall)
  77. str += " ";
  78. }
  79. str += "|\n";
  80. }
  81. //Draw bottom part of frame
  82. str += " ";
  83. for (int i = 0; i < dimensions.x; ++i) {
  84. str += "-";
  85. }
  86. //Clear the screen and print
  87. System.out.print(ANSI_CLS + ANSI_HOME + str);
  88. }
  89. }
  90. class MinOppgave1 {
  91. private static Arena arena;
  92. private static final Random rand = new Random();
  93. //Random int in range
  94. private static int randInt(int min, int max) {
  95. return rand.nextInt((max - min) + 1) + min;
  96. }
  97. //Overloading randInt to let us exclude a number if we want
  98. private static int randInt(int min, int max, int exclude) {
  99. int r;
  100. do {
  101. r = rand.nextInt((max - min) + 1) + min;
  102. } while (r == exclude);
  103. return r;
  104. }
  105. //We generally don't want user input to be 0 or negative
  106. private static int getPositiveInt(Scanner s) {
  107. int i = s.nextInt();
  108. if (i <= 0) {
  109. System.out.println("Please only input positive integers.");
  110. System.exit(1);
  111. }
  112. return i;
  113. }
  114. public static void main(String[] args) {
  115. Scanner s = new Scanner(System.in);
  116. //Get arena width, arena height, and the amount of balls from the user
  117. System.out.println("Insert arena width");
  118. int width = getPositiveInt(s);
  119. System.out.println("Insert arena height");
  120. int height = getPositiveInt(s);
  121. System.out.println("How many balls do you want?");
  122. int nBalls = getPositiveInt(s);
  123. arena = new Arena(width, height);
  124. //Add balls to the arena, with a random position and velocity
  125. for (int i = 0; i < nBalls; ++i) {
  126. arena.addBall(new Ball(
  127. randInt(0, width), //X
  128. randInt(0, height), //Y
  129. randInt(-4, 4, 0), //Velocity X
  130. randInt(-2, 2, 0) //Velocity Y
  131. ));
  132. }
  133. //Update the arena (moving the balls, etc), draw the result,
  134. //and sleep for 33 milliseconds to get an animation at about 30 FPS
  135. while (true) {
  136. arena.update();
  137. arena.draw();
  138. try {
  139. Thread.sleep(33);
  140. } catch (Exception e) {
  141. System.out.println("Failed to sleep.");
  142. System.exit(1);
  143. }
  144. }
  145. }
  146. }