Example #1
0
 /*Creates the equipment in all states to initiate the game */
 private void fillStates(int sAvail, int sRent, int sShop) {
   int[] statesSizes = {sAvail, sRent, sShop};
   int s = 1;
   for (int stateSize : statesSizes) {
     for (int t = 1; t <= TYPES; t++) {
       for (int i = 1; i <= stateSize; i++) {
         Equipment e = new Equipment();
         e.state = s;
         e.type = t;
         int sMax =
             Math.max(
                 s - 2, 0); /*formula to make sure identifiers resume counting and stay unique */
         int sMin =
             Math.min(
                 s - 1, 1); /*formula to make sure identifiers resume counting and stay unique */
         e.ident = i + (sMin) * sAvail + (sMax) * sRent;
         e.c = equipColor(e.type);
         switch (s) {
           case 1:
             availEquipment.add(e);
             break;
           case 2:
             rentEquipment.add(e);
             break;
           case 3:
             shopEquipment.add(e);
             break;
         }
       }
     }
     s++;
   }
   computeInitialTimes();
   assignShapes();
 }
Example #2
0
  private void DoAI(int pnum) {
    int[] dx = {0, 1, 0, -1};
    int[] dy = {-1, 0, 1, 0};
    int tdir;

    if (palive[pnum] != 1) return;
    if (map[(px[pnum] + dx[pdir[pnum]]) + (py[pnum] + dy[pdir[pnum]]) * SIZE_X] != 0) {
      if (Math.random() > 0.5) {
        tdir = (pdir[pnum] + 1) % 4;
        if (map[(px[pnum] + dx[tdir]) + (py[pnum] + dy[tdir]) * SIZE_X] == 0) pdir[pnum] = tdir;
        else pdir[pnum] = (pdir[pnum] + 3) % 4;
      } else {
        tdir = (pdir[pnum] + 3) % 4;
        if (map[(px[pnum] + dx[tdir]) + (py[pnum] + dy[tdir]) * SIZE_X] == 0) pdir[pnum] = tdir;
        else pdir[pnum] = (pdir[pnum] + 1) % 4;
      }
    } else if (Math.random() > 0.985) {
      if (Math.random() > 0.5) {
        tdir = (pdir[pnum] + 1) % 4;
        if (map[(px[pnum] + dx[tdir]) + (py[pnum] + dy[tdir]) * SIZE_X] == 0) pdir[pnum] = tdir;
      } else {
        tdir = (pdir[pnum] + 3) % 4;
        if (map[(px[pnum] + dx[tdir]) + (py[pnum] + dy[tdir]) * SIZE_X] == 0) pdir[pnum] = tdir;
      }
    }
  }
 // -------------------------------------------------------------------------------------------------------------------------------------------------------
 // Decorations
 public void newDecos() {
   int prob = (int) (Math.random() * 100); // randomly spawnst he decoration
   if (prob == 1) {
     if (decoList.size() < 1) {
       int prob2 = (int) (Math.random() * 2);
       if (prob2 == 1) { // right side or left side
         decoList.add(new Decorations(-700, (int) (backy * 0.1) % 23080, false));
       } else {
         decoList.add(new Decorations(-700, (int) (backy * 0.1) % 23080, true));
       }
     }
   }
   Boolean check = true;
   for (Decorations i : decoList) {
     if (i.getYTop() >= 2000) {
       check = false;
       break;
     }
   }
   if (check == false
       && decoList.size()
           > 0) { // theres only on in the lsit but for consitency we kept it as a list
     decoList.remove(0);
   }
 }
Example #4
0
  public void mouseClicked(MouseEvent e) {
    int x;
    int y;

    e.consume();

    if (mouseEventsEnabled) {

      x = Math.round(e.getX() / scale);
      y = Math.round(e.getY() / scale);

      // allow for the canvas margin
      y -= margin;

      // System.out.println("Mouse Click: (" + x + ", " + y + ")");

      if (e.getClickCount() < 2) {
        if (nativeSelectItem(x, y)) {
          parentFTAFrame.updateFrame();
        }
      } else {
        if (nativeSelectItem(x, y)) {
          parentFTAFrame.updateFrame();
        }

        editSelected();
        parentFTAFrame.updateFrame();
      }

      if (focusEventsEnabled) {
        // tell the main Canvas to point to this coordinate
        parentFTAFrame.setCanvasFocus(x, y);
      }
    }
  }
Example #5
0
 public void update(double prob_slowdown, double prob_create) {
   int i = 0;
   while (i < LENGTH && speed[i] == -1) i++;
   while (i < LENGTH) {
     if (Math.random() <= prob_slowdown && speed[i] > 0) speed[i]--;
     else if (speed[i] < MAXSPEED) speed[i]++;
     int inext = i + 1;
     while (inext < LENGTH && speed[inext] == -1) inext++;
     if (inext < LENGTH) {
       if (speed[i] >= inext - i) speed[i] = inext - i - 1;
     }
     if (speed[i] > 0) {
       if (i + speed[i] < LENGTH) {
         int ni = i + speed[i];
         speed[ni] = speed[i];
         colors[ni] = colors[i];
       }
       speed[i] = -1;
     }
     i = inext;
   }
   if (Math.random() <= prob_create && speed[0] == -1) {
     speed[0] = (int) (5.99 * Math.random());
     colors[0] = ++count % 10 == 0 ? Color.red : Color.black;
   }
 }
Example #6
0
 /*Generates a random exponential distribution random time difference vs. timeIn based on the frequency lambda */
 private double nextRandomTime(double lambda) {
   double timeLag = 0;
   /*System.out.println("I am computing nextRandomTime");
   System.out.println("When I am getting timeIn of " +timeIn+" and lambda of "+lambda);*/
   double random = Math.random();
   timeLag = -Math.log(random) / (lambda / (LENGTHOFADAY * 1000));
   /*System.out.println("I am returning a time difference of "+timeLag);*/
   return timeLag;
 }
 public void run() {
   while (running) {
     if (!paused) {
       if (mode == 0) {
         for (double i = 0; i < 3.14f; i = i + 0.1f) {
           for (int j = 0; j < 12; j++) {
             double osc = Math.sin(i) * 127;
             int value = (int) osc;
             MIDILight(j, value);
           }
           delay(30);
         }
         for (double i = 3.14f; i >= 0; i = i - 0.1f) {
           for (int j = 0; j < 12; j++) {
             double osc = Math.sin(i) * 127;
             int value = (int) osc;
             MIDILight(j, value);
           }
           delay(30);
         }
       } else if (mode == 1) {
         for (int j = 0; j < 12; j++) {
           MIDILight(j, 127);
         }
         mode = 1000;
       } else if (mode == 2) {
         for (int j = 0; j < 12; j++) {
           MIDILight(j, 0);
         }
         mode = 1000;
       } else if (mode == 3) {
         int controller = (int) random(12);
         int randomValue = (int) random(127);
         MIDILight(controller, randomValue);
         delay(30);
       } else if (mode == 4) {
         for (int i = 0; i < 64; i++) {
           for (int j = 0; j < 12; j++) {
             MIDILight(j, i * 2);
           }
           delay(20);
         }
         for (int i = 63; i >= 0; i--) {
           for (int j = 0; j < 12; j++) {
             MIDILight(j, i * 2);
           }
           delay(20);
         }
       } else {
         delay(10);
         // FIX THIS. WITHOUT THIS THE CPU USE GOES THROUGH THE ROOF. NEED A WAY
         // TO SLEEP THIS THREAD WHEN IT'S NOT IN USE.
       }
     }
   }
   System.out.println(id + " thread is done!");
 }
 public void nudge(int i) {
   x[i] += (double) rand.nextInt(1000) / 8756;
   y[i] += (double) rand.nextInt(1000) / 5432;
   int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);
   scale[i] = (double) tmpScale / 10;
   int nudgeX = (int) (((double) getWidth() / 2) * .8);
   int nudgeY = (int) (((double) getHeight() / 2) * .60);
   xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;
   yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;
 }
 public void loseCoins() {
   int x = player1.getX();
   int y = player1.getY();
   double losePercentage = 0.1;
   for (int i = 0;
       i < (int) coins * losePercentage;
       i++) { // makes the user lose 10 percent of the coin and draws them in a circle
     // System.out.println(i);
     int xPos = x + (int) (100 * Math.cos(Math.toRadians((360 / (coins * losePercentage)) * i)));
     int yPos = y - (int) (100 * Math.sin(Math.toRadians((360 / (coins * losePercentage)) * i)));
     coinList.add(new Coin(xPos, yPos, 3));
   }
   coins -= (int) (coins * losePercentage);
 }
Example #10
0
  /* move the ball */
  public void move(ArrayList movingObjects, int objectCounter) {
    // EFFECT OF GRAVITY
    gravityEffect(movingObjects);

    // MOVE
    pos_x += Math.round(x_speed);
    pos_y += Math.round(y_speed);

    // IS BALL OUT OF BOUNDS?
    isOut();

    // HAS BALL COLLIDED WITH ANYTHING?
    objectCollision(movingObjects);
  }
Example #11
0
  /**
   * Set the scroll position.
   *
   * @param x coordinate of upper left corner.
   * @param y coordinate of upper left corner.
   */
  public void setScrollPosition(int x, int y) {
    final DjVuImage image = getImage();
    if ((image != null) && ((x != scrollPosition.x) || (y != scrollPosition.y))) {
      final Dimension imageSize = image.getSize();
      final Dimension viewportSize = getViewportSize();
      x = Math.max(0, Math.min(imageSize.width - viewportSize.width, x));
      y = Math.max(0, Math.min(imageSize.height - viewportSize.height, y));

      if ((x != scrollPosition.x) || (y != scrollPosition.y)) {
        scrollPosition.setLocation(x, y);
        updateScrollbars();
        repaint(50L);
      }
    }
  }
Example #12
0
 /**
  * create a random surface
  *
  * @param obj
  * @param x1:surface x begin
  * @param x2:surface x end
  * @param y1:surface y begin
  * @param y2:surface y end
  * @return: the surface mObject
  */
 private mObject createRandomSurface(mObject obj, int x1, int x2, int y1, int y2) {
   int i = 0, j = 0;
   x1 /= 20;
   x2 /= 20;
   y1 /= 20;
   y2 /= 20;
   mPoint3[][] points = new mPoint3[x2 - x1 + 1][y2 - y1 + 1];
   for (i = y1; i <= y2; i++) {
     for (j = x1; j <= x2; j++) {
       points[j - x1][i - y1] =
           new mPoint3(j * 20, i * 20, (float) (Math.random() * 70 * Math.exp(0 - 0.3 * j)));
     }
   }
   return new mSurface(points, x2 - x1 + 1, y2 - y1 + 1);
 }
Example #13
0
 static { // data[] is a bitmap image of the ball of radius R
   data = new byte[R * 2 * R * 2];
   for (int Y = -R; Y < R; Y++) {
     int x0 = (int) (Math.sqrt(R * R - Y * Y) + 0.5);
     for (int X = -x0; X < x0; X++) {
       // sqrt(x^2 + y^2) gives distance from the spot light
       int x = X + hx, y = Y + hy;
       int r = (int) (Math.sqrt(x * x + y * y) + 0.5);
       // set the maximal intensity to the maximal distance
       // (in pixels) from the spot light
       if (r > maxr) maxr = r;
       data[(Y + R) * (R * 2) + (X + R)] = (r <= 0) ? 1 : (byte) r;
     }
   }
 }
Example #14
0
 /**
  * Create a note (sine wave) of the given frequency (Hz), for the given duration (seconds) scaled
  * to the given volume (amplitude).
  */
 public static double[] note(double hz, double duration, double amplitude) {
   int N = (int) (StdAudio.SAMPLE_RATE * duration);
   double[] a = new double[N + 1];
   for (int i = 0; i <= N; i++)
     a[i] = amplitude * Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
   return a;
 }
Example #15
0
  /* -------------------------------------------------------------*/
  private void objectCollision(ArrayList movingObjects) {
    for (int i = 0; i < movingObjects.size(); i++) {
      // make sure not testing if collided with yourself :P
      if (((SpaceObject) movingObjects.get(i)).getObjCount() != objectNum) {
        SpaceObject object = (SpaceObject) movingObjects.get(i);

        // find distance vector between two objects
        int x = pos_x - object.getXPos();
        int y = pos_y - object.getYPos();

        double distance = Math.sqrt(x * x + y * y);

        // has it collided with the object?
        if (distance < (radius + object.getRadius())) {
          // has it collided with a BULLET (or MISSILE)?
          if (object.isBullet()) {
            // do nothing
          }
          // is it another SPACESHIP? (INSTANT DEATH)
          else if (object.isSpaceShip()) {
            // do nothing
          }
          // collided with anything else (e.g PLANET): (INSTANT DEATH)
          else {
            collision.play();
            kill(movingObjects); // object has died
          }
        }
      }
    } // end for loop
  }
Example #16
0
  /* -------------------------------------------------------------*/
  private void gravityEffect(ArrayList movingObjects) {
    // find effect of gravity on this objects from all other objects
    for (int i = 0; i < movingObjects.size(); i++) {
      // reset variables
      double add_vx = 0.0;
      double add_vy = 0.0;

      SpaceObject object = (SpaceObject) movingObjects.get(i);

      if (object.getObjCount() != objectNum
          && // ignore yourself (distance = 0!)
          !object.isBullet()
          && // ignore bullets
          !object.isSpaceShip()) // ignore spaceships
      {

        // find distance vector between planet and ball
        int x = pos_x - object.getXPos();
        int y = pos_y - object.getYPos();

        double distance = Math.sqrt(x * x + y * y);

        // find effect of planet on velocity
        double add_vec = (SpaceObject.G * k * object.getMass() * ballMass) / (distance * distance);

        add_vx = -(x / distance) * add_vec;
        add_vy = -(y / distance) * add_vec;

        // add objects speeds onto spaceship speed (clip speed if too large)
        x_speed += add_vx;
        y_speed += add_vy;
      }
    }
  }
Example #17
0
  public void makeField() {
    int vector[] = new int[rows * cols];
    int temp = 0;
    int limit, m, n;
    field = new int[rows][cols];
    for (m = 0; m < rows; m++)
      for (n = 0; n < cols; n++)
        vector[(n * rows) + m] = field[m][n] = (int) (Math.random() * 10000);
    Sort(vector, 0, vector.length - 1);
    limit = vector[nummines - 1];
    for (m = 0; m < rows; m++)
      for (n = 0; n < cols; n++) {
        field[m][n] = (field[m][n] <= limit ? (-1) : (0));
        if (field[m][n] == -1) {
          minerowlist[temp] = m;
          minecollist[temp++] = n;
        }
      }

    for (m = 0; m < rows; m++)
      for (n = 0; n < cols; n++)
        if (field[m][n] == 0) {
          int v = countmines(m, n);
          field[m][n] = v;
        }
    // printField();
  }
 // -------------------------------------------------------------------------------------------------------------------------------------------------------
 // Patterns
 public void newPattern() {
   int prob = (int) (Math.random() * probs.get(level - 1) + 1);
   Pattern p = new Pattern(0, -300, prob, (int) (backy * 0.1));
   if (checkSpawn(new Rectangle(p.getX(), p.getY(), p.getWidth(), p.getHeight())) == true) {
     for (Coin i : p.getCoins()) {
       coinList.add(i);
     }
     for (Star i : p.getStars()) {
       starList.add(i);
     }
     for (Box i : p.getBoxes()) {
       boxList.add(i);
     }
     for (Jumper i : p.getJumpers()) {
       jumperList.add(i);
     }
     for (Enemy i : p.getEnemies()) {
       enemyList.add(i);
     }
     for (Spikes i : p.getSpikes()) {
       spikeList.add(i);
     }
     for (Powerup i : p.getPowerUps()) {
       pupList.add(i);
     }
     for (Rectangle i : p.getRects()) {
       rectList.add(i);
     }
   }
 }
 public void bulletCreator(
     int x, int y, double mouseX, double mouseY, ArrayList<ZBullet> bullets) {
   bullet = bullets;
   for (int i = 0; i < 150; i++) {
     double r = (Math.random());
     if (r < .33333) c = new Color(170, 64, 151);
     else if (r < .66666) c = new Color(186, 97, 191);
     else c = new Color(144, 86, 135);
     double bobX = x - mouseX;
     double bobY = y - mouseY;
     int sinX = (int) (Math.random() * 15 - 8);
     int sinY = (int) (Math.random() * 15 - 8);
     ZBullet bull = new ZBullet(sinX, sinY, (int) mouseX, (int) mouseY, 30, 10, c, this, 0, 0);
     bullet.add(bull);
   }
   ammo--;
 }
Example #20
0
 public void setList(String[][] text) {
   for (int i = 0; i < Math.min(nLists, text.length); i++) {
     list[i].clear();
     for (int j = 0; j < text[i].length; j++) {
       list[i].addItem(text[i][j]);
     }
   }
 }
  /**
   * The user has clicked in the applet. Figure out where and see if a legal move is possible. If it
   * is a legal move, respond with a legal move (if possible).
   */
  public void mouseReleased(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    switch (status()) {
      case WIN:
      case LOSE:
      case STALEMATE:
        play(getCodeBase(), "audio/return.au");
        white = black = 0;
        if (first) {
          white |= 1 << (int) (Math.random() * 9);
        }
        first = !first;
        repaint();
        return;
    }

    // Figure out the row/column
    Dimension d = getSize();
    int c = (x * 3) / d.width;
    int r = (y * 3) / d.height;
    if (yourMove(c + r * 3)) {
      repaint();

      switch (status()) {
        case WIN:
          play(getCodeBase(), "audio/yahoo1.au");
          break;
        case LOSE:
          play(getCodeBase(), "audio/yahoo2.au");
          break;
        case STALEMATE:
          break;
        default:
          if (myMove()) {
            repaint();
            switch (status()) {
              case WIN:
                play(getCodeBase(), "audio/yahoo1.au");
                break;
              case LOSE:
                play(getCodeBase(), "audio/yahoo2.au");
                break;
              case STALEMATE:
                break;
              default:
                play(getCodeBase(), "audio/ding.au");
            }
          } else {
            play(getCodeBase(), "audio/beep.au");
          }
      }
    } else {
      play(getCodeBase(), "audio/beep.au");
    }
  }
  void sendContacts() {
    try {
      int n = (int) (7 * Math.random());
      if (n < 1) n = 2;

      String[] nicks = new String[n];
      String[] loginIds = new String[n];

      int i = 0;
      while (i < n) {
        nicks[i] = "random uin #" + i;
        loginIds[i] = "" + (22222 + (int) (10000000 * Math.random()));
        i++;
      }
      plugin.sendContacts(getMyLoginId(), contactListEntry.getText(), nicks, loginIds);
    } catch (Exception ex) {
      printException(ex);
    }
  }
Example #23
0
 public double bond_degree_of_association() {
   double dof;
   // used to calculate strength of bonds of this type
   if ((!clamp_bdoa) && (activation == 100.0)) dof = shrunk_link_length;
   else dof = intrinsic_link_length;
   dof = 100.0 - dof;
   dof = Math.sqrt(dof) * 11.0; // / usually *11.0- but lets see what happens
   if (dof > 100.0) dof = 100.0;
   return dof;
 }
Example #24
0
 public int getHeight() {
   float fHeight;
   int height;
   fHeight = scale * nativeGetMaxDepth();
   height = Math.round(fHeight) + (2 * margin);
   if (parentContainer == null) {
     return height;
   } else {
     return FTAUtilities.max(height, parentContainer.getHeight());
   }
 }
Example #25
0
 public int getWidth() {
   float fWidth;
   int width;
   fWidth = scale * nativeGetMaxWidth();
   width = Math.round(fWidth);
   if (parentContainer == null) {
     return width;
   } else {
     return FTAUtilities.max(width, parentContainer.getWidth());
   }
 }
Example #26
0
File: set.java Project: wcyuan/Set
 public void shuffle() {
   for (int i = 0; i < 3; i++) {
     for (int j = 0; j < num_cards; j++) {
       int k = Math.abs(rand.nextInt() % num_cards);
       /* swap */
       int temp = deck[j];
       deck[j] = deck[k];
       deck[k] = temp;
     }
   }
   current = 0;
 }
Example #27
0
  /** Test client - play an A major scale to standard audio. */
  public static void main(String[] args) {
    // 440 Hz for 1 sec
    double freq = 440.0;
    for (int i = 0; i <= StdAudio.SAMPLE_RATE; i++) {
      StdAudio.play(0.5 * Math.sin(2 * Math.PI * freq * i / StdAudio.SAMPLE_RATE));
    }

    // scale increments
    int[] steps = {0, 2, 4, 5, 7, 9, 11, 12};
    for (int i = 0; i < steps.length; i++) {
      double hz = 440.0 * Math.pow(2, steps[i] / 12.0);
      StdAudio.play(note(hz, 1.0, 0.5));
    }

    // need to call this in non-interactive stuff so the program doesn't
    // terminate
    // until all the sound leaves the speaker.
    StdAudio.close();

    // need to terminate a Java program with sound
    System.exit(0);
  }
Example #28
0
  /**
   * Get the Minimum Y value of all attached DataSets.
   *
   * @return The minimum value
   */
  public double getYmin() {
    DataSet d;
    double min = 0.0;

    if (dataset == null | dataset.isEmpty()) return min;
    for (int i = 0; i < dataset.size(); i++) {
      d = ((DataSet) dataset.elementAt(i));
      if (i == 0) min = d.getYmin();
      else min = Math.min(min, d.getYmin());
    }

    return min;
  }
Example #29
0
  /**
   * Get the Maximum Y value of all attached DataSets.
   *
   * @return The maximum value
   */
  public double getYmax() {
    DataSet d;
    double max = 0.0;

    if (dataset == null | dataset.isEmpty()) return max;
    for (int i = 0; i < dataset.size(); i++) {
      d = ((DataSet) dataset.elementAt(i));
      if (i == 0) max = d.getYmax();
      else max = Math.max(max, d.getYmax());
    }

    return max;
  }
Example #30
0
  void animate() {
    dim = getSize();
    size = (int) (Math.min(dim.height, dim.width) / 2.2);
    timer.tell_time();
    if (timer.time_diff == 0) return; // not enought time has passed, dont animate-crach fix
    dragged_speed = dragged_vec.sub(last_dragged_vec).div(timer.time_diff);
    last_dragged_vec = dragged_vec;
    if (dragged_ball != -1) {
      balls.get2(dragged_ball).pos = dragged_vec.add(find_offset).trim(-1, 1);
      balls.get2(dragged_ball).speed = dragged_speed;
    }

    balls = new WorldAnimate().calc_new_frame(balls, springs, RADIUS, timer);
  }