Advertisement
starcat38

Create

Nov 23rd, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.49 KB | None | 0 0
  1. void Create (int n)
  2.     {
  3.         Random rand = new Random();
  4.         ships = new PointShip[n];
  5.         for (int x=0; x<n; x++) {
  6.             do {
  7.                 ships[x].pos_x=rand.nextInt(10);
  8.                 ships[x].pos_y=rand.nextInt(10);
  9.                 if(CanPut(ships[x],n)) break;
  10.             } while (true);
  11.         }
  12.     }
  13.     boolean CanPut(PointShip ship, int n) {
  14.         int repeat=0;
  15.         for (int i=0; i<n; i++) {
  16.             if (ships[i].pos_x==ship.pos_x && ships[i].pos_y==ship.pos_y) {
  17.                 repeat++;
  18.             }
  19.         }
  20.         if (repeat>1) return false;
  21.         else return true;
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement