2

I'm trying to write a code for class that does the following:

  • Asks how many decks to use
  • Program produces a single unused card, from the deck(s) each time enter is pressed
  • Program notifies the user when there are no more cards to deal
  • Program Allows the user to play again
  • Methods are used extensively

So far, my code looks like this:

import java.util.*;
public class IA3 {

@SuppressWarnings({ })
public static void play () {
}
@SuppressWarnings("resource")
public static void main(String[] args) {
    boolean draw = true;
    boolean pa = true;
    Scanner console = new Scanner(System.in);
    // TODO Auto-generated method stub
    System.out.println("Hello! Please input the number of decks you would like to use:");
    int decks = console.nextInt();

    int t = decks * 52;
    System.out.println("Your total amount of cards to use are " +t);
    int a = 0;
    do {
        int[] deck = new int[t];
        //declaration of suits and ranks
        String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

        //Initializing of the cards
        for (int i = 0; i < t; i++) {
        deck[i] = i;
                }

            System.out.println("Press enter to draw a random card : "); //allows player to prompt the system for the next card

            String input = console.nextLine();
            a++;
            if (input.equals("")) {
                // Shuffles the cards
                for (int i = 0; i < 1; i++) {
                  int index = (int)(Math.random() * t);
                  int temp = deck[i];
                  deck[i] = deck[index];
                  deck[index] = temp;
                }

                      for (int i = 0; i < 1; i++) {
                      String suit = suits[deck[i] / 13];
                      String rank = ranks[deck[i] % 13];
                      System.out.println(rank + " of " + suit);
                      }
             if (a>t) {
                 System.out.println ("No more cards available");
                 break;
             }
                      System.out.println("Draw another card? (Yes or No) : ");
                      String again = console.nextLine();
                      if (again.equals("Yes")) {
                          continue;
                      }
                      if (again.equals("No")) {
                          draw = false;
                          System.out.println("Game over!");
                      }
                        System.out.println("Play again? (Yes or No) : ");
                          String plag = console.nextLine();
                          if (plag.equals("Yes")) {
                              continue;
                          }
                          if (plag.equals("No")) {
                              pa = false;
                              System.out.println("Thank you for playing!");
                              break;
                          } while (pa == true);
                  }
        } while (draw == true);
    }
}

Issues arise when I try to run more than 1 deck, sometimes failing immediately, while sometimes running maybe 4 deals then failing. I also cannot seem to get it to run again; it just terminates whenever I input "Yes" instead of playing again. As you can see, I have no methods in there (I am SO LOST on methods). Any help would be appreciated, thank you!

Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82

2 Answers2

1

You should post full error message. Without it it's hard to guess what is happening. I see at least one place where array index can be out of boundaries:

String suit = suits[deck[i] / 13];

Result of this devision will be greater than 3 if you have more than 1 deck. You can fix it using:

String suit = suits[(deck[i] / 13) % 4];

This way you ensure than only elements with indexes [0,1,2,3] will be used.

Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82
  • That part worked; thank you! Would you happen to know how I can input methods, and get the play again function to work? – user3750472 Jun 17 '14 at 23:49
0

Can't post a comment yet, so here are a few ideas to make this code easier to follow, especially that your question is asking for "any help."

Remove for loops that look like this:

for (int i = 0; i < 1; i++) {
 // code
}

The above for loop sets i to 0, then executes the internal code, then increments i (so i is now equal to 1), then checks to see if i is less than 1 (which it is not because it equals 1 now), so it does not execute the internal code again. So in other words, this type of for loop is not necessary because it can only ever execute its internal code once. The surrounding for loop should be removed and the internal code left intact.

It may be simpler to store your deck as a ArrayList, instead of an array. The advantage of this is that you can take advantage of operations like .remove(int) and .size(), and you wouldn't have to worry about shuffling your deck (you could just randomly remove an entry and print it out).

Hope that gives you a few pointers in the right direction. Keep it up!

And here is an helpful tutorial for creating methods, which I found by doing a google search for "java methods". http://www.tutorialspoint.com/java/java_methods.htm

matt
  • 115
  • 5
  • To reformat your code, assuming you're using eclipse, see: http://stackoverflow.com/questions/15655126/how-to-auto-format-code-in-eclipse (then update your post so it's more readable) – matt Jun 17 '14 at 23:58