여기 간단한 해결책이 있습니다. 나는 그것이 당신이 의도 한 것과 같은 것인지 아닌지를 모른다. Run Code
class GAME {
static class Card {
public enum Shape {
square, triangle, circle
}
public enum Color {
red, blue, green
}
public int count;
public Shape shape;
public Color color;
public int gencode(boolean difcount, boolean difcolor, boolean difshape) {
int gen = -1;
if (difcount == true || difcolor == true || difshape == true) {
gen = 0;
}
if (difcount == true) {
gen = count+1;//for count=0
}
if (difcolor == true && shape != null) {
gen += 1000 * (shape.ordinal() + 1);
}
if (difshape == true && color != null) {
gen += 100000 * (color.ordinal() + 1);
}
return gen;
}
public String toString() {
String s = "Card::properties " + count;
if (color != null) {
switch (color) {
case red:
s += " red";
break;
case blue:
s += " blue";
break;
case green:
s += " green";
break;
default:
}
}
if (shape != null) {
switch (shape) {
case square:
s += " square";
break;
case triangle:
s += " triangle";
break;
case circle:
s += " circle";
break;
default:
}
}
return s;
}
}
public static void add(HashMap<Integer, ArrayList<GAME.Card>> cardmap, GAME.Card card, int key) {
ArrayList<GAME.Card> cardlist = cardmap.get(key);
if (cardlist == null) {
cardlist = new ArrayList<Card>();
cardlist.add(card);
cardmap.put(key, cardlist);
return;
}
cardlist.add(card);
return;
}
public static void showGroups(HashMap<Integer, ArrayList<GAME.Card>> cardmap){
for (List<GAME.Card> value : cardmap.values()) {
if(value.size()<=1) continue;
System.out.println("---------------------------------------------------");
int ii = 0;
for (GAME.Card p : value) {
ii += 1;
System.out.println("" + ii + ") "+p);
}
}
}
public static void main(String[] args) {
HashMap<Integer, ArrayList<GAME.Card>> cardmap = new HashMap< >();
GAME.Card[] allcards = new Card[9];
//init here (simple test
int lennn = GAME.Card.Shape.values().length;
int lennm = GAME.Card.Color.values().length;
Random randomGenerator = new Random();
for (int i = 0; i < allcards.length; i++) {
int ppp=randomGenerator.nextInt(9);
int kkk=randomGenerator.nextInt(2456);
allcards[i] = new Card();
allcards[i].count = ppp ;
allcards[i].shape = GAME.Card.Shape.values()[ppp % lennn];
allcards[i].color = GAME.Card.Color.values()[kkk % lennm];
}
System.out.println("######## Generated random cards");
int ii = 0;
for (GAME.Card p : allcards) {
ii += 1;
System.out.println("" + ii + ") "+p);
}
System.out.println("\n########Group at least by one property");
//group Cards by properties and its values
for (int i = 0; i < allcards.length; i++) {
//check one property
add(cardmap, allcards[i], allcards[i].gencode(true, false, false));
add(cardmap, allcards[i], allcards[i].gencode(false, true, false));
add(cardmap, allcards[i], allcards[i].gencode(false, false, true));
}
//show groups
showGroups(cardmap);
//check two
System.out.println("\n########Group at least by two properties");
cardmap = new HashMap< >();
//group Cards by properties and its values
for (int i = 0; i < allcards.length; i++) {
//check two properties
add(cardmap, allcards[i], allcards[i].gencode(true, true, false));
add(cardmap, allcards[i], allcards[i].gencode(true, false, true));
add(cardmap, allcards[i], allcards[i].gencode(false, true, true));
}
//show groups
showGroups(cardmap);
//check all
System.out.println("\n########Group by all properties");
cardmap = new HashMap< >();
//group Cards by properties and its values
for (int i = 0; i < allcards.length; i++) {
//check all properties
add(cardmap, allcards[i], allcards[i].gencode(true, true, true));
}
//show groups
showGroups(cardmap);
}
}
출처
2014-10-28 12:00:55
qwr
당신은 텍스트 예제와 함께 좀 더 clearify 수 있습니다. – qwr
전체 코드가 아닌 [MCVE] (http://stackoverflow.com/help/mcve)를 추가하십시오. – user1803551
미안 해요 내 첫 stackoverflow에 대한 질문을하고있다. 나는 그것의 더 많은 것을 맑게하려고 노력할 것이다. –