Reputation: 17
I'm trying to create a program that asks the user to shuffle or draw from a deck of cards. It compiles, but I'm running into one main problem:
After it prints the shuffled deck or draws a card (based on what command I give), it does not allow the user to perform another command after.
Any help would be greatly appreciated.
Here's the code:
import java.util.Random;
import java.util.Scanner;
public class Deck {
static String [] number = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
static String [] suit = {"Clubs", "Hearts", "Diamonds", "Spades"};
static String [] deck = new String[52];
int cardsUsed = 0;
public static void main (String [] args) {
Deck d = new Deck();
// print d.
for (int i = 0; i<number.length; i++) {
for (int j = 0; j<suit.length; j++) {
deck[13*j+i] = number[i] + " of " + suit[j];
}
}
while (true) {
System.out.println ("shuffle or draw");
Scanner s = new Scanner(System.in);
System.out.println ("What would you like to do?");
String command = s.next( );
if (command.equals("shuffle")) {
d.shuffle();
} else if (command.equals("draw")){
d.draw();
}
s.close();
}
}
public void shuffle () {
int k;
Random shuffle = new Random();
for (k = deck.length-1; k >= 0; k--){
int index = Math.abs((shuffle.nextInt())%52);
String temp = deck[k];
deck[k] = deck[index];
deck[index] = temp;
}
int l;
for (l = 0; l<deck.length; l++){
System.out.println (deck[l]);
}
}
public void draw () {
System.out.print (deck[cardsUsed]);
if (cardsUsed < deck.length) {
System.out.print (deck[cardsUsed]);
} else {
System.out.println ("No cards are left in the deck.");
cardsUsed++;
}
}
}
Upvotes: 0
Views: 102
Reputation: 22481
Read @Ivan answer.
After you have done that, move your Scanner
out of the loop and combine it with some neat Java 7 feature (try-with-resources) in order to solve your problem.
try (Scanner s = new Scanner(System.in)) {
while (true) {
System.out.println ("shuffle or draw");
Scanner s = new Scanner(System.in);
System.out.println ("What would you like to do?");
String command = s.next( );
if (command.equals("shuffle")) {
d.shuffle();
} else if (command.equals("draw")){
d.draw();
}
}
}
Upvotes: 0
Reputation: 134
The reason you are only able to issue one command is because you are closing your scanner. The documentation at http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close() indicates that when you do so, it also closes the underlying stream. So after you've closed your scanner you can't get any more input. You should move your scanner initialization outside of your while loop and never close it.
Upvotes: 3
Reputation: 306
Nice fun program to learn from. When you figure this one out, I highly recommend going back over it and creating a more object-oriented design (ie. make a Card object and work with them)!
It looks like the problem is that you're defining your Deck object incorrectly and then not actually working with it. Note how your shuffling doesn't actually affect the deck itself (it should be d.deck
)! What I'd do is scrap the idea of a Deck object for now, just work directly with one class, a set of data, and methods. Once you have that down, think about how to generalize it using separate Deck objects. Once you have that, try to generalize the cards themselves down to a Card class.
Feel free to ask me questions if you're confused, I'm happy to help.
Upvotes: 0
Reputation: 1256
You could/should consider creating a Card object.
Next, use 2 nested for loops to populate a list. Finally, use Collections.shuffle(list).
for(suit : suits) {
for (rank : ranks) {
listOfCards.add(new Card(suit, rank));
}
}
Collections.shuffle(listOfCards);
class Card {
private final String rank;
private final String suit;
Card(String rank, String suit) {
this.rank = rank;
this.suit = suit;
}
}
Upvotes: 2