/** * Generate a random Secret * * @param secretSize */ public Combination generate(int secretSize) { Color[] colortab = new Color[secretSize]; Random rand = new Random(); for (int i = 0; i < secretSize; i++) { int index = rand.nextInt(Color.values().length); colortab[i] = Color.values()[index]; } return new Combination(colortab); }
public static void main(String[] args) { System.out.println("enum as array:"); System.out.println(" " + Arrays.toString(Color.values()) + "\n"); System.out.println("All enum constants:"); for (Color c : Color.values()) { System.out.println(" " + c.name() + "=" + c.ordinal()); } System.out.println("\nenum from a String:"); String color = "YELLOW"; Color c = Color.valueOf(color); System.out.println( " " + c.name() + "=" + c.ordinal() + " RGB=0x" + Integer.toHexString(c.getRGB())); System.out.println("\nenum from unknown String:"); String color2 = "yellow"; try { Color c2 = Color.valueOf(color2); // throws "IllegalArgumentException" System.out.println( " " + c2.name() + "=" + c2.ordinal() + " RGB=0x" + Integer.toHexString(c2.getRGB())); } catch (IllegalArgumentException iae) { System.out.println(" \"" + color2 + "\" not found in enum."); } // Sample of mapping an int to an enum: EnumSample service = new EnumSample(); switch (Index.valueOf(service.getIndex())) { case ONE: System.out.println("ONE"); break; case TWO: System.out.println("TWO"); break; case INVALID: System.out.println("INVALID"); break; case REG: break; case THREE: break; case ZERO: break; default: break; } }
public Board(int numPlayers) { assert numPlayers > 0; coordinates = new HashMap<>(); for (int i = 0; i < numPlayers; i++) { coordinates.put(Color.values()[i], new HashSet<>()); } }
/** @param Random 是否随机 */ public Robot(String robot_name, boolean Random) { this.name = robot_name; if (Random) { this.position = new robotPosition(true); this.sight = new robotSight(true); switch ((int) (Math.random() * Color.values().length)) { case 0: this.color = Color.BLACK; break; case 1: this.color = Color.BLUE; break; case 2: this.color = Color.WHITE; break; case 3: this.color = Color.RED; break; case 4: this.color = Color.ORANGE; break; case 5: this.color = Color.GREEN; break; case 6: this.color = Color.YELLOW; break; case 7: this.color = Color.CRAY; break; default: this.color = Color.DEFAULT; break; } } else { this.position = new robotPosition(false); this.sight = new robotSight((int) (Math.random() * 360)); this.color = Color.DEFAULT; } }
private int other() { return (value + 1) % (Color.values().length - 1); }
public Color notTake() { return Color.values()[this.other()]; }
public Color take() { return Color.values()[value]; }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub for (Color myColor : Color.values()) { System.out.println(myColor); } }