【问题标题】:Proccessing compare combination of 3 Strings处理 3 个字符串的比较组合
【发布时间】:2014-10-28 09:52:46
【问题描述】:

我正在为一个学校项目制作一个小游戏。在我的游戏中,我在 3x3 区域上有 9 张卡片,每张卡片都有 3 个属性:count、shape 和 color。当 3 张卡片具有相同或不同的属性时,您就有了一套,因此当所有颜色相同或不同且所有三种形状相同或不同且所有颜色相同或不同时。

例如:

  • 1 个红色三角形、1 个红色矩形、1 个红色椭圆。
  • 1 个蓝色三角形,2 个绿色三角形

三个优先级必须相同或不同。正如您在上面看到的,第一张卡片具有相同的数量、不同的形状和相同的颜色。

现在我必须创建一个函数来检查桌子上有多少套。我有一个功能可以检查 3 张牌是否是一组,但我无法创建一个功能来检查场上 3 张牌的所有可能组合。场上的卡片保存在二维数组中。

我的程序是荷兰语:

  • setsOpTafel == 放在桌子上
  • kaarten == 卡片
  • aantalSets == 套数

http://pastie.org/private/zqkfhqew1q8kwqvi6cvm0w

有人可以帮忙吗?

编辑: 如何运作必须发挥作用。 所以首先我必须检查 3 张卡片的组合 -> 卡片 1、卡片 2、卡片 3。 然后是卡片 1、卡片 2、卡片 4,以此类推,直到它检查了 3 张不同卡片的所有不同组合。

【问题讨论】:

  • 您能否通过文本示例更清楚一点。
  • 请添加 MCVE 而不是您的完整代码。
  • 抱歉,这是我第一次在 stackoverflow 上提问。我会尽量澄清它。
  • 不,我必须从场上的 9 张牌中比较不同牌的所有可能组合。 1,2,3 和 1,3,2 是一样的,所以只能算 1 种可能性

标签: java for-loop compare processing


【解决方案1】:

这是简单的解决方案。我不知道这是否如你所愿。 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);

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2019-09-19
    • 2012-08-16
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多