我正在为一个学校项目制作一个小游戏。在我的游戏中,我在3x3字段上有9张牌,每张牌都有3个属性:计数,形状和颜色。当3张卡分享相同或不同的属性时,你有一套,所以当所有颜色相同或不同,所有三种形状相同或不同,所有颜色相同或不同时。
例如:
3个优先事项必须相同,必须有所不同。如上所示,第一张卡具有相同的数量,不同的形状和相同的颜色。
现在我必须创建一个函数来检查表上有多少个集合。我有一个功能,将检查3张牌是否是一组,但是我无法制作能够检查场上3张牌的所有可能组合的功能。该字段上的卡片以二维数组保存。
我的节目是荷兰语:
setsOpTafel
==设置在表格kaarten
== cards aantalSets
==套数http://pastie.org/private/zqkfhqew1q8kwqvi6cvm0w
有人可以帮忙吗?
编辑: 如何运作必须工作。 所以首先我必须检查3张卡的组合 - >卡1,卡2,卡3。 然后是卡1,卡2,卡4等,直到它检查了3张不同卡的所有不同组合。
答案 0 :(得分:0)
这是一个简单的解决方案。我不知道这是不是你想要的。 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);
}
}