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 Out extends Exception{
- public Out(){
- super("Mingea este in afara terenului -> Out");
- }
- }
- class Gol extends Exception{
- public Gol(){
- super("S-a dat gol -> Gol");
- }
- }
- class Corner extends Exception{
- public Corner(){
- super("Este corner -> Corner");
- }
- }
- class Minge{
- private CoordinateGenerator c = new CoordinateGenerator();
- private int X,Y;
- public Minge(int x,int y){
- this.X = x;
- this.Y = y;
- }
- public void suteaza() throws Out, Gol, Corner{
- X = c.generateX();
- Y = c.generateY();
- if(Y == 0 || Y == 50)
- throw new Out();
- else if((X == 0 || X == 100) && (Y >= 20 && Y <= 30))
- throw new Gol();
- else if((X == 0 || X == 100) && ((Y > 0 && Y<20) || (Y >30 && Y < 50)))
- throw new Corner();
- }
- public int getX(){
- return X;
- }
- public int getY(){
- return Y;
- }
- }
- class Joc{
- private String Echipa1,Echipa2;
- private int nr_gol1, nr_gol2, nr_out, nr_corner;
- public Joc(String e_1, String e_2){
- Echipa1 = e_1;
- Echipa2 = e_2;
- }
- public String toString(){
- return Echipa1+ " vs " + Echipa2 + " "+ " Scor: " + nr_gol1 + "-" + nr_gol2 + "\nNumar out-uri: " + nr_out + "\nNumar cornere: " + nr_corner;
- }
- public void simuleaza(){
- Minge minge = new Minge(50,25);
- int x_temp = 0, y_temp = 0, i;
- for(i = 1; i <= 1000; i++)
- {
- try{
- x_temp = minge.getX();
- y_temp = minge.getY();
- minge.suteaza();
- System.out.println(Echipa1 + " - " + Echipa2 + ": Mingea se afla la poz("+ minge.getX() + "," + minge.getY()+")");
- } catch(Gol g){
- if(minge.getX() == 0)
- nr_gol1++;
- else
- nr_gol2++;
- System.out.println(g.getMessage());
- Minge m = new Minge(50,25);
- } catch(Out o){
- nr_out++;
- System.out.println(o.getMessage());
- Minge m = new Minge(x_temp,y_temp);
- } catch(Corner c){
- Minge m;
- nr_corner++;
- System.out.println(c.getMessage());
- if(minge.getX() == 0)
- if(minge.getY() > 0 && minge.getY() < 20)
- m = new Minge(0,0);
- else
- m = new Minge(10,50);
- else
- if(minge.getY() > 0 && minge.getY() < 20)
- m = new Minge(50,50);
- else
- m = new Minge(100,100);
- }
- }
- }
- }
- class Main{
- public static void main(String[] argv){
- Joc game = new Joc("Echipa1", "Echipa2");
- game.simuleaza();
- Joc game2=new Joc("Echipa3","Echipa4");
- game2.simuleaza();
- System.out.println(game);
- System.out.println(game2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement