2014-10-28 4 views
0

저는 학교 프로젝트를위한 작은 게임을 만들고 있습니다. 내 게임에서 나는 3x3 필드에 9 개의 카드를 가지고 모든 카드는 카운트, 모양색상과 같은 3 개의 속성을가집니다. 3 장의 카드가 같거나 다른 속성을 공유 할 때 세트가 있습니다. 그래서 모든 색상이 같거나 다를 때 세 가지 모양 모두 동일하거나 다를 때 모든 색상이 동일하거나 다를 수 있습니다. 예를 들어3 문자열의 조합 비교 수행

:

  • 1 적색 삼각형, 사각형 레드 1, 1 개 붉은 타원.
  • 1 블루 삼각형 2 녹색 삼각형

3 개 우선 순위가 상이 할 필요가 동일해야한다. 위에서 보았 듯이 첫 번째 카드의 개수, 모양 및 색상이 동일합니다.

이제 테이블에 얼마나 많은 세트가 있는지 확인해야하는 기능을 만들어야합니다. 3 장의 카드가 세트인지를 확인하는 기능이 있지만, 3 장의 카드 조합을 모두 확인할 수있는 기능을 만들 수는 없습니다. 필드의 카드는 2 차원 배열로 저장됩니다.

내 프로그램은 네덜란드에 있습니다 테이블에

  • setsOpTafel == 세트
  • kaarten == 카드
  • 세트의
  • aantalSets == 수

http://pastie.org/private/zqkfhqew1q8kwqvi6cvm0w

누군가 제발 도와 줄 수 있니?

편집 : 작동 방법은 작동해야합니다. 먼저 카드 3 장, 카드 2 장, 카드 3 장의 콤보를 확인해야합니다. 그런 다음 카드 1, 카드 2, 카드 4 등 3 가지 다른 카드 조합을 모두 점검 할 때까지 계속하십시오.

+0

당신은 텍스트 예제와 함께 좀 더 clearify 수 있습니다. – qwr

+1

전체 코드가 아닌 [MCVE] (http://stackoverflow.com/help/mcve)를 추가하십시오. – user1803551

+0

미안 해요 내 첫 stackoverflow에 대한 질문을하고있다. 나는 그것의 더 많은 것을 맑게하려고 노력할 것이다. –

답변

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); 

    } 

}