Home > other >  How can I temporarily hide a specific index in an array?
How can I temporarily hide a specific index in an array?

Time:05-21

I'm making a black jack game. My AI and Player classes both contain an arraylist that takes in all the cards they currently hold. However, I do not want to reveal the first card the dealer draws until later.

This is what it looks like now on initial set up:

player:
Total: 20
[(TEN, CLUBS), (TEN, SPADES)]

Dealer:
Total: 15
[(NINE, SPADES), (SIX, DIAMONDS)]

I want the Dealer to look more like (xx,xx), (SIX, DIAMONDS) until I decide to reveal it.

The main method where this method passes out the cards initially:

public static void setup(Deck deck, Player player, AI ai) {
    deck.draw(player);
    deck.draw(ai);
    deck.draw(player);
    deck.draw(ai);
    System.out.println("player: \n"   "Total: "   player.getValueOfHand()   "\n"   player.getPlayersCards()   "\n");
    System.out.println("Dealer: \n"   "Total: "   ai.getValueOfHand()   "\n"   ai.getAiCards()   "\n");
}

Deck class which contains draw methods:

/**
 * Draws a card for the player and adds it to players list of cards.
 * @param player the player 
 * @return the card that was drawn out of the deck.
 */
public Cards draw(Player player) {
    Cards card = listOfCards.get(0);
    player.addToPlayersCards(card);
    listOfCards.remove(card);
    pulledCards.add(card);
    return card;
}
/**
 * Draws a card for the ai and adds it to ai list of cards.
 * @param player the dealer 
 * @return the card that was drawn out of the deck.
 */
public Cards draw(AI ai) {
    Cards card = listOfCards.get(0);
    ai.addToAiCards(card);
    listOfCards.remove(card);
    pulledCards.add(card);
    return card;
}

CodePudding user response:

A card couldn't be shared between a dealer and a player, and therefore could carry with itself the state whether it should be visible.

Your code could look like something..

    public class Card {
             Suit suit;
             int Value;
             Boolean visibleToOthers = false; //by default only card owner sees it.
            // constructor, getters, setters
            
              public static Enum Suit {
                   SPADE,
                   HEART,
                   DIAMOND,
                   CLUB
              }
            }
            // other classes and logic
        
            class Application { 
        
                //.. other logic
        
               // dummy display
               public void display(Card card) {
                   if (card.isVisibleToOthers()) System.out.println ("card.getValue()")
                   else{ System.out.println ("X")
                 }

 public void display(List<Card> cards) {
        cards.stream().filter(() -> card.isVisibleToOthers())
        .forEach((card) -> System.out.println(card));
 }
}

CodePudding user response:

You need to store the information somewhere which cards are openly visible and which are still hidden. You could add an boolean to each card that indicates this. You could also add the openly visible cards to another list that is 'the table'. You have a lot of design freedom here.

CodePudding user response:

So you don't show your method: getAiCards() but I think you would want to pass in a boolean as an argument to the method to say if you want to reveal the first card. Like so:

void getAiCards(boolean revealFirstCard) {
    if (!revealFirstCard && aiCards.length > 0) {
        String[][] aiCardsClone = aiCards.clone();
        aiCardsClone[0][0] = "xx";
        aiCardsClone[0][1] = "xx";
        System.out.println(Arrays.deepToString(aiCardsClone));
    } else {
        System.out.println(Arrays.deepToString(aiCards));
    }
}

When you call it and you don't want to reveal the card: getAiCards(false)

When you do want to reveal the first card: getAiCards(true)

  •  Tags:  
  • java
  • Related