import java.util.ArrayList; import java.util.Scanner; import java.util.Random; //A 2D vector, with an x and a y property class Vec2 { public int x; public int y; public Vec2(int vx, int vy) { x = vx; y = vy; } } //A ball, with a velocity and position class Ball { public Vec2 pos; public Vec2 vel; public final String img = "0"; public Ball(int x, int y, int vx, int vy) { pos = new Vec2(x, y); vel = new Vec2(vx, vy); } //Update the ball's position, according to its velocicty public void update() { pos.x += vel.x; pos.y += vel.y; } } //An arena full of balls class Arena { private Vec2 dimensions; private ArrayList balls; private final String ANSI_CLS = "\u001b[2J"; private final String ANSI_HOME = "\u001b[H"; public Arena(int w, int h) { dimensions = new Vec2(w, h); balls = new ArrayList(); } public void addBall(Ball b) { balls.add(b); } //Update all balls, reversing their velocity if they collide with walls public void update() { for (Ball b: balls) { if (b.pos.x + b.vel.x <= 0 || b.pos.x + b.vel.x >= dimensions.x) { b.vel.x *= -1; } if (b.pos.y + b.vel.y < 0 || b.pos.y + b.vel.y >= dimensions.y) { b.vel.y *= -1; } b.update(); } } public void draw() { //Buffer to avoid drawing to the screen a billion times String str = ""; //Draw top part of frame str += " "; for (int i = 0; i < dimensions.x; ++i) { str += "-"; } str += "\n"; //Draw all balls for (int y = 0; y < dimensions.y; ++y) { str += "|"; for (int x = 0; x < dimensions.x; ++x) { //Draw a ball if there's a ball in this square boolean isBall = false; for (Ball b: balls) { if (b.pos.x == x && b.pos.y == y) { isBall = true; str += b.img; break; } } //Draw blank if there's no ball here if (!isBall) str += " "; } str += "|\n"; } //Draw bottom part of frame str += " "; for (int i = 0; i < dimensions.x; ++i) { str += "-"; } //Clear the screen and print System.out.print(ANSI_CLS + ANSI_HOME + str); } } class MinOppgave1 { private static Arena arena; private static final Random rand = new Random(); //Random int in range private static int randInt(int min, int max) { return rand.nextInt((max - min) + 1) + min; } //Overloading randInt to let us exclude a number if we want private static int randInt(int min, int max, int exclude) { int r; do { r = rand.nextInt((max - min) + 1) + min; } while (r == exclude); return r; } //We generally don't want user input to be 0 or negative private static int getPositiveInt(Scanner s) { int i = s.nextInt(); if (i <= 0) { System.out.println("Please only input positive integers."); System.exit(1); } return i; } public static void main(String[] args) { Scanner s = new Scanner(System.in); //Get arena width, arena height, and the amount of balls from the user System.out.println("Insert arena width"); int width = getPositiveInt(s); System.out.println("Insert arena height"); int height = getPositiveInt(s); System.out.println("How many balls do you want?"); int nBalls = getPositiveInt(s); arena = new Arena(width, height); //Add balls to the arena, with a random position and velocity for (int i = 0; i < nBalls; ++i) { arena.addBall(new Ball( randInt(0, width), //X randInt(0, height), //Y randInt(-4, 4, 0), //Velocity X randInt(-2, 2, 0) //Velocity Y )); } //Update the arena (moving the balls, etc), draw the result, //and sleep for 33 milliseconds to get an animation at about 30 FPS while (true) { arena.update(); arena.draw(); try { Thread.sleep(33); } catch (Exception e) { System.out.println("Failed to sleep."); System.exit(1); } } } }