简体   繁体   中英

(Java) Card Game Logic

I am trying to create a card game of war. However, for this game, there will be an additional card pile called "trump." If either player 1 or 2 has a card that is references in the trump pile then it is an automatic win regardless of the rank. At the moment, I am stuck with the logic.

In a class called CardPile here is the constructor and the methods.

public CardPile(Card[ ] initialCards)
{
    pile = new ArrayList<Card>();
    for (int i=0; i<initialCards.length; i++)
        pile.add(initialCards[i]);
}

public void add(Card aCard)
{
    pile.add(aCard);
}

public Card get(int index)
{
    return pile.get(index);
}

In my class called TrumpWar

protected CardPile tCard;
protected CardPile cp;
protected CardPile p1;
protected CardPile p2;

public TrumpWar( )
{
    cp = new CardPile (new Card[52]);
    cp.shuffle();

    tCard = new CardPile ();
    for (int i=1; i<7; i++)  //<---Stuck.
        {
             tCard.add(tCard.get(i)); //<---error 
        }

    cp.shuffle();

    p1 = new CardPile(new Card [26]);
    p2 = new CardPile(new Card [26]);
}

When I run the game I am getting a NullPointerException , and I am pretty sure that is because I am not passing anything into the trump pile. When I try to put in an int for the trump ArrayList I would get an error int cannot be converted to Card [] .

How can I get the top six cards from the deck of 52 without removing them just storing them as references, and adding them to the trump pile?

Moreover, am I declaring the player1, player2, and the cardpile correctly?

I greatly appreciate the help, thank you.

You should replace with:

for (int i=0; i<6; i++)  
    {
         tCard.add(cp.get(i));
    }  

You were trying to get cards from the empty tCard .

Note that this code, would still not work until you call cp = new CardPile(array) where array actually contains cards that are not null . Otherwise, tCard.add(cp.get(0)) would not add the reference to the first card, but just null

Card Class:

public class Card {
   Integer i = new Integer(0);
   Card(Integer is) {
    this.i = is;
   }
}

CardPile class:

public class CardPile {
   ArrayList<Card> pile = null;
   public CardPile(Integer no)
   {
     pile = new ArrayList<Card>();
     for (int i=1; i<=no; i++) {
        pile.add(new Card(i));
     }
   }

  public void add(Card aCard)
  {
    pile.add(aCard);
  }

  public Card get(int index)
  {
    return pile.get(index);
  }
}

TrumpWar class:

public class TrumpWar {
  protected CardPile tCard;
  protected CardPile cp;
  protected CardPile p1;
  protected CardPile p2;

  public TrumpWar( )
  {
    cp = new CardPile (52); // only passing the no of cards to be created.
    //cp.shuffle();
    tCard = new CardPile(52); // only passing the no of cards to be created.
    for (int i=1; i<7; i++)  
    {
        tCard.add(tCard.get(i));
    }

    // cp.shuffle();

    p1 = new CardPile(26);
    p2 = new CardPile(26);
  }

  public static void main(String a[]){
    new TrumpWar();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM