There is a card game I was trying to make for the past two weeks in java. Can be played here => http://www.angelfire...ardtrick02.html
Basically the player picks a card and the program will ask which column contains their card. It than shuffles the cards back together. After doing that three times, the program should find their secret card. After getting down the logic and putting it into the code. This is what i came up with

The problems are that deck seems to be broken the build/display and the results are somewhat off... Also generating random arrays from 0 - 51 seems to be not working.
Display problems not displaying the deck in 3 columns but only one.

import java.util.Scanner;
import java.util.Random;

public class Main {

	public static void main(String[] args) {

// declare variables
int column = 0, i = 0;
char PlayAgain;
char SeeDeck;

// 52  array of ints 
int[] deck = new int[52];
for(i = 0; i < deck.length; i++)
   deck[i] = 0;

// 7 by 3 array
int[][] play = new int[7][3];
for(i = 0; i < 7; i++)
	for (int j = 0; j < 3; j++)
		play[i][j] = 0;

// scanner
Scanner input = new Scanner(System.in);

// random
Random generator = new Random();


// display name capital etc..

System.out.println("First, enter the name before we start the game: ");
String name = input.nextLine();


name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
System.out.println("\nThanks " + name);

do {
    // build the deck
   BuildDeck(deck);

    // ask 
    System.out.println("Ok " + name + ", you want to see the deck? y/n ");
   
    SeeDeck = input.next().charAt(0);

    if(SeeDeck == 'y') { 
        PrintDeck(deck);
    }
    
    System.out.printf("\n%s, pick and remember your card...", name);

    // This is the loop of the trick
    for(i = 0; i < 3; i++)
    {
		// start call the function to deal first 21 cards

                Deal(deck, play); 
                    
        do
		{
			
			System.out.print("\nDon't tell me what card you are thinking of. I just want to know the column its in - 0, 1, 2?: ");
			column = input.nextInt();
		} while(column < 0 || column > 2);

		// Pick up by column with selected column second..
                PickUp(deck, play, column);  }

    // show top ten then reveal the secret ...
        SecretCard(deck); 

    System.out.printf("%s, play again (y/n)? ", name);
    PlayAgain = input.next().charAt(0);

}while(PlayAgain == 'y');

System.out.println("Thanks for playing");
return;
}

public static void BuildDeck( int deck[])
{
  int[] used = new int [52];
  int card = 0, i = 0;

  // Generate deck till full...
  while(i < deck.length)
    {
		// generate a random  from 0 and 51
                Random random = new Random();
                card = random.nextInt(51 - 0) + 0;

		// Check the used array at the position of the card.
		 //  If 0, add the card and set the used location to 1. If 1, generate another number
		if(used[card] == 0)
		{
                    used[card] = 1;
                    deck[i] = card;
                    i++;
		}
                if (used[card] ==1 ) {
                    card = random.nextInt(51 - 0) + 0;
                }
    }
	return;
}

public static void PrintDeck( int deck[] )
{
      
	int i = 0;
        while (i < 52) {
            PrintCard(deck[i]);
            System.out.println();
            i++;
        }
	
}

public static void Deal( int deck[], int play[][] )
{
	int row = 0, col = 0, card = 0;

	// deal cards from deck to play array...
        
	System.out.println("\n   Column 0           Column 1           Column 2");
	System.out.println("=======================================================\n");

        for (row = 0; row < 7; row++){
            play[row][col] = deck[card];
                PrintCard(play[row][col]);
                    card++;

                for (col = 0; col < 2; col++) {
                    play[row][col] = deck[card];
                        PrintCard(play[row][col]);
                            card++;
    }
                 System.out.println();
    }
	return;
}

public static void PrintCard( int card )
{
	int rank = 0;
	int suit = 0;

        rank = card % 13; // use remainder to determine rank
        suit = card / 13;

        switch (rank) {
            case 0: System.out.println("King");
            case 1: System.out.println("Ace");
            case 11: System.out.println("Jack");
            case 12: System.out.println("Queen");
            default: System.out.printf("%5d", rank);

        }
        switch (suit) {
            case 0: System.out.println("of clubs");
            case 1: System.out.println("of diamonds");
            case 2: System.out.println("of hearts");
            case 3: System.out.println("of spades");

        }

	  return;
}

public static void PickUp( int deck[], int play[][], int column )
{
  int card = 0, row = 0;
  if (column == 2) {
     

      for (row = 0; row < 7; row++){
          System.out.println("Col 2");
            deck[card] = play[row][2];
                System.out.println("Pickup");
                    PrintCard(play[row][2]);
                        System.out.printf("Card Number: %d", card);
                            card++; }

      for (row = 0; row < 7; row++) {

            System.out.println ("Col 1");
                deck[card] = play[row][1];
                    System.out.println( "Pickup " );
                        PrintCard( play[row][1] );
                            System.out.printf( "Card number: %d", card );
                                System.out.println( "\n" );
                                    card++; }


      for (row = 0; row< 7; row++) {

             System.out.println ("Col 0");
                deck[card] = play[row][0];
                    System.out.println( "Pickup " );
                        PrintCard( play[row][0] );
                            System.out.printf( "Card number: %d", card );
                                System.out.println( "\n" );
                                    card++; }
      }

  else if (column == 1) {

      for(row = 0; row < 7; row++) {
          System.out.println("Col 2");
          deck[card] = play[row][2];
          System.out.println("Pickup ");
          PrintCard(play[row][2]);
          System.out.printf("Card number: %d\n", card);
          System.out.println();
          card++;
      }

      for(row = 0; row < 7; row++) {
          System.out.println("Col 0");
          deck[card] = play[row][0];
          System.out.println("Pickup ");
          PrintCard(play[row][0]);
          System.out.printf("Card number: %d\n", card);
          System.out.println();
          card++;}

      for(row = 0; row < 7; row++) {
          System.out.println("Col 1");
          deck[card] = play[row][1];
          System.out.println("Pickup ");
          PrintCard(play[row][1]);
          System.out.printf("Card number: %d\n", card);
          System.out.println();
          card++;}

  }


  else if (column == 0) {

      for(row = 0; row < 7; row++) {
          System.out.println("Col 1");
          deck[card] = play[row][1];
          System.out.println("Pickup ");
          PrintCard(play[row][1]);
          System.out.printf("Card number: %d\n", card);
          System.out.println();
          card++;
      }

      for(row = 0; row < 7; row++) {
          System.out.println("Col 2");
          deck[card] = play[row][2];
          System.out.println("Pickup ");
          PrintCard(play[row][2]);
          System.out.printf("Card number: %d\n", card);
          System.out.println();
          card++;}

      for(row = 0; row < 7; row++) {
          System.out.println("Col 0");
          deck[card] = play[row][0];
          System.out.println("Pickup ");
          PrintCard(play[row][0]);
          System.out.printf("Card number: %d\n", card);
          System.out.println();
          card++;}

  }

	return;
}

public static void SecretCard( int deck[] )
{
	int card = 0;

	System.out.println("\n looking 4 the secret card...");
	for(card = 0; card < 10; card++) {
		PrintCard(deck[card]); }

	System.out.print("\nAND the magic card is...: ");
        PrintCard(deck[card]);
	return;
}

}

Recommended Answers

All 5 Replies

Loops work very well with arrays. If you're having trouble, I don't see where it is in a quick scan through your code. Maybe you can be more specific about what functionality is giving you trouble, or even point us to a particular method?

The problem is the output...
My current output

Before we start, please enter your name: 
nino

Thank you Nino
You want to see the deck (y/n)? 
y
King
Ace
Jack
Queen
    0of clubs
of diamonds
of hearts
of spades

King
Ace
Jack
Queen
    0of clubs
of diamonds
of hearts
of spades

King
Ace
Jack
Queen
    0of clubs
of diamonds
of hearts
of spades

King
Ace
Jack
Queen
    0of clubs
of diamonds
of hearts
of spades

King
Ace
Jack
Queen
    0of clubs
of diamonds
of hearts
of spades

King
Ace
Jack
Queen
    0of clubs
of diamonds
of hearts
of spades

After priting out the same thing over again and the 0ofclubs rather then randomizing numbers... I can't pinpoint the problem but it is most likely to be inside the while loop...

expected output should be something like this

Column 0           Column 1          Column 2 
======================================================= 
    9 of Clubs         7 of Clubs        8 of Hearts 
    6 of Spades     Jack of Clubs     King of Spades 
 Jack of Hearts        4 of Diamonds     8 of Diamonds 
    6 of Hearts     Jack of Diamonds     8 of Spades 
    8 of Clubs      King of Clubs        9 of Hearts 
    4 of Hearts      Ace of Clubs      Ace of Spades 
   10 of Diamonds      5 of Spades       3 of Diamonds 
  
Don't tell me what card you are thinking of. I just want to know the column its in - 0, 1, 2?:

Okay, your main problem is a simple and a common one. Look at your switches in printCard. Look carefully at them - what happens if rank is 0?
If you don't find yourself pounding your head on the desk when you answer that, it's the wrong answer. Test it, call the method with 0 as an argument.

Then, stop pounding your head on the desk and fix it. You'll know what to do.

(how common a problem is this? It's the second time I've answered this question this week...)

After each case there should be a break; :idea:
How stupid of me not to see that...


Okay, your main problem is a simple and a common one. Look at your switches in printCard. Look carefully at them - what happens if rank is 0?
If you don't find yourself pounding your head on the desk when you answer that, it's the wrong answer. Test it, call the method with 0 as an argument.

Then, stop pounding your head on the desk and fix it. You'll know what to do.

(how common a problem is this? It's the second time I've answered this question this week...)

It'll be stupid if you ever make that mistake again. First one's free.

Now there's probably still some stuff to sort out, but I think you'll be able to clean it up pretty well from here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.