I'm working on a Card game assignment and our lecturers want us to use different data structures. Now I have an ArrayList of Cards object and a HashMap of Points<Cards, Integer>. I'm trying to compare the HashMap key to the elements in the ArrayList to check the points for the Cards in the ArrayList but nothing seems to be working.
Here's the HashMap
Map<Cards, Integer> Points = new HashMap<>();
Points.put(new Cards("d", "A"), 1);
Points.put(new Cards("c", "A"), 1);
Points.put(new Cards("h", "A"), 1);
Points.put(new Cards("s", "A"), 1);
Points.put(new Cards("d", "2"), 2);
Points.put(new Cards("c", "2"), 2);
Points.put(new Cards("s", "2"), 2);
Points.put(new Cards("h", "2"), 2);
Points.put(new Cards("d", "3"), 3);
Points.put(new Cards("c", "3"), 3);
Points.put(new Cards("s", "3"), 3);
Points.put(new Cards("h", "3"), 3);
Points.put(new Cards("d", "4"), 4);
Points.put(new Cards("c", "4"), 4);
Points.put(new Cards("s", "4"), 4);
Points.put(new Cards("h", "4"), 4);
Points.put(new Cards("d", "5"), 5);
Points.put(new Cards("c", "5"), 5);
Points.put(new Cards("s", "5"), 5);
Points.put(new Cards("h", "5"), 5);
Points.put(new Cards("d", "6"), 6);
Points.put(new Cards("c", "6"), 6);
Points.put(new Cards("s", "6"), 6);
Points.put(new Cards("h", "6"), 6);
Points.put(new Cards("d", "7"), 7);
Points.put(new Cards("c", "7"), 7);
Points.put(new Cards("s", "7"), 7);
Points.put(new Cards("h", "7"), 7);
Points.put(new Cards("d", "8"), 8);
Points.put(new Cards("c", "8"), 8);
Points.put(new Cards("s", "8"), 8);
Points.put(new Cards("h", "8"), 8);
Points.put(new Cards("d", "9"), 9);
Points.put(new Cards("c", "9"), 9);
Points.put(new Cards("s", "9"), 9);
Points.put(new Cards("h", "9"), 9);
Points.put(new Cards("d", "X"), 10);
Points.put(new Cards("c", "X"), 10);
Points.put(new Cards("s", "X"), 10);
Points.put(new Cards("h", "X"), 10);
Points.put(new Cards("d", "J"), 10);
Points.put(new Cards("c", "J"), 10);
Points.put(new Cards("s", "J"), 10);
Points.put(new Cards("h", "J"), 10);
Points.put(new Cards("d", "Q"), 10);
Points.put(new Cards("c", "Q"), 10);
Points.put(new Cards("s", "Q"), 10);
Points.put(new Cards("h", "Q"), 10);
Points.put(new Cards("d", "K"), 10);
Points.put(new Cards("c", "K"), 10);
Points.put(new Cards("s", "K"), 10);
Points.put(new Cards("h", "K"), 10);
Here's the ArrayList (Player1) of object Cards
/**Cards class*/
class Cards implements Comparable<Cards> {
public String suit;
public String face;
public Cards() {
}
//Create Cards object
public Cards (String suit, String face){
this.suit = suit;
this.face = face;
}
//Return card suit
public String getSuit() {
return suit;
}
//Return card face
public String getFace() {
return face;
}
@Override
public int compareTo(Cards currentCards) {
if (getSuit().compareTo(currentCards.getSuit()) > 0) {
return 1;
} else if ((getSuit().compareTo(currentCards.getSuit())) < 0){
return -1;
} else if ((getSuit().compareTo(currentCards.getSuit())) == 0){
if (getFace().compareTo(currentCards.getFace()) > 0){
return 1;
} else if (getFace().compareTo(currentCards.getFace()) < 0){
return -1;
}
}
return 0;
}
@Override
public String toString() {
return this.getSuit() + this.getFace();
}
}
/**Creating Cards object*/
String[] SUIT = new String[]{ "d", "c", "h", "s" };
String[] FACE = new String[]{ null, "A", "2", "3", "4", "5", "6", "7", "8", "9", "X", "J", "Q", "K" };
Cards[] cardDeck = new Cards[52];
int index = 0;
for (int i = 0; i <= 3; i++) {
for (int j = 1; j <= 13; j++) {
cardDeck[index] = new Cards(SUIT[i], FACE[j]);
index++;
}
}
/**Player 1 in hand*/
ArrayList<Cards> player1 = new ArrayList<Cards>();
int j=0;
for (int i = 0; i <18; i++){
player1.add(i, cardDeck[j++]);
}
We tried this
for(Cards n : player1){
if (Points.containsKey(n)){
System.out.println("Card points: " + Points.get(n));
}
}
The program compiles and run but the loop didn't go through as the print statement did not print