我最近开始学习 java 并且大部分都进展顺利。为了帮助理解我正在学习的内容,我决定编写一个纸牌游戏引擎。我使用我认为正确的抽象和封装方法构建了卡片和甲板类。但是,在构建经销商类时,我遇到了一个问题,我不知道如何从经销商类中访问卡类数据。从甲板类我可以访问卡类所需的所有权限,但是一旦我进入经销商类,我就不确定如何访问封装在 2 个深度类中的数据。
这是我到目前为止所拥有的,虽然可以解决问题的代码表示赞赏,但如果有人可以指出我的文档或书籍的方向以帮助更好地理解我所经历的,将不胜感激。
只是要清楚。我希望能够访问与甲板数组中的各个卡片有关的数据,例如来自经销商类的实例变量值。我知道如果我从 println 语句中引用卡片,它会调用卡片到字符串方法。但我正在寻找的是卡片的实际数据。
最后一节课只是我用来测试我写的方法的东西。
import java.util.*;
public class Card {
public Card() {
suit = 0;
value = 0;
setName(value);
}
public Card(int cSuit, int cValue) {
suit = cSuit;
value = cValue;
setName(cValue);
}
public String getNewCard() {
suit = rgen.nextInt(4)+1;
value = rgen.nextInt(13)+1;
setName(value);
return name +" of " +getSuitAsString();
}
public int getValue() {
return value;
}
public int getSuit() {
return suit;
}
public String getName() {
setName(value);
return name;
}
public String getName(int val) {
setName(val);
return name;
}
public String getSuitAsString() {
switch(suit) {
case 1: return "Diamonds";
case 2: return "Hearts";
case 3: return "Spades";
case 4: return "Clubs";
default: return null;
}
}
public String getSuitAsString(int cSuit) {
switch (cSuit) {
case 1: return "Diamonds";
case 2: return "Hearts";
case 3: return "Spades";
case 4: return "Clubs";
default:return null;
}
}
public String toString() {
return name +" of "+ getSuitAsString();
}
private void setName(int value) {
switch (value) {
case 0 : name = "null";
break;
case 1 : name = "Ace";
break;
case 2 : name = "Two";
break;
case 3 : name = "Three";
break;
case 4 : name = "Four";
break;
case 5 : name = "Five";
break;
case 6 : name = "Six";
break;
case 7 : name = "Seven";
break;
case 8 : name = "Eight";
break;
case 9 : name = "Nine";
break;
case 10 : name = "Ten";
break;
case 11: name = "Jack";
break;
case 12 : name = "Queen";
break;
case 13 : name = "King";
break;
}
}
private int suit;
private String name;
private int value;
private Random rgen = new Random();
}
import java.util.Random;
public class Deck {
//Constructor assembles an initial deck of 52 cards in order by suit.
//Array element [0] is never used.
public Deck(){
int cards = 1;
int cardsPerSuit = 13;
int suits = 4;
while(cards < DECK_ARRAY_SIZE){
for(int i = 1; i <= suits ; i++){
for(int j = 1; j <= cardsPerSuit; j++){
deck[cards++] = new Card(i , j);
}
}
}
}
// Constructor creates and empty deck of 53 indexes set to null.
// Array element [0] is never used.
public Deck(int deckArraySize){
for(int i = 1; i < deckArraySize ; i++){
deck[i] = new Card();
}
}
public Deck(int suitCount , int cardsPerSuit , int deckArraySize){
// Constructor for special deck configuration.
}
public void shuffle(){
int SHUFFLE_COUNT = 100000;
int arrayPos1 = 0;
int arrayPos2 = 0;
int count = 0;
while(count < SHUFFLE_COUNT){
Card card1 = deck[rgen.nextInt(DECK_ARRAY_SIZE)];
if(card1 == deck[0]) card1 = deck[1];//This prevents the NullPointerException error.broke the always use braces rule.
Card card2 = deck[rgen.nextInt(DECK_ARRAY_SIZE)];
if(card2 == deck[0]) card2 = deck[52];//This prevents the NullPointerException error.broke the always use braces rule.
for(int i = 1; i < DECK_ARRAY_SIZE; i++){
for (int j = 1; j < DECK_ARRAY_SIZE; j++){
if (deck[i].equals(card1)){
arrayPos1 = i;
if (deck[j].equals(card2)){
arrayPos2 = j;
}
}
}
Card temp = deck[arrayPos1];
deck[arrayPos1] = deck[arrayPos2];
deck[arrayPos2] = temp;
count++;
}
}
}
// Gets the top card of the deck.
public Card getTopCard(){
Card topCard = deck[1];
return topCard;
}
// Gets a card at specified index.
public Card getCardAt(int cardAtPos){
Card cardAt = deck[cardAtPos];
return cardAt;
}
//This method makes an implicit call to the Card classes toString method when string manipulation is performed .
//This is done by the compiler automatically.
public void getDeckInfo(){
for(int i = 1; i < DECK_ARRAY_SIZE ; i++){
System.out.print(i +" ");
System.out.println(deck[i]);
}
}
public String toString(){
// getDeckInfo();
return "Nothing to see here, move along.";
}
private Random rgen = new Random();
public static final int DECK_ARRAY_SIZE = 53;
private Card[] deck = new Card[DECK_ARRAY_SIZE];
}
public class Dealer {
public Dealer(){
playDeck = new Deck();
discardStack = new Deck(Deck.DECK_ARRAY_SIZE);
}
public int getDeckCardCount(){
// this count should go down anytime a card is dealt to a player
return deckCardCount;
}
public int getDiscardCount(){
// this count should go up anytime a card is removed from the deck or from play.
return discardCount;
}
public void dealCards(){
// should be self explanatory.
}
public void shuffle(){
// need to make sure that shuffle compensates for cards removed
// from deck and move cards to the front of the array before the shuffle.
// make sure that the empty indexes at the end of the array are not included in the shuffle.
// also need a shuffle method for shuffles at the end of the game. or just
// throw out he deck and get a new deck....
// maybe pass the remaining cards in the deck to the shuffle method .
}
public String toString(){
return "Nothing to see here , move along.";
}
public Deck playDeck;
public Deck discardStack;
private int deckCardCount;
private int discardCount;
}
public class CardTest {
public static void main(String[] args){
Card singleCard = new Card();
Deck deck = new Deck();
Deck playDeck = new Deck();
Deck discardStack = new Deck(53);
Dealer dealer = new Dealer();
System.out.println("value: "+singleCard.getValue());
System.out.println("Name: "+singleCard.getName());
System.out.println("Suit: "+singleCard.getSuitAsString());
System.out.println("Card to string: "+singleCard.toString());
System.out.println("New card: " +singleCard.getNewCard());
System.out.println("New card: " +singleCard.getNewCard());
System.out.println("New card: " +singleCard.getNewCard());
System.out.println("Pass a 4 to the getSuitAsString() method: "+singleCard.getSuitAsString(4));
System.out.println("Pass a 12 to the getName() method: "+singleCard.getName(12));
deck.getDeckInfo();
System.out.println("Top card is: "+deck.getTopCard());
System.out.println("Shuffling...");
int count =0;
while(count < 500){
dealer.playDeck.shuffle();
discardStack.shuffle();
count++;
}
deck.getDeckInfo();
System.out.println("Top card is: "+deck.getTopCard());
System.out.println("Card at position ??: "+deck.getCardAt(5));
playDeck.getDeckInfo();
discardStack.getDeckInfo();
playDeck.shuffle();
playDeck.getDeckInfo();
discardStack.shuffle();
discardStack.getDeckInfo();
dealer.playDeck.getDeckInfo();
dealer.playDeck.shuffle();
System.out.println("Shuffling...");
dealer.playDeck.getDeckInfo();
System.out.println(dealer.playDeck.getCardAt(5));
System.out.println(dealer.discardStack.getCardAt(5));
}
}