Poker

I need to display my array in 4 rows like this:
♠A ♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K
♦A ♦2 ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q ♦K
❤A ❤2 ❤3 ❤4 ❤5 ❤6 ❤7 ❤8 ❤9 ❤10 ❤J ❤Q ❤K
♣A ♣2 ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣10 ♣J ♣Q ♣K

But instead it is displaying all in 1 row like below and I can't figure out how to separate the rows.
♠A♠2♠3♠4♠5♠6♠7♠8♠9♠10♠J♠Q♠K♦A♦2♦3♦4♦5♦6♦7♦8♦9♦10♦J♦Q♦K❤A❤2❤3❤4❤5❤6❤7❤8❤9❤10❤J❤Q❤K♣A♣2♣3♣4♣5♣6♣7♣8♣9♣10♣J♣Q♣K

the method Display() is where my problem is, it is a for loop that displays all 52 cards on 1 line, how do I make it so it displays 13 cards per row?

package pokergames;
import java.util.Random;

//Problem 1.3
public class DeckOfCards {

private Card[] deck; //declaring the array, didnt initialize because a constructor will initialize it
private int currentCard = 0; //Problem 1.4.4 index that points to your current card on top of the deck
public static final int totalCards = 52; //total number of cards in this deck (had to be initialized because it is final)
private String[] face = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; //Problem 1.4.1 string is the array of cards

private static final char spade = (char)'\u2660';
private static final char diamond = (char)'\u2666';
private static final char heart = (char)'\u2764';
private static final char club = (char)'\u2663';

private char[] suit = {spade,diamond,heart,club}; //Problem 1.4.2 array for the suit

private static final Random randomNumbers = new Random(); 

public DeckOfCards() //Problem 1.4 constructor
{
    deck = new Card[totalCards]; //initializing the Card[] array with the totalCards (52)

    for(int i = 0; i<this.totalCards; i++) //1.4.5
    {
        //divided by 13 because there are only 13 of each suit
        deck[i] = new Card(face[i%13], suit[i/13]);
    }


}

public void Display() //Problem 1.6
{
    for(int i = 0; i < this.totalCards; i++)
    {
        System.out.print(deck[i].toString());                       

    }

    System.out.println("");
}

public void shuffle() //problem 1.5
{
    Card tempCard; 
    int secondCardIndex;
    for(int first = 0; first < deck.length; first++)
    {
        tempCard = deck[first];
        //put the totalcards in there so it will generate a random number from 0-51
        secondCardIndex = randomNumbers.nextInt(totalCards);

        //Swap
        deck[first] = deck[secondCardIndex];
        deck[secondCardIndex] = tempCard;

    }
}

public void sort()
{
    for(int i=0; i<deck.length; i++)
    {
        //this shows that it is putting the max numbers at the beginning sorting from largest to smallest
        System.out.printf("we are searching deck index from %d to %d \n ", i, deck.length);
        this.findMaxCard(deck, i);

    }
}

//this goes through the whole deck and finds the biggest number and puts it
//in the first element of the start point
//we will call this functiojn in our sort function
public void findMaxCard(Card[] deck, int startIndex)
{
    Card tempCard;
    Card maxCard = deck[startIndex];

    for(int i = startIndex; i<deck.length; i++)
    {
     if(deck[i].toInt() > maxCard.toInt())   
     {
         //swap
         maxCard = deck[i];
         tempCard = deck[startIndex];
         deck[startIndex] = maxCard;
         deck[i] = tempCard;
     }
  }

}

}

Comments

  • a loop doesn't have to start from zero - so you can have 4 for loops each starting off where the last one ends... print out your cards based on the index of each loop...

    another way is to just check for certain index values in the loop, and println then.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion