I am trying to get my private void transfer method and pass it into a 2D array transfer2D method, after which I am able to print it out using the print2D_1 method. I am also trying to sort the array from the highest suit to the lowest suit followed by the highest rank to the lowest rank e.g. S12, S9, H13, D09, C10. Please advice.
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
enum SuitEnum
{
Spade ('S'),
Heart ('H'),
Diamond ('D'),
Club ('C');
public char suit;
SuitEnum(char suit)
{
this.suit = suit;
}
// Accessor getter
public char getSuit ()
{
return suit;
}
}
enum RankEnum
{
Two ('2'),
Three ('3'),
Four ('4'),
Five ('5'),
Six ('6'),
Seven ('7'),
Eight ('8'),
Nine ('9'),
Ten ('T'),
Jack ('J'),
Queen ('Q'),
King ('K'),
Ace ('A');
public char rank;
RankEnum(char rank)
{
this.rank = rank;
}
// Acessor getter
public char getRank ()
{
return rank;
}
}
class PlayingCard
{
private SuitEnum suit;
private RankEnum rank;
private PlayingCard pc;
//constructor
public PlayingCard(SuitEnum suit, RankEnum rank)
{
this.suit = suit;
this.rank = rank;
}
//copy constructor
public PlayingCard(PlayingCard pc)
{
this.pc = pc;
}
//accessor get method
public SuitEnum getSuit()
{
return suit;
}
public RankEnum getRank()
{
return rank;
}
//setter
public void setCard(SuitEnum suit, RankEnum rank)
{
this.suit = suit;
this.rank = rank;
}
@Override
public String toString()
{
return String.format("%3s%s",suit.getSuit(),rank.getRank());
}
}
class ChuaWeiheng_A1
{
private final int MAXC = 13;
private final int MAXD = 52;
private void deckOfCards(ArrayList<PlayingCard> values)
{
for (SuitEnum suit : SuitEnum.values())
{
for (RankEnum rank: RankEnum.values())
values.add(new PlayingCard (suit, rank));
}
}
private void printDeck(ArrayList<PlayingCard>values)
{
int count = 0;
System.out.println("Printing from ArrayList");
System.out.println();
{
for (PlayingCard s : values)
{
count++;
System.out.print(s);
if (count == MAXC)
{
System.out.println();
count = 0;
}
}
}
System.out.println("-------------------------");
}
private void listToArray(ArrayList<PlayingCard> values, PlayingCard[] valuesArray)
{
int i = 0;
for (PlayingCard s: values)
{
valuesArray [i] = s;
++i;
}
}
private void printDeck(PlayingCard[] valuesArray)
{
int count = 0;
System.out.println("Printing from Array");
System.out.println();
for (PlayingCard s : valuesArray)
{
count++;
System.out.print(s);
if (count == MAXC)
{
System.out.println();
count = 0;
}
}
System.out.println("-------------------------");
}
private void transfer(PlayingCard[] cardArray, String[] strArray)
{
String[] arrayNo = new String[]{"02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14"};
String[] arrayLetter = new String[]{"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
int i = 0;
for (PlayingCard s : cardArray)
{
String currentCard = s.getRank().toString();
for (int j = 0; j < 13; j++)
{
if (currentCard.equals(arrayLetter[j]))
{
strArray[i] = s.getSuit().toString().charAt(0) + arrayNo[j];
}
}
i++;
}
}
private void printStringArray(String[] strArray)
{
int count = 0;
System.out.println("Printing from string array");
System.out.println();
for (int i = 0; i < MAXD; i++)
{
count++;
System.out.print(" " + strArray[i]);
if (count == MAXC)
{
System.out.println();
count = 0;
}
}
System.out.println("-------------------------");
}
// shuffle
private void shuffle(PlayingCard[] valuesArray)
{
Random random = ThreadLocalRandom.current();
int count = 0;
for (int k = valuesArray.length -1; k> 0; k --)
{
int i = random.nextInt(k+1);
PlayingCard j = valuesArray[i];
valuesArray[i] = valuesArray[k];
valuesArray[k] = j;
}
System.out.println("Shuffle the cards - Array Version");
System.out.println("Printing from array");
System.out.println();
{
for (PlayingCard s : valuesArray)
{
count++;
System.out.print(s);
if (count == MAXC)
{
System.out.println();
count = 0;
}
}
System.out.println("-------------------------");
}
}
private void transfer2D(String[][] twoD, String[] strArray)
{
int rows = 4;
int columns = 13;
twoD = new String[rows][columns];
for(int i = 0; i < twoD.rows; i++)
{
for(int j = 0; j < twoD.columns; j++)
{
twoD[i][j] = strArray[(i*twoD.columns)+j];
}
}
}
private void print2D_1(String[][] twoD)
{
int count = 0;
System.out.println("Printing from string array");
System.out.println();
for(int[] a : twoD)
{
for(int i : a)
{
}
}
}
public static void main(String[] args)
{
ArrayList<PlayingCard>X = new ArrayList<PlayingCard> ();
ChuaWeiheng_A1 T1 = new ChuaWeiheng_A1();
PlayingCard [] T2 = new PlayingCard[T1.MAXD];
String[] strArr = new String[52];
T1.deckOfCards(X);
T1.printDeck(X);
T1.listToArray(X,T2);
T1.printDeck(T2);
T1.shuffle(T2);
T1.transfer(T2, strArr);
T1.printStringArray(strArr);
}
}