Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Date;
- class CoordinateGenerator {
- private Random randomGenerator;
- public CoordinateGenerator() {
- Date now = new Date();
- long sec = now.getTime();
- randomGenerator = new Random(sec);
- }
- public int generateX() {
- int x = randomGenerator.nextInt(101);
- if(x < 5) {
- x = 0;
- } else if(x > 95) {
- x = 100;} else {
- x = randomGenerator.nextInt(99) + 1;
- }
- return x;
- }
- public int generateY() {
- int y = randomGenerator.nextInt(101);
- if(y < 5) {
- y = 0;
- } else if(y > 95) {
- y = 50;
- } else {
- y = randomGenerator.nextInt(49) + 1;
- }
- return y;
- }
- }
- class EOut extends Exception {}
- class EGoal extends Exception {}
- class ECorner extends Exception {}
- class Team {
- private String name;
- private int goals;
- public Team(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Team{" +
- "name='" + name + '\'' +
- ", goals=" + goals +
- '}';
- }
- public void markGoal() {
- goals++;
- }
- public String getName() {
- return name;
- }
- }
- class Ball {
- private int x, y;
- public Ball(int x, int y) {
- this.x = x;
- this.y = y;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- void shot() throws EOut, EGoal, ECorner {
- var coordinateGenerator = new CoordinateGenerator();
- x = coordinateGenerator.generateX();
- y = coordinateGenerator.generateY();
- if (y == 0 || y == Game.HEIGHT) {
- throw new EOut();
- }
- if ((x == 0 || x == Game.WIDTH) && Game.GATE_START <= y && y <= Game.GATE_END) {
- throw new EGoal();
- }
- if (
- (x == 0 || x == Game.WIDTH)
- && (Game.GATE_START > y && y < Game.GATE_END)
- ) {
- throw new ECorner();
- }
- }
- }
- class Game {
- private Team[] teams;
- private int outs, corners;
- public static final int WIDTH = 100, HEIGHT = 50, GATE_START = 20, GATE_END = 30;
- public Game(String name1, String name2) {
- teams = new Team[2];
- teams[0] = new Team(name1);
- teams[1] = new Team(name2);
- }
- @Override
- public String toString() {
- return "Game{" +
- "t1=" + teams[0] +
- ", t2=" + teams[1] +
- ", outs=" + outs +
- ", corners=" + corners +
- '}';
- }
- private void printData(final Ball ball) {
- System.out.println(teams[0].getName() + " - " + teams[1].getName() + ": ("
- + ball.getX() + ", " + ball.getY() + ")"
- );
- }
- void simulate() {
- var ball = new Ball(Game.WIDTH / 2, Game.HEIGHT / 2);
- printData(ball);
- for (int i = 0; i < 1000; i++) {
- try {
- ball.shot();
- } catch (EGoal eGoal) {
- if (ball.getX() == 0) {
- teams[0].markGoal();
- } else {
- teams[1].markGoal();
- }
- ball = new Ball(Game.WIDTH / 2, Game.HEIGHT / 2);
- } catch (ECorner eCorner) {
- int x = ball.getX(), y = ball.getY();
- if (x == 0) {
- if (y > GATE_END) {
- ball = new Ball(0, HEIGHT);
- } else {
- ball = new Ball(0, 0);
- }
- } else {
- if (y > GATE_END) {
- ball = new Ball(WIDTH, HEIGHT);
- } else {
- ball = new Ball(WIDTH, 0);
- }
- }
- corners++;
- } catch (EOut eOut) {
- ball = new Ball(ball.getX(), ball.getY());
- outs++;
- }
- printData(ball);
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Game game = new Game("team1", "team2");
- game.simulate();
- System.out.println(game);
- game = new Game("teamA", "teamB");
- game.simulate();
- System.out.println(game);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement