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;
}
}