Using Java and JOptionPane I. INTRODUCTION: This project applies the concepts of
ID: 3847643 • Letter: U
Question
Using Java and JOptionPane
I. INTRODUCTION: This project applies the concepts of random number generation, multi-dimensional array and bit-mapped technique. ll. DESCRIPTION A deck of playing cards has four suits which are Spade (A Mu2660''), Heart (v-mu2665), Diamond -u2666), and Club 1u2663). Each suit has 13 cards in the order of 12, 3 9, 10, J, Q, K, A) and this is called the rank of the card. Most card games do not rank suits; the ace of clubs is just as good as the ace of spades However, there are occasions that the ranking of the suits needs to be decided. Please be reminded that no standard of ranking of suits exits. However, in this project we are going to use the Reverse Alphabetical order convention: clubs (lowest), followed by diamonds, hearts, and spades (highest). This ranking is also used in the game of bridge. Ill. ASSIGNMENT Write a JAVA program according to produce the output similar to the following according to the steps specified below: EAST A 10 3 V: A 9 8 A Q J Q 10 86 NORTH V: K 10 7 10 9 8 7 6 2 4 J 4 3 WEST 4 Q 6 5 6 5 4 3 2 4 3 K 7 2 SOUTH K J 9 8 7 4. Q J K 5 4 A 9 5 OKExplanation / Answer
Cards.java:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Cards{
static int[] deckOfCard = new int[52];
static int[][] hands = new int[4][13];
static String[] Suit = {"Spade", "Heart", "Diamond", "Club"};
static char[] SuitSymbol = {'u2660', 'u2665', 'u2666', 'u2663'};
static String[] Party = {"East", "North", "West", "South"};
static String[] Card = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
static void generateCard(int[] A){
boolean[] flag = new boolean[52];
for (int i=0;i<52;i++){
flag[i] = false;
}
int numbers_generated=0, random_number;
while(numbers_generated<52){
random_number = (int)( Math.random() * (52) );
if(flag[random_number]==false){
A[numbers_generated] = random_number;
flag[random_number] = true;
numbers_generated++;
}
}
}
static void printCard(int[] A, int perLine, int width){
String width_string = "";
for (int i=0;i<width;i++){
width_string += " ";
}
int lines = A.length/perLine;
for (int i=0;i<lines;i++){
String temp_line = "";
//One less loop so that we do not print spaces after last element in a line
for (int j=0;j<perLine-1;j++){
temp_line += A[i*perLine + j] + width_string;
}
temp_line += A[i*perLine + (perLine-1)];
System.out.println(temp_line);
}
}
static void deal(int[] deckOfCard, int[][] hands){
//assigning row numbers to the parties
int east=0,north=1,west=2,south=3;
for (int i=0;i<13;i++){
hands[east][i] = deckOfCard[4*i + 0];
hands[north][i] = deckOfCard[4*i + 1];
hands[west][i] = deckOfCard[4*i + 2];
hands[south][i] = deckOfCard[4*i + 3];
}
}
static void printHands(int[][] hands){
int[][][] fourDecks = new int[4][4][13];
//Initializing every element to 0
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
for (int k=0;k<13;k++){
fourDecks[i][j][k] = 0;
}
}
}
for (int i=0;i<4;i++){
for (int j=0;j<13;j++){
int suit = hands[i][j]/13;
int card = hands[i][j]%13;
fourDecks[i][suit][card] = 1;
}
}
StringBuffer sb=new StringBuffer();
for (int i=0;i<4;i++){
sb.append(Party[i] + " ");
for (int j=0;j<4;j++){
sb.append(SuitSymbol[j] + ": ");
for (int k=12;k>=0;k--){
if(fourDecks[i][j][k]==1){
sb.append(Card[k] + " ");
}
}
sb.append(" ");
}
}
JFrame frame = new JFrame("printHands");
JOptionPane.showMessageDialog(frame, sb.toString());
}
public static void main(String[] args) {
generateCard(deckOfCard);
printCard(deckOfCard, 13, 5);
deal(deckOfCard, hands);
printCard(hands[0], 13, 5);
printCard(hands[1], 13, 5);
printCard(hands[2], 13, 5);
printCard(hands[3], 13, 5);
printHands(hands);
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.