Beispiel #1
0
  // Shuffles Arraylist like a deck of cards
  private static ArrayList<Restaurant> shuffle(ArrayList<Restaurant> places) {
    int j = 0;
    Restaurant temp = null;

    // Randomly swaps one index with another
    for (int i = 0; i < places.size(); i++) {
      j = ThreadLocalRandom.current().nextInt(0, i + 1);
      temp = places.get(j);
      places.set(j, places.get(i));
      places.set(i, temp);
    }
    return places;
  }
  /**
   * React to the pushing of the given button
   *
   * @param index The number of the button pushed
   */
  private void slide(int index) {

    // Don't do anything if the puzzle is solved
    if (solved) return;

    // if not a valid click return
    if (!check(index)) {
      return;
    }

    // swap positions
    current.set(index, current.set(hiddenIndex, current.get(index)));

    // swap strings
    JButton b = (JButton) buttons.get(index);
    b.setText((String) current.get(index));
    b.setIcon(icons[Integer.parseInt(current.get(index)) - 1]);
    b.setVisible(false);
    b = (JButton) buttons.get(hiddenIndex);
    b.setText((String) current.get(hiddenIndex));
    b.setIcon(icons[Integer.parseInt(current.get(hiddenIndex)) - 1]);
    b.setVisible(true);

    // update the position of the blanked spot
    hiddenIndex = index;

    // Increment the number of moves and update status
    moves++;
    getStatusLabel().setText("Number of moves: " + moves);

    // if you've won
    if (current.equals(correct)) {
      solved = true;
      getStatusLabel().setText("Solved the game in " + moves + " moves.");
      // Change the buttons colors to green
      Iterator itr = buttons.iterator();
      while (itr.hasNext()) {
        ((JButton) itr.next()).setBackground(java.awt.Color.green);
      }
    }
  }
 private ArrayList<Integer> findRanges() {
   int rowsPerRange = (int) Math.ceil((grid.size()) / (double) threads);
   ArrayList<Integer> ranges = new ArrayList<Integer>();
   int startRow = 0, endRow = rowsPerRange;
   for (int i = 0; i < threads; i++) {
     ranges.add(startRow);
     ranges.add(endRow);
     startRow = endRow;
     ;
     endRow += rowsPerRange;
   }
   ranges.set(ranges.size() - 1, Math.min(grid.size(), ranges.get(ranges.size() - 1)));
   System.out.println(ranges);
   return ranges;
 }
Beispiel #4
0
 /*
  * this method return a new file that contain the new format of the protein sequence.
  */
 private void CreatedTranslatedText(Reader rd) {
   ArrayList<String> allData = new ArrayList<String>();
   Scanner scanner = new Scanner(rd);
   while (scanner.hasNext()) {
     allData.add(scanner.next());
   }
   ArrayList<String> ID = new ArrayList<String>();
   String key = "*";
   for (int i = 0; i < allData.size(); i++) {
     if (i % 8 == 0) {
       key = allData.get(i);
       if (ID.contains(key) != true) {
         ID.add(key);
         ID.add("*");
         ID.add(allData.get(i + 1));
       } else {
       }
     } else {
     }
   }
   /*
    * i want to use array here, because i can actually know the number of elements that store
    * in the array,but the type of the elements is String.if i want to do so,i must create a
    * new class that contain ID.but here i just use arraylist.
    */
   for (int i = 0; i < ID.size() / 3; i++) {
     for (int j = 0; j < allData.size() / 8; j++) {
       if (ID.get(3 * i).equals(allData.get(8 * j))) {
         ID.set(3 * i + 1, ID.get(3 * i + 1) + ";" + allData.get(8 * j + 2));
       }
     }
   }
   try {
     PrintWriter wr = new PrintWriter(new FileWriter("FASTA-R.elm"));
     for (int i = 0; i < ID.size() / 3; i++) {
       wr.println(ID.get(3 * i) + "@" + ID.get((3 * i + 1)));
       wr.println(ID.get(3 * i + 2));
     }
   } catch (IOException ex) {
   }
 }