Reputation: 59
I have been working on this program for over 30 hours and I am almost as lost now as when i first started. I've finally been able to print out 7 rows of 3 cards but i still have a few issues with my program.
I need to have the cards line up on the word "of"
when i call my PrintDeck() function it is supposed to print out all the cards in the deck but it does not.
I cannot for the life of me figure out how to pick up the columns of cards in order to do the trick, If teh user chooses column 1 i'm supposed to pick it up second and then deal them out again in 7 rows of 3 columns. Then ask them to pick their card 2 more time and do the same thing.
Any help you could offer would be greatly appreciated. I know some of the code is not very pretty but it has to be that way for the assignment in order for me to get credit :/ Thank you so much for all your help.
instructions: here is a doc if the link works ----> instructions doc Program Requirements:
Your program must generate its own random deck of cards using the following technique:
generate a random integer for each card
display a string value for each card ("Ace of Diamonds")
use the rand() function to generate integer card values
use the srand(time(0)) command (only once at the beginning of the program) to generate a truly random deck once the program has been tested and runs properly.
Each value must be converted to the format of as done in the example above. Whenever your program displays the cards they must line up as they do above, on the word "of".
Your program must deal the cards out by row and pick them up by column (3 times to make it work properly).
Your program must ask the player is he/she wants to printout the entire deck before playing. Each card must be printed out formatted as above.
Your program must ask the player if he/she wants to play again, and continue playing as many times as the player wants.
The program must ask the player for his/her name and refer to the player by name throughout the playing of the game.
Your program code must be logically organized using functions. You also must use the starter file provided. You will GET A ZERO (0) if you do not use the starter file!
The starter file provided: CardtrickStarterFile.java if this pastes weird here is a pastebin link: http://pastebin.com/rsZY2vKq
import java.util.Scanner;
import java.lang.String;
import java.util.Random;
public class CardTrickStarterFile {
private static Random rand = new Random();
private static String PlayAgain;
public static void main(String[] args) {
/* declare and initialize variables */
int column = 0, i = 0;
/* Declare a 52 element array of integers to be used as the deck of cards */
int[] deck = new int[52];
/* Declare a 7 by 3 array to receive the cards dealt to play the trick */
int[][] play = new int[7][3];
/* Declare a Scanner object for input */
Scanner input = new Scanner(System.in);
/* Generate a random seed for the random number generator. */
/* Openning message. Ask the player for his/her name */
System.out.println("\nHello, I am a computer program that is so smart");
System.out.println("I can even perform a card trick. Here's how.\n");
System.out.println("To begin the card trick type in your name: ");
String name = input.nextLine();
char firstL = name.charAt(0);
firstL = Character.toUpperCase(firstL);
name = replaceCharAt(name, 0, firstL);
/* Capitalize the first letter of the person's name. */
System.out.println("\nThank you " + name);
do
{
/* Build the deck */
BuildDeck(deck);
/* Ask if the player wants to see the entire deck. If so, print it out. */
System.out.println("Ok " + name + ", first things first. Do you want to see what ");
System.out.println("the deck of cards looks like (y/n)? ");
String SeeDeck = input.nextLine();
switch (SeeDeck)
{
case "y":
PrintDeck(deck);
break;
case "n":
System.out.println("Ok then let us begin.");
Deal(deck,play);
break;
default:
break;
}
System.out.printf("\n%s, pick a card and remember it...\n", name);
/* Begin the card trick loop */
for(i = 0; i < 3; i++)
{
/* Begin the trick by calling the function to deal out the first 21 cards */
/* Include error checking for entering which column */
do
{
/* Ask the player to pick a card and identify the column where the card is */
System.out.print("\nWhich column is your card in (0, 1, or 2)?: ");
column = input.nextInt();
} while(column < 0 || column > 2);
/* Pick up the cards, by column, with the selected column second */
}
/* Display the top ten cards, then reveal the secret card */
/* if the player wants to play again */
System.out.printf("%s, would you like to play again (y/n)? ", name);
String PlayAgain = input.nextLine();
}
while(PlayAgain == "y");
/* Exiting message */
System.out.println("\nThank you for playing the card trick!\n");
return;
}
public static String replaceCharAt(String s, int pos, char c) {
return s.substring(0,pos) + c + s.substring(pos+1);
}
public static void BuildDeck( int deck[])
{
int[] used = new int[52];
int i = 0;
int card = 0;
/* Generate cards until the deck is full of integers */
while(i < deck.length)
{
/* generate a random number between 0 and 51 */
card = rand.nextInt(52);
/* 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++;
}
}
}
public static void PrintDeck( int deck[] )
{
for (int i=0; i < 52; i++){
PrintCard(i);
}
/* Print out each card in the deck */
}
public static void Deal( int deck[], int play[][] )
{
int card = 0;
/* deal cards by passing addresses of cardvalues from
the deck array to the play array */
System.out.println("\n Column 0 Column 1 Column 2");
System.out.println("=======================================================");
for(int row = 0; row < play.length; row++){
for(int col = 0; col < play[row].length; col++){
play[row][col]=deck[card];
card++;
System.out.printf("%s", PrintCard(play[row][col]) + " ");
}
System.out.println();
}
}
public static String PrintCard( int card )
{
int rank = (card % 13);
int suit = (card / 13);
String[] suits = { "Clubs", "Hearts", "Diamonds", "Spades" };
String[] ranks = { "King", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen"};
return (ranks[rank] + " of " + suits[suit]);
}
public static void PickUp( int deck[], int play[][], int column )
{
int card = 0, row = 0;
return;
}
public static void SecretCard( int deck[] )
{
int card = 0;
System.out.println("\nFinding secret card...");
for(card = 0; card < 10; card++)
PrintCard(deck[card]);
System.out.println("\nYour secret card is: ");
PrintCard(deck[card]);
return;
}
}
Upvotes: 2
Views: 3059
Reputation: 2494
OK, there's a few things wrong, so i'll try help you bit by bit. The first is your print deck method, it doesn't actually print anything... you are also passing the wrong thing to it. have another look at ONLY this method and see if you can firstly make it print something, then secondly see why it's print the wrong things
Upvotes: 2