Advertisement
STANAANDREY

except lab

Nov 27th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Date;
  3.  
  4. class CoordinateGenerator {
  5.     private Random randomGenerator;
  6.     public CoordinateGenerator() {
  7.         Date now = new Date();
  8.         long sec = now.getTime();
  9.         randomGenerator = new Random(sec);
  10.     }
  11.     public int generateX() {
  12.         int x = randomGenerator.nextInt(101);
  13.         if(x < 5) {
  14.             x = 0;
  15.         } else if(x > 95) {
  16.             x = 100;} else {
  17.             x = randomGenerator.nextInt(99) + 1;
  18.         }
  19.         return x;
  20.     }
  21.     public int generateY() {
  22.         int y = randomGenerator.nextInt(101);
  23.         if(y < 5) {
  24.             y = 0;
  25.         } else if(y > 95) {
  26.             y = 50;
  27.         } else {
  28.             y = randomGenerator.nextInt(49) + 1;
  29.         }
  30.         return y;
  31.     }
  32. }
  33.  
  34. class EOut extends Exception {}
  35. class EGoal extends Exception {}
  36. class ECorner extends Exception {}
  37.  
  38. class Team {
  39.     private String name;
  40.     private int goals;
  41.  
  42.     public Team(String name) {
  43.         this.name = name;
  44.     }
  45.  
  46.     @Override
  47.     public String toString() {
  48.         return "Team{" +
  49.                 "name='" + name + '\'' +
  50.                 ", goals=" + goals +
  51.                 '}';
  52.     }
  53.  
  54.     public void markGoal() {
  55.         goals++;
  56.     }
  57.  
  58.     public String getName() {
  59.         return name;
  60.     }
  61. }
  62.  
  63. class Ball {
  64.     private int x, y;
  65.  
  66.     public Ball(int x, int y) {
  67.         this.x = x;
  68.         this.y = y;
  69.     }
  70.  
  71.     public int getX() {
  72.         return x;
  73.     }
  74.  
  75.     public int getY() {
  76.         return y;
  77.     }
  78.  
  79.     void shot() throws EOut, EGoal, ECorner {
  80.         var coordinateGenerator = new CoordinateGenerator();
  81.         x = coordinateGenerator.generateX();
  82.         y = coordinateGenerator.generateY();
  83.         if (y == 0 || y == Game.HEIGHT) {
  84.             throw new EOut();
  85.         }
  86.         if ((x == 0 || x == Game.WIDTH) && Game.GATE_START <= y && y <= Game.GATE_END) {
  87.             throw new EGoal();
  88.         }
  89.         if (
  90.                 (x == 0 || x == Game.WIDTH)
  91.                 && (Game.GATE_START > y && y < Game.GATE_END)
  92.         ) {
  93.             throw new ECorner();
  94.         }
  95.     }
  96. }
  97.  
  98. class Game {
  99.     private Team[] teams;
  100.     private int outs, corners;
  101.     public static final int WIDTH = 100, HEIGHT = 50, GATE_START = 20, GATE_END = 30;
  102.  
  103.     public Game(String name1, String name2) {
  104.         teams = new Team[2];
  105.         teams[0] = new Team(name1);
  106.         teams[1] = new Team(name2);
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         return "Game{" +
  112.                 "t1=" + teams[0] +
  113.                 ", t2=" + teams[1] +
  114.                 ", outs=" + outs +
  115.                 ", corners=" + corners +
  116.                 '}';
  117.     }
  118.  
  119.     private void printData(final Ball ball) {
  120.         System.out.println(teams[0].getName() + " - " + teams[1].getName() + ": ("
  121.             + ball.getX() + ", " + ball.getY() + ")"
  122.         );
  123.     }
  124.  
  125.     void simulate() {
  126.         var ball = new Ball(Game.WIDTH / 2, Game.HEIGHT / 2);
  127.         printData(ball);
  128.         for (int i = 0; i < 1000; i++) {
  129.             try {
  130.                 ball.shot();
  131.             } catch (EGoal eGoal) {
  132.                 if (ball.getX() == 0) {
  133.                     teams[0].markGoal();
  134.                 } else {
  135.                     teams[1].markGoal();
  136.                 }
  137.                 ball = new Ball(Game.WIDTH / 2, Game.HEIGHT / 2);
  138.             } catch (ECorner eCorner) {
  139.                 int x = ball.getX(), y = ball.getY();
  140.                 if (x == 0) {
  141.                     if (y > GATE_END) {
  142.                         ball = new Ball(0, HEIGHT);
  143.                     } else {
  144.                         ball = new Ball(0, 0);
  145.                     }
  146.                 } else {
  147.                     if (y > GATE_END) {
  148.                         ball = new Ball(WIDTH, HEIGHT);
  149.                     } else {
  150.                         ball = new Ball(WIDTH, 0);
  151.                     }
  152.                 }
  153.                 corners++;
  154.             } catch (EOut eOut) {
  155.                 ball = new Ball(ball.getX(), ball.getY());
  156.                 outs++;
  157.             }
  158.             printData(ball);
  159.         }
  160.     }
  161. }
  162.  
  163. public class Main {
  164.  
  165.     public static void main(String[] args) {
  166.         Game game = new Game("team1", "team2");
  167.         game.simulate();
  168.         System.out.println(game);
  169.         game = new Game("teamA", "teamB");
  170.         game.simulate();
  171.         System.out.println(game);
  172.     }
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement