Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.math; |
| 2 | |||
| 3 | import java.util.Random; |
||
| 4 | import java.util.Vector; |
||
| 5 | |||
| 6 | public class RandomChooser |
||
| 7 | { |
||
| 8 | public class PoolEntry |
||
| 9 | { |
||
| 10 | private int mIndex; |
||
| 11 | |||
| 12 | public PoolEntry(int index) |
||
| 13 | { |
||
| 14 | mIndex = index; |
||
| 15 | } |
||
| 16 | |||
| 17 | public int getIndex() |
||
| 18 | { |
||
| 19 | return mIndex; |
||
| 20 | } |
||
| 21 | } |
||
| 22 | |||
| 23 | private Vector<PoolEntry> mEntries = new Vector<PoolEntry>(); |
||
| 24 | private Random mRandomizer = new Random(); |
||
| 25 | |||
| 26 | public RandomChooser() |
||
| 27 | { |
||
| 28 | } |
||
| 29 | |||
| 30 | public void addEntry(int index, int amount) |
||
| 31 | { |
||
| 32 | for (int i = 0; i < amount; i++) |
||
| 33 | { |
||
| 34 | mEntries.add(new PoolEntry(index)); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | public int chooseEntry(int whichEntry) |
||
| 39 | { |
||
| 40 | if (whichEntry < 0) |
||
| 41 | whichEntry = 0; |
||
| 42 | if (whichEntry >= mEntries.size()) |
||
| 43 | whichEntry = mEntries.size() - 1; |
||
| 44 | |||
| 45 | return mEntries.get(whichEntry).getIndex(); |
||
| 46 | } |
||
| 47 | |||
| 48 | public int chooseEntry() |
||
| 49 | { |
||
| 50 | if (mEntries.size() == 0) |
||
| 51 | { |
||
| 52 | return -1; |
||
| 53 | } |
||
| 54 | |||
| 55 | return chooseEntry(mRandomizer.nextInt(mEntries.size())); |
||
| 56 | } |
||
| 57 | |||
| 58 | /* public void printDebug() |
||
| 59 | { |
||
| 60 | for (int i = 0; i < mEntries.size(); i++) |
||
| 61 | { |
||
| 62 | System.out.println("Entry " + mEntries.get(i).getIndex()); |
||
| 63 | } |
||
| 64 | }*/ |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 |