Exemplo n.º 1
1
  public void paint(Graphics g) {
    // paint()
    int x, y;
    Random place = new Random(WorldParams.version);
    g.translate(5, 25);
    Color curColor = new Color(0, 0, 0);

    if (pleaseDraw) {
      for (int i = 0; i < WorldParams.worldXsize; i++)
        for (int j = 0; j < WorldParams.worldYsize; j++) {
          curColor = new Color(0, 0, 0);
          if (World2D.cWorld[i][j].hereIsGrass) curColor = new Color(0, 100, 0);
          g.setColor(curColor);
          g.fillRect(i * scale, j * scale, scale, scale);
          if (World2D.cWorld[i][j].agents.size() > 0) {
            for (int z = 0; z < World2D.cWorld[i][j].agents.size(); z++) {
              x = place.nextInt(scale - 8);
              y = place.nextInt(scale - 8);
              agent = (Genome) World2D.cWorld[i][j].agents.elementAt(z);
              switch (agent.act) {
                case 0: // rest
                  curColor = new Color(255, 255, 255);
                  break;
                case 1: // eat
                  curColor = new Color(255, 255, 0);
                  break;
                case 2: // moving
                  curColor = new Color(100, 100, 250);
                  break;
                case 3: // turn left
                  curColor = new Color(0, 0, 255);
                  break;
                case 4: // turn right
                  curColor = new Color(0, 0, 255);
                  break;
                case 5: // divide
                  curColor = new Color(255, 50, 150);
                  break;
                case 6: // fight
                  curColor = new Color(255, 0, 0);
                  break;
              } // end switch
              g.setColor(curColor);
              g.fillRect(i * scale + x, j * scale + y, 8, 8);
            }
          }
        }
    }
    pleaseDraw = false;
    if (fForm_Create) InitialPositionSet();
  } // End of paint()
Exemplo n.º 2
0
  void enemieHandeler() {
    int ran = (int) ((70 - difficulty) * delta);
    if (ran <= 0) return;
    if (rand.nextInt(ran) == 1) {
      int x = rand.nextInt(2);
      int y = rand.nextInt(2);
      int size = rand.nextInt(1000) + 50;
      if (x == 0 && y == 0)
        if (rand.nextBoolean()) x = rand.nextInt(4) + 1;
        else y = rand.nextInt(4) + 1;

      if (rand.nextBoolean()) x = -x;
      if (rand.nextBoolean()) y = -y;
      size = size * (rand.nextInt(difficulty) + 1);

      x = (int) (x * 400 + player.body.getCenterX());
      y = (int) (y * 400 + player.body.getCenterY());

      int type = 0;

      if (rand.nextInt(60 / difficulty) <= 1) type = rand.nextInt(3) + 1;

      spawnEnemie(x, y, size, type);
    }
  }
Exemplo n.º 3
0
 private Card genNewCard() {
   Random rand = new Random();
   Card card = new Card(rand.nextInt(52) + 1);
   while (cardUsed.contains(card)) card = new Card(rand.nextInt(52) + 1);
   cardUsed.add(card);
   return card;
 }
Exemplo n.º 4
0
  void generateBackground(int size) {
    backgroundWidth = 100;
    int spacing = backgroundWidth;
    int id = 0;
    for (int i = 0; i < bounds.getWidth() / spacing; i++) {
      background[id][0] = spacing * (i + 1) - bounds.getWidth() / 2;
      background[id][1] = -bounds.getHeight() / 2;
      background[id][2] = spacing * (i + 1) - bounds.getWidth() / 2;
      background[id][3] = 0;
      background[id][4] = spacing * (i + 1) - bounds.getWidth() / 2;
      background[id][5] = bounds.getHeight();

      background[id][6] = rand.nextInt(3) - 1;
      background[id][7] = rand.nextInt(3) - 1;
      background[id][8] = 1;
      id++;
    }
    for (int i = 0; i < bounds.getHeight() / spacing; i++) {
      background[id][0] = -bounds.getHeight() / 2;
      background[id][1] = spacing * (i + 1) - bounds.getHeight() / 2;
      background[id][2] = 0;
      background[id][3] = spacing * (i + 1) - bounds.getWidth() / 2;
      background[id][4] = bounds.getWidth();
      background[id][5] = spacing * (i + 1) - bounds.getHeight() / 2;

      background[id][6] = rand.nextInt(3) - 1;
      background[id][7] = rand.nextInt(3) - 1;
      background[id][8] = 1;
      id++;
    }
  }
Exemplo n.º 5
0
  void itemHandeler(Graphics2D map) {
    boolean tmp = false;
    if (rand.nextInt((int) (300 * delta + difficulty * 40)) <= 1 && tmp) {
      int x = rand.nextInt(4) - 2;
      int y = rand.nextInt(4) - 2;
      int type = 1;
      if (x == 0 && y == 0) {
        x = 0;
        y = 1;
      }
      if (rand.nextInt(10) == 1) {
        type = 2;
      }
      if (rand.nextInt(5) == 1)
        for (int i = 0; i < player.weapon.length; i++)
          if (player.weapon[i][11] != null && (Boolean) player.weapon[i][11] == true)
            if ((Integer) player.weapon[i][10] < 5) type = 3;

      spawnItem(player.body.getCenterX() + 200 * x, player.body.getCenterY() + 200 * y, type);
    }

    for (int i = 1; i < items.length; i++) {
      if (items[i] != null) {
        if (items[i].body == null) {
          items[i] = null;
        } else {
          items[i].Runtime(map, player, delta);
        }
      }
    }
  }
Exemplo n.º 6
0
 /**
  * roll takes no arguments and randomly generates a value between 1 and 6. 1 = thorax 2 = head 3 =
  * legs 4 = eyes 5 = antenna 6 = abdomen
  */
 public void roll() {
   Random rnd = new Random();
   int min = 1;
   int max = 6;
   int rndInt = rnd.nextInt(max - min + 1) + min;
   this.roll = rndInt;
 }
  public void updatePosition() {
    Point position = new Point(spaceOccupied.x, spaceOccupied.y);

    // Insert random behavior.  During
    // each update, a sprite has about
    // one chance in 10 of making a
    // random change to its
    // motionVector.  When a change
    // occurs, the motionVector
    // coordinate values are forced to
    // fall between -7 and 7.  This
    // puts a cap on the maximum speed
    // for a sprite.
    if (rand.nextInt() % 10 == 0) {
      Point randomOffset = new Point(rand.nextInt() % 3, rand.nextInt() % 3);
      motionVector.x += randomOffset.x;
      if (motionVector.x >= 7) motionVector.x -= 7;
      if (motionVector.x <= -7) motionVector.x += 7;
      motionVector.y += randomOffset.y;
      if (motionVector.y >= 7) motionVector.y -= 7;
      if (motionVector.y <= -7) motionVector.y += 7;
    } // end if

    // Move the sprite on the screen
    position.translate(motionVector.x, motionVector.y);

    // Bounce off the walls
    boolean bounceRequired = false;
    Point tempMotionVector = new Point(motionVector.x, motionVector.y);

    // Handle walls in x-dimension
    if (position.x < bounds.x) {
      bounceRequired = true;
      position.x = bounds.x;
      // reverse direction in x
      tempMotionVector.x = -tempMotionVector.x;
    } else if ((position.x + spaceOccupied.width) > (bounds.x + bounds.width)) {
      bounceRequired = true;
      position.x = bounds.x + bounds.width - spaceOccupied.width;
      // reverse direction in x
      tempMotionVector.x = -tempMotionVector.x;
    } // end else if

    // Handle walls in y-dimension
    if (position.y < bounds.y) {
      bounceRequired = true;
      position.y = bounds.y;
      tempMotionVector.y = -tempMotionVector.y;
    } else if ((position.y + spaceOccupied.height) > (bounds.y + bounds.height)) {
      bounceRequired = true;
      position.y = bounds.y + bounds.height - spaceOccupied.height;
      tempMotionVector.y = -tempMotionVector.y;
    } // end else if

    if (bounceRequired)
      // save new motionVector
      setMotionVector(tempMotionVector);
    // update spaceOccupied
    setSpaceOccupied(position);
  } // end updatePosition()
  public Point getEmptyPosition(Dimension spriteSize) {
    Rectangle trialSpaceOccupied = new Rectangle(0, 0, spriteSize.width, spriteSize.height);
    Random rand = new Random(System.currentTimeMillis());
    boolean empty = false;
    int numTries = 0;

    // Search for an empty position
    while (!empty && numTries++ < 100) {
      // Get a trial position
      trialSpaceOccupied.x = Math.abs(rand.nextInt() % backgroundImage.getSize().width);
      trialSpaceOccupied.y = Math.abs(rand.nextInt() % backgroundImage.getSize().height);
      // Iterate through existing
      // sprites, checking if position
      // is empty
      boolean collision = false;
      for (int cnt = 0; cnt < size(); cnt++) {
        Rectangle testSpaceOccupied = ((Sprite) elementAt(cnt)).getSpaceOccupied();
        if (trialSpaceOccupied.intersects(testSpaceOccupied)) {
          collision = true;
        } // end if
      } // end for loop
      empty = !collision;
    } // end while loop
    return new Point(trialSpaceOccupied.x, trialSpaceOccupied.y);
  } // end getEmptyPosition()
  public static int[][] generateCellMap(
      int ax, int ay, int gx, int gy, int gt, int seed, double p) {

    int w = 30;
    int h = 30;
    int[][] map = new int[w][h];

    map[gx][gy] = gt + 1;

    Random rand = new Random(seed);

    for (int x = 0; x < w; x++) {
      for (int y = 0; y < h; y++) {
        if ((x == ax && y == ay) || (x == gx && y == gy)) {
          continue;
        }
        double roll = rand.nextDouble();
        if (roll < p) {
          map[x][y] = chooseType(rand, gt);
        }
      }
    }

    return map;
  }
Exemplo n.º 10
0
  /*
   * @param i - accepts int i for person number
   * @return - new positions
   */
  public int[] getNewPosition(int i) {
    // logic to auto select for computer player:
    if (i != 0) {
      // while space is not selected, randomly select rooms and spaces.
      while (spaceSelected == false) {

        // generate random number
        Random generator = new Random();
        int room = generator.nextInt(10);

        // random select a room
        allRooms[room].doClick();

        // generate random space
        int space = generator.nextInt(524);

        // randomly select a space
        btn[space].doClick();
      }
    }

    // if (debugBoardPanel) { System.out.println("[526] boardPanel.getNewPosition(): Called.") ; }
    int[] newPositions = {newPositionColumn, newPositionRow};

    // if (debugBoardPanel) { System.out.println("[530] boardPanel.getNewPosition().newPositions: "
    // + newPositions[0] + ", " + newPositions[1]); }

    return newPositions;
  }
Exemplo n.º 11
0
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    if (dst == null) dst = createCompatibleDestImage(src, null);

    int width = src.getWidth();
    int height = src.getHeight();
    int numScratches = (int) (density * width * height / 100);
    ArrayList<Line2D> lines = new ArrayList<Line2D>();
    {
      float l = length * width;
      Random random = new Random(seed);
      Graphics2D g = dst.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setColor(new Color(color));
      g.setStroke(new BasicStroke(this.width));
      for (int i = 0; i < numScratches; i++) {
        float x = width * random.nextFloat();
        float y = height * random.nextFloat();
        float a = angle + ImageMath.TWO_PI * (angleVariation * (random.nextFloat() - 0.5f));
        float s = (float) Math.sin(a) * l;
        float c = (float) Math.cos(a) * l;
        float x1 = x - c;
        float y1 = y - s;
        float x2 = x + c;
        float y2 = y + s;
        g.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
        lines.add(new Line2D.Float(x1, y1, x2, y2));
      }
      g.dispose();
    }

    if (false) {
      //		int[] inPixels = getRGB( src, 0, 0, width, height, null );
      int[] inPixels = new int[width * height];
      int index = 0;
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          float sx = x, sy = y;
          for (int i = 0; i < numScratches; i++) {
            Line2D.Float l = (Line2D.Float) lines.get(i);
            float dot = (l.x2 - l.x1) * (sx - l.x1) + (l.y2 - l.y1) * (sy - l.y1);
            if (dot > 0) inPixels[index] |= (1 << i);
          }
          index++;
        }
      }

      Colormap colormap = new LinearColormap();
      index = 0;
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          float f = (float) (inPixels[index] & 0x7fffffff) / 0x7fffffff;
          inPixels[index] = colormap.getColor(f);
          index++;
        }
      }
      setRGB(dst, 0, 0, width, height, inPixels);
    }
    return dst;
  }
Exemplo n.º 12
0
 // 给定范围获得随机颜色
 Color getRandColor(int fc, int bc) {
   Random random = new Random();
   if (fc > 255) fc = 255;
   if (bc > 255) bc = 255;
   int r = fc + random.nextInt(bc - fc);
   int g = fc + random.nextInt(bc - fc);
   int b = fc + random.nextInt(bc - fc);
   return new Color(r, g, b);
 }
  private void createSarena() {

    fuelCapacity = randy.nextInt(500) + 201;
    materialCapacity = randy.nextInt(500) + 201;
    controlledBy = -1;
    planetType = SARENA;
    baseOffense = 5;
    baseDefense = 5;
  }
  private void createTomeko() {

    materialCapacity = randy.nextInt(400) + 301;
    fuelCapacity = randy.nextInt(300) + 1;
    controlledBy = -1;
    planetType = TOMEKO;
    baseOffense = 3;
    baseDefense = 3;
  }
  private void createThesban() {

    fuelCapacity = randy.nextInt(400) + 301;
    materialCapacity = randy.nextInt(300) + 1;
    controlledBy = -1;
    planetType = THESBAN;
    baseOffense = 3;
    baseDefense = 3;
  }
Exemplo n.º 16
0
 public void randomize() {
   seed = new Date().getTime();
   randomGenerator.setSeed(seed);
   a = randomGenerator.nextFloat();
   b = randomGenerator.nextFloat();
   c = randomGenerator.nextFloat();
   d = randomGenerator.nextFloat();
   k = randomGenerator.nextInt() % 20 - 10;
 }
Exemplo n.º 17
0
 private int displace(int rgb, float amount) {
   int r = (rgb >> 16) & 0xff;
   int g = (rgb >> 8) & 0xff;
   int b = rgb & 0xff;
   r = PixelUtils.clamp(r + (int) (amount * (randomGenerator.nextFloat() - 0.5)));
   g = PixelUtils.clamp(g + (int) (amount * (randomGenerator.nextFloat() - 0.5)));
   b = PixelUtils.clamp(b + (int) (amount * (randomGenerator.nextFloat() - 0.5)));
   return 0xff000000 | (r << 16) | (g << 8) | b;
 }
Exemplo n.º 18
0
 private int randomRGB(int[] inPixels, int x, int y) {
   if (useImageColors) {
     return inPixels[y * originalSpace.width + x];
   } else {
     int r = (int) (255 * randomGenerator.nextFloat());
     int g = (int) (255 * randomGenerator.nextFloat());
     int b = (int) (255 * randomGenerator.nextFloat());
     return 0xff000000 | (r << 16) | (g << 8) | b;
   }
 }
Exemplo n.º 19
0
 int selectCycle() {
   if (t < N) {
     return generator.nextInt(5);
   } else {
     if (Rtmp > alpha) {
       currentPath = previousPath;
     } else currentPath = generator.nextInt(5);
   }
   return currentPath;
 }
Exemplo n.º 20
0
 public int attackDmg() {
   Random random = new Random();
   float randomInt = (float) (random.nextInt(10 + (agi * 2)) + 1);
   System.out.println(
       "PDAMAGE: "
           + this.initDmg
           + " PFINAL DAMAGE: "
           + (int) (this.initDmg + (this.initDmg * (randomInt / 100)))
           + " PRANDOM: "
           + randomInt);
   return (int) (this.initDmg + (this.initDmg * (randomInt / 100)));
 }
Exemplo n.º 21
0
  @SuppressWarnings("unchecked")
  public void _addRandomSampleData(int setCount, int tempCount) throws Exception {
    long sts = this.minDateTS;
    long ets = this.maxDateTS;
    Random ran = new Random(sts);

    /* init datasets */
    if (setCount <= 0) {
      setCount = 1;
    }
    java.util.List<Data> dataSet[] = new java.util.List[setCount];
    for (int d = 0; d < dataSet.length; d++) {
      dataSet[d] = new Vector<Data>();
    }

    /* populate random temperature data */
    double rangeC = this.maxTempC - this.minTempC;
    long deltaSize = (ets - sts) / (long) tempCount;
    long deltaRangeTS = DateTime.HourSeconds(3);
    long ts = sts + (deltaSize / 2L);
    double Cs = (ran.nextDouble() * rangeC * 0.10) + (rangeC * 0.05);
    for (int t = 0; t < tempCount; t++) {
      double C[] = new double[dataSet.length];
      for (int d = 0; d < dataSet.length; d++) {
        C[d] = (ran.nextDouble() * 7.0) + ((d == 0) ? Cs : C[d - 1]) - 2.5;
        if (C[d] < this.minTempC) {
          C[d] = this.minTempC;
        }
        if (C[d] > this.maxTempC) {
          C[d] = this.maxTempC;
        }
        dataSet[d].add(new Data(ts, C[d]));
      }
      ts =
          sts
              + ((long) (t + 1) * deltaSize)
              + (long) ran.nextInt((int) deltaRangeTS)
              + (deltaRangeTS / 2L);
      if (ts > ets) {
        ts = ets - 1L;
      }
      // ts = sts + ((t==0)?DateTime.HourSeconds(1):(long)ran.nextInt((int)(ets - sts)));
      Cs = C[0];
    }

    /* add datasets */
    for (int d = 0; d < dataSet.length; d++) {
      ListTools.sort(dataSet[d], null);
      Color color = TEMP_COLOR[d % TEMP_COLOR.length];
      this.addDataSet(color, "Temp " + (d + 1), dataSet[d].toArray(new Data[dataSet[d].size()]));
    }
  }
  public static double runTrial(int memoryType, int probability) {
    HashMap<Point, EnergySource> energySources = new HashMap<>();
    int sources = 40, width = 200, height = 200, energy = 125;
    Point location = new Point(0, 0);
    Random generator = new Random();
    Robot<EnergySource> robot;
    EnergySource energySource;
    double amount;

    // Generate all the energy sources and the robot
    while (energySources.size() < sources) {
      location = new Point(generator.nextInt(width), generator.nextInt(height));
      if (isFarEnough(location, energySources)) {
        energySources.put(location, new EnergySource(location, energy));
      }
    }
    while (!isFarEnough(location, energySources)) {
      location = new Point(generator.nextInt(width), generator.nextInt(height));
    }
    robot = new Robot<>(location, memoryType);
    // Program loop
    while (robot.getEnergy() > 0) {
      if (robot.isCurious()) {
        robot.moveRandomly(width, height);
      } else {
        energySource = robot.retrieveEnergySource(probability);
        if (energySource != null) {
          robot.moveToLocation(energySource.getLocation());
        } else {
          robot.moveRandomly(width, height);
        }
        energySource = energySources.get(robot.getLocation());
        if (energySource != null) {
          if (robot.getMaxEnergy() - robot.getEnergy() < energySource.getEnergy()) {
            amount = robot.getMaxEnergy() - robot.getEnergy();
          } else {
            amount = energySource.getEnergy();
          }
          robot.increaseEnergy(amount);
          energySource.decreaseEnergy(amount);
          if (energySource.getEnergy() <= 0) {
            robot.forgetEnergySource(energySource);
            energySources.remove(robot.getLocation());
          }
        }
      }
      robot.detectEnergySources(energySources);
    }
    return robot.getTravelDistance();
  }
Exemplo n.º 23
0
  protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
    int[] outPixels = new int[width * height];

    randomGenerator.setSeed(seed);

    int w1 = width - 1;
    int h1 = height - 1;
    putPixel(0, 0, randomRGB(inPixels, 0, 0), outPixels, width);
    putPixel(w1, 0, randomRGB(inPixels, w1, 0), outPixels, width);
    putPixel(0, h1, randomRGB(inPixels, 0, h1), outPixels, width);
    putPixel(w1, h1, randomRGB(inPixels, w1, h1), outPixels, width);
    putPixel(w1 / 2, h1 / 2, randomRGB(inPixels, w1 / 2, h1 / 2), outPixels, width);
    putPixel(0, h1 / 2, randomRGB(inPixels, 0, h1 / 2), outPixels, width);
    putPixel(w1, h1 / 2, randomRGB(inPixels, w1, h1 / 2), outPixels, width);
    putPixel(w1 / 2, 0, randomRGB(inPixels, w1 / 2, 0), outPixels, width);
    putPixel(w1 / 2, h1, randomRGB(inPixels, w1 / 2, h1), outPixels, width);

    int depth = 1;
    while (doPixel(0, 0, width - 1, height - 1, outPixels, width, depth, 0)) depth++;

    if (useColormap && colormap != null) {
      int index = 0;
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          outPixels[index] = colormap.getColor((outPixels[index] & 0xff) / 255.0f);
          index++;
        }
      }
    }
    return outPixels;
  }
 protected static int chooseType(Random rand, int gt) {
   int type = gt;
   while (type == gt) {
     type = rand.nextInt(5);
   }
   return type + 1;
 }
Exemplo n.º 25
0
 public void setRandom(Random random)
       /* Sets all the scalars in 'this' to random values uniformly distributed
        *   over (0.0, 1.0) using the pseudo-random generator 'random'. */
     {
   // setZero();
   for (int k = 0; k < size; ++k) f[k] = random.nextFloat();
 }
Exemplo n.º 26
0
 // Generate n components of random sizes
 public static JComponent[] generateRandomComponents(int n) {
   Random r = new Random(0);
   JComponent[] c = new JComponent[n];
   int m = n;
   while (m > 0) {
     int i = r.nextInt(n);
     if (c[i] == null) {
       c[i] = new JLabel("Component " + i, null, SwingConstants.CENTER);
       int w = 5 * (2 + r.nextInt(20));
       int h = 5 * (2 + r.nextInt(20));
       c[i].setPreferredSize(new Dimension(w, h));
       c[i].setBorder(new EtchedBorder());
       m--;
     }
   }
   return c;
 }
Exemplo n.º 27
0
 public Star(Point loc, int red, int green, int blue, int alpha) {
   location = loc;
   r = red;
   b = blue;
   g = green;
   a = alpha;
   pulseRate = rand.nextInt(254);
 }
Exemplo n.º 28
0
 public Star(Point loc, Color c, int alpha) {
   location = loc;
   r = c.getRed();
   b = c.getBlue();
   g = c.getGreen();
   a = alpha;
   pulseRate = rand.nextInt(155);
 }
Exemplo n.º 29
0
 public void receiveDmg(int damage) {
   Random random = new Random();
   float randomInt = (float) (random.nextInt(10 + str) + 1);
   System.out.println(
       "DAMAGE: "
           + damage * (randomInt / 100)
           + " FINAL DAMAGE: "
           + (int) (damage - (damage * (randomInt / 100)))
           + " RANDOM: "
           + randomInt);
   this.currentHP = currentHP - (int) (damage - damage * (randomInt / 100));
   if (this.currentHP <= 0) {
     this.currentHP = 0;
     this.isAlive = false;
   }
   // System.out.println("NEW HP: " + currentHP);
 }
Exemplo n.º 30
0
 private static String generateKey(final int size) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < size; i++) {
     int idx = rand.nextInt(ALPHANUMERIC.length());
     sb.append(ALPHANUMERIC.substring(idx, idx + 1));
   }
   return (sb.toString());
 }