Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- class Out extends Exception
- {
- public Out()
- {
- super("Out");
- }
- }
- class Goal extends Exception
- {
- public Goal()
- {
- super("Goal");
- }
- }
- class Corner extends Exception
- {
- public Corner()
- {
- super("Corner");
- }
- }
- //-----------------------------------------------------------------------------------------
- class Ball
- {
- private int x, y;
- Random r = new Random();
- public Ball(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- public void play() throws Out, Corner, Goal
- {
- int check1 = 0, check2 = 0;
- this.x = r.nextInt(101);
- this.y = r.nextInt(51);
- if( y == 0 || y == 50)
- {
- check1 = 1;
- throw new Out();
- }
- if(( x == 0 || x == 100) && ( y >= 20 && y <= 30))
- {
- check2 = 1;
- throw new Goal();
- }
- if((x == 0 || x == 100) && check1 == 0 && check2 == 0)
- {
- throw new Corner();
- }
- }
- public int get_x()
- {
- return this.x;
- }
- public int get_y()
- {
- return this.y;
- }
- public String toString()
- {
- return "Ball at " + this.x + " " + this.y + "\n";
- }
- }
- //-----------------------------------------------------------------------------------------
- class Game
- {
- private String team1, team2;
- private int outs = 0, goals = 0, corners = 0;
- int prev_x = 1, prev_y = 1;
- Ball ball = new Ball(prev_x,prev_y);
- int goals_1 = 0, goals_2 = 0;
- public Game(String team1, String team2)
- {
- this.team1 = team1;
- this.team2 = team2;
- }
- public String toString()
- {
- return team1 + " - " + team2 + "\n" + goals + " Goals\n" + outs+ " Outs\n" + corners + " Corners\n";
- }
- public String simulate()
- {
- for(int i = 0; i < 2000; i++)
- {
- try
- {
- prev_x = ball.get_x();
- prev_y = ball.get_y();
- ball.play();
- System.out.println(ball);
- }
- catch(Out e)
- {
- outs++;
- System.out.println(e.getMessage());
- ball = new Ball(prev_x, prev_y);
- }
- catch(Corner e)
- {
- corners++;
- System.out.println(e.getMessage());
- if(ball.get_x() <= 50)
- if(ball.get_y() <= 25)
- ball = new Ball(0,0);
- else
- ball = new Ball(0, 50);
- else
- {
- if(ball.get_y() <= 25)
- ball = new Ball(100,0);
- else
- ball = new Ball(100, 50);
- }
- }
- catch(Goal e)
- {
- goals++;
- System.out.println(e.getMessage());
- if(ball.get_x() == 0 && (ball.get_y() >= 20 && ball.get_y() <= 30))
- goals_1++;
- else if(ball.get_x() == 100 && (ball.get_y() >= 20 && ball.get_y() <= 30))
- goals_2++;
- ball = new Ball(50, 25);
- }
- }
- String team_goals = team1 + " = " + goals_1+ " " + team2 + " = " + goals_2 + "\n";
- return team_goals;
- }
- }
- //-----------------------------------------------------------------------------------------
- class Main
- {
- public static void main(String[] args)
- {
- Game game = new Game("Team 1", "Team 2");
- System.out.println(game.simulate());
- System.out.println(game);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment