开发者

How to print objects nested in arrays in java

开发者 https://www.devze.com 2023-02-15 07:13 出处:网络
The card game program I am creating should prompt the user for the number of players, and how many cards in each hand, then shuffle the Deck, then deal cards, then display the cards in each players ha

The card game program I am creating should prompt the user for the number of players, and how many cards in each hand, then shuffle the Deck, then deal cards, then display the cards in each players hand. It will display the Hands for all players unless there are not enough Cards in the Deck (we cannot deal 10 Hands of 7 Cards from a Deck of 52 Cards).

I have created a Card class, a Deck class, a Hand class, and driver to run the program. Since this is an assignment I must stick to the rules given, and so I am only allowed to use arrays, not array lists. My problem is that I cannot figure out how to print the Card objects inside the Hand object, inside the array of Hands. There is also probably a much better way to do this, but I am limited to what I am allowed to import / use. Can I get some help on where to look for printing objects inside nested arrays? I cannot use my method written in the Hand class to add cards to the Hands in an array, due to not having an object name.

EDIT: I have the constructor in the Hand class create a hand sized on the int passed to it. The confusion is that when I get how many players are indeed playing the game from the user, I create an array of type Hand in the driver, which is filled with new Hand objects using a loop. At that point I have no idea how to reference each individual hand object so I can print the contents using toString(). They do not appear to have a name.

Code to follow:

import java.util.Arrays;
import java.util.Scanner;
public class CardsDriver {

public static void main(String[] args) 
{
    Card c1 = new Card();       //create new card object
    Scanner kb = new Scanner(System.in);
    int cards;
    int players;


    System.out.print("How many players are in the game?");
    players = Integer.parseInt(kb.nextLine());
    System.out.print("\nHow many cards will be dealt to each player?");
    cards = Integer.parseInt(kb.nextLine());

    while ((cards * players) > 52)
    {
        System.out.print("There are not enough cards in th开发者_开发问答e deck to deal " +
                         players + " hands of " + cards + " cards. try again.");

        System.out.print("How many players are in the game?");
        players = Integer.parseInt(kb.nextLine());
        System.out.print("\nHow many cards will be dealt to each player?");
        cards = Integer.parseInt(kb.nextLine());
    }

    Deck readyDeck = new Deck();    //create new deck 
    readyDeck.shuffleDeck();        //shuffle the newly built deck using Java.Util.Random   
    Hand[] playerHands= new Hand[players];  //create another array to hold all player hands

    for(int index =0; index < playerHands.length; index++)
    {
        playerHands[index] = new Hand(cards);   //create hand object for each player of the size input by the player
        /*for (int index2 =0; index2<cards;index2++)
        {
            //fill each hand with cards using addcard somehow. i have no object name.
            Hand.addCard(readyDeck.dealACard());
        }*/
    }
}


Assuming you have overridden the toString method of each of the mentioned classes, printing out arrays should be as simple as invoking the Arrays.toString() method (rather than just passing the array reference to System.out.println() and its variants.

In case you are wondering how this handles nested arrays, the toString() method of the Deck class would involve calling the Arrays.toString method on the Card array.

Example:

class Card {

    int suit, rank;

    @Override
    public String toString() {
        // outputs something like ACE of SPADE
        return suitToName(suit) + " of " + rankToName(rank);
    }

}

class Deck {

    private Card[] cards;

    private String name;

    @Override
    public String toString() {
        // prints : MyDeck -> [ACE of SPADE, ACE of HEART] etc.
        return name + " -> " + Arrays.toString(cards);
    }

}

BTW, I think you should enums for Rank and Suits rather than messing around with integers. That would be much cleaner I should say.


as Sanjay suggested you should add this to your Card class :

@Override 
public String toString() {
...
}

Then just use cardinstance.toString() whenever you want to print your Card object no matter where it is.

I don't know what are you trying to accomplish but here is a good example of good programming in java(for creating Texas Hold Em).

http://code.google.com/p/texasholdem-java/source/checkout

Checkout this see how its done properly and you're good to go with any card game.


To override it, you can either make use of the new wizard in the newer versions of Eclipse or use a plugin such as JUtils ToString Generator which I once wrote.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号