java - Not displaying cards -
i creating instance deck of cards should display 52 cards when built, should shuffle cards , deal 5. after menu should prompt either 5 dealt, or deck reshuffled, adds drawn cards in, or exit application. however, when run app, displays null 52 cards, , each card dealt. application worked , met specification before talked instructor, , have got during/ after our talk. i'm not sure i'm doing wrong , use help. clarification post requirements, code. appreciate help.
at startup, constructs deck of cards 52 distinct cards running ace - king, of suit heart, club, diamond, or spade shuffle cards!! proof, print out 52 cards console in useful, readable way. then, deal top 5 cards console (meaning print them console) after of this, allow user choose between dealing next 5 cards, reshuffling deck, or quitting application. if user chooses deal next 5 cards, based on cards have not yet been dealt. not reshuffle. if user chooses reshuffle, repeat process of shuffling, printing deck, , dealing top 5 cards. if user chooses quit application, end app. the code i'm practicing encapsulation post each class broken in eclipse.
driver
public class driver { public static void main(string[] args) throws ioexception { deckrun.run(); } } deckrun
public class deckrun { static card[] d1 = new card[52]; public static void run() throws ioexception { printdeck(); system.out.println(""); deal.dealcards(d1); system.out.println(""); menu(); } public static void printdeck() { (card c : d1) { system.out.println(c); } } public static void menu() throws ioexception{ bufferedreader readracer = new bufferedreader(new inputstreamreader(system.in)); int menu = 0; { system.out.println("press 1 dealt 5 random cards."); system.out.println("press 2 shuffle cards deck."); system.out.println("press 3 quit application."); string input = readracer.readline(); menu = integer.parseint(input); switch (menu) { case 1: deal.dealcards(d1); break; case 2: system.out.println("the deck has been shuffled."); deck[] d1 = new deck[52]; break; case 3: system.out.println("i'm not bad, i'm drawn way."); break; } } while (menu != 3); } } card
public class card { private rank rank; // variable assign card rank. private suit suit; // variable assign card suit. public card (rank rank, suit suit) { // constructor build card. this.rank = rank; this.suit = suit; } public rank getrank() { // retrieves card's rank enum rank. return rank; } public suit getsuit() { // retrieves card's suit enum suit. return suit; } @override public string tostring() { // return rank + " of " + suit; } } deck
public class deck { private card[] cards; public deck() { int numberofranks = 13; int numberofsuits = 4; int numberofcards = numberofranks * numberofsuits; rank[] rank = rank.values(); suit[] suit = suit.values(); cards = new card[numberofcards]; (int = 0; < numberofranks; i++) { (int j = 0; j < numberofsuits; j++) { cards[j * numberofranks + i] = new card(rank[i], suit[j]); } } } public void shufflecards() { random rand = new random(); (int c = rand.nextint(6) + 5; c > 0; c--) { (int = cards.length - 1; > 0; i--) { int index = rand.nextint(i + 1); card card = cards[index]; cards[index] = cards[i]; cards[i] = card; } } } } deal
public class deal { private static int counter = 0; public static void dealcards(card[] d1) { (int = 0; < 5; i++) { counter++; system.out.println(d1[counter]); if (counter == 50) { system.out.println("almost cards have been used, please reshuffle."); } } } } rank (enum)
public enum rank { ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, } suit (enum)
public enum suit { clubs, diamonds, hearts, spades, } not sure issue is, i've changed today after realizing need instance of deck, professor helped me understand instead of method deck of cards in class deck, needed constructor. believe i'm going down right path utterly stumped. appreciated , if can explain errors may learn , progress more thanks.
in main calling deckrun.run(), let's take @ definition of method. calls printdeck iterates on array of cards (d1), never populated. that's why getting bunch of nulls.
Comments
Post a Comment