Java Data structures and algorithmsSolution import javautil

Java - Data structures and algorithms

Solution

import java.util.Scanner; import java.util.Random; public class CardTrick { public static void main(String[] args) { /* declare and initialize variables */ int column = 0, i = 0; String name = \" \"; String playAgain = \" \"; String seeDeck = \" \"; /* Declare a 52 element array of cards */ Card[] deck = new Card [52]; /* Declare a 7 by 3 array to receive the cards dealt to play the trick */ Card [][] play = new Card[7][3]; /* Declare a Scanner object for input */ Scanner input = new Scanner(System.in); /* Openning message. Ask the player for his/her name */ System.out.println(\"\ Hello, I am a very talented computer program and\"); System.out.println(\"I can even perform a card trick. Just watch and be amazed.\ \"); System.out.print(\"To begin the card trick type in your first name: \"); name = input.nextLine(); /* Capitalize the first letter of the person\'s name. */ name = name.substring(0,1).toUpperCase() + name.substring(1); System.out.println(\"\ Thank 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.print(\"the deck of cards looks like (y/n)? \"); seeDeck = input.next(); System.out.printf(\"\ %s, pick a card and remember it...\ \", name); /* Begin the card trick loop */ for(i = 0; i < 3; i++) { /* Begin the trick by calling the method to deal out the first 21 cards */ Deal(deck, play); /* 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(\"\ Which 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 */ PickUp(deck, play, column); } /* Display the top ten cards, then reveal the secret card */ SecretCard(deck); /* if the player wants to play again */ System.out.printf(\"%s, would you like to play again (y/n)? \", name); playAgain = input.next(); } while(playAgain.equals(\"y\")); /* Exiting message */ System.out.print(\"\ \ Thank you for playing the card trick!\ \"); } public static void BuildDeck( Card deck[]) { int [][] used = new int[13][4]; int rank = 0, suit = 0, i = 0; Random rand = new Random(); /* Generate cards until the deck is full of cards */ while(i < deck.length) { /* generate a random number between 0 and 12 for rank */ rank = rand.nextInt(13); /* generate another random number between 0 and 3 for suit */ suit = rand.nextInt(4); /* 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[rank][suit] == 0) { used[rank][suit] = 1; deck[i] = new Card(rank, suit); i++; } } } public static void PrintDeck( Card deck[] ) { /* Print out each card in the deck */ for (int i = 0; i< deck.length; i++){ System.out.println(deck[i].toString()); } } public static void Deal( Card deck[], Card play[][] ) { int row = 0, col = 0, card = 0; /* deal cards by passing addresses of cardvalues from the deck array to the play array */ System.out.println(\"\ Column 0 Column 1 Column 2\"); System.out.println(\"=======================================================\ \"); for (row = 0; row < 7; row++){ for (col = 0; col < 3; col++){ play[row][col] = deck[card]; System.out.print(deck[card].toString()); card++; } System.out.println(); } public static void PickUp( Card deck[], Card play[][], int column) { int card = 0, row = 0, first = 0, last = 0; switch(column) { case 0: first = 1; last = 2; break; case 1: first = 2; last = 0; break; case 2: first = 1; last = 0; break; } for(row = 0; row < 7; row++) { deck [card] = play [row][first]; card++; } for(row = 0; row < 7; row++) { deck [card] = play [row][column]; card++; } for(row = 0; row < 7; row++) { deck [card] = play [row][last]; card++; } } public static void SecretCard( Card deck[] ) { int card; System.out.println(\"\ Finding secret card...\"); for(card = 0; card < 10; card++) System.out.printf(\"%s \ \", deck[card].toString()); System.out.printf(\"\ Your secret card is: %s \ \", deck[card].toString()); } private static class card { public card() { } } }
Java - Data structures and algorithmsSolution import java.util.Scanner; import java.util.Random; public class CardTrick { public static void main(String[] args)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site