卡片组JAVA [英] Deck of cards JAVA

查看:93
本文介绍了卡片组JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一副卡片,可以处理每张卡片和一套西装,直到没有剩余卡片为止。对于我的项目,我需要将其拆分为3个类,其中包括一个驱动程序类。我首先用一切创建了一个类,所以我知道如何使它全部工作。

I have created my deck of cards that deals every card and a suit until there is no card remaining. For my project, I need to split it up into 3 classes which includes a driver class. I first created one class with everything so I knew how to make it all work.

public class DeckOfCards2 {
  public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    // Initialize cards
    for (int i = 0; i < deck.length; i++) {
      deck[i] = i;
    }

    // Shuffle the cards
    for (int i = 0; i < deck.length; i++) {
      int index = (int)(Math.random() * deck.length);
      int temp = deck[i];
      deck[i] = deck[index];
      deck[index] = temp;
    }

    // Display the all the cards
    for (int i = 0; i < 52; i++) {
      String suit = suits[deck[i] / 13];
      String rank = ranks[deck[i] % 13];
      System.out.println( rank + " of " + suit);
    }
  }
}

现在尝试将其拆分分为3个班级。我在DeckOfCards类的所有deck / suit变量上得到红色sqiggle行。我不知道如何解决它。

Now trying to split it up into 3 classes. I am getting red sqiggle lines on ALL my deck/suit variables on my DeckOfCards class. I dont know how to fix it.

public class DeckOfCards {
  private Card theCard;
  private int remainingCards = 52;

  DeckOfCards() {
    theCard = new Card();   
  }

  public void shuffle(){
    for (int i = 0; i < deck.length; i++) {
       int index = (int)(Math.random() deck.length);
       int temp = deck[i];
       deck[i] = deck[index];
       deck[index] = temp;
       remainingCards--;
     }
  }

  public void deal(){
    for (int i = 0; i < 52; i++) {
       String suit = suits[deck[i] / 13];
       String rank = ranks[deck[i] % 13];
       System.out.println( rank + " of " + suit);
       System.out.println("Remaining cards: " + remainingCards);
     }
   }
}

卡类:

public class Card {
  int[] deck = new int[52];
  String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
  String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

  Card() {
    for (int i = 0; i < deck.length; i++) {
      deck[i] = i;
    }
  }
}

经销商类

public class Dealer {
  public static void main(String[]args){
    System.out.println("The deck will randomly print out a card from a full deck each time");

    DeckOfCards player = new DeckOfCards();
    player.deal();
  }
}


推荐答案

As其他人已经说过,你的设计不是很清晰,面向对象。

As somebody else already said, your design is not very clear and Object Oriented.

最明显的错误是你的设计中卡片知道卡片组。 Deck应该知道卡片并在其构造函数中实例化对象。例如:

The most obvious error is that in your design a Card knows about a Deck of Cards. The Deck should know about cards and instantiate objects in its constructor. For Example:

public class DeckOfCards {
    private Card cards[];

    public DeckOfCards() {
        this.cards = new Card[52];
        for (int i = 0; i < ; i++) {
            Card card = new Card(...); //Instantiate a Card
            this.cards[i] = card; //Adding card to the Deck
        }
     }

之后,如果你希望你也可以扩展Deck以建立不同的Deck of Cards(例如,超过52张牌,Jolly等)。例如:

Afterwards, if you want you can also extend Deck in order to build different Deck of Cards (for example with more than 52 cards, Jolly etc.). For Example:

public class SpecialDeck extends DeckOfCards {
   ....

我要改变的另一件事是使用String数组来表示套装和等级。从Java 1.5开始,该语言支持Enumeration,它非常适合这类问题。例如:

Another thing that I'd change is the use of String arrays to represent suits and ranks. Since Java 1.5, the language supports Enumeration, which are perfect for this kind of problems. For Example:

public enum Suits {
    SPADES, 
    HEARTS, 
    DIAMONDS,
    CLUBS;  
}

使用Enum可以获得一些好处,例如:

With Enum you get some benefits, for example:

1)枚举是类型安全的,你不能将除预定义的枚举常量之外的任何东西分配给枚举变量。例如,你可以写下你的卡的构造函数如下:

1) Enum is type-safe you can not assign anything else other than predefined Enum constants to an Enum variable. For Example, you could write your Card's constructor as following:

public class Card {

   private Suits suit;
   private Ranks rank;

public Card(Suits suit, Ranks rank) {
    this.suit = suit;
    this.rank = rank;
}

这样你就可以建立一致的卡片,只接受你的枚举。

This way you are sure to build consistent cards that accept only values ​​of your enumeration.

2)您可以在Switch语句中使用Enum in int或int原始数据类型(这里我们不得不说,因为Java 1.7上的switch语句也允许在String上)

2) You can use Enum in Java inside Switch statement like int or char primitive data type (here we have to say that since Java 1.7 switch statement is allowed also on String)

3)在Java中使用Enum添加新常量非常简单,您可以在不破坏现有代码的情况下添加新常量。

3) Adding new constants on Enum in Java is easy and you can add new constants without breaking existing code.

4)您可以遍历Enum,这在实例化卡片时非常有用。例如:

4) You can iterate through Enum, this can be very helpful when instantiating Cards. For Example:

/* Creating all possible cards... */
for (Suits s : Suits.values()) {
    for (Ranks r : Ranks.values()) {
         Card c = new Card(s,r);
    }  
}

为了不再发明轮子,我' d也改变了你将卡从数组保存到Java Collection的方式,这样你就可以在你的套牌上使用很多强大的方法,但最重要的是你可以使用 Java Collection的shuffle功能 to洗牌你的甲板。例如:

In order to not invent again the wheel, I'd also change the way you keep Cards from array to a Java Collection, this way you get a lot of powerful methods to work on your deck, but most important you can use the Java Collection's shuffle function to shuffle your Deck. For example:

private List<Card> cards = new ArrayList<Card>();

//Building the Deck...

//...

public void shuffle() {
    Collections.shuffle(this.cards); 
}

这篇关于卡片组JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆