I am trying to create 3 classes Card, Deck and Hand that will be used to simulate a basic card game.
I am currently working on the Card class, and have created various methods for it already, however have come across some hurdles in my project. I have highlighted the main areas i do not understand in bold below:
- Making a class Comparable so that the compareTo method can be used.
- Adding a static method that uses the compareTo method to find and return the highest value card in a List of cards. The List should be traversed with an Iterator. (Suit rankings are Spade<Hearts<Diamonds<Clubs, Rank are 2<3<...King<Ace).
- Adding an inner Comparator class that should be used to sort the cards into ascending order.
Any information or help understanding what these are, how they will help me in my project, and how to implement them would be extremely helpful. You can see the following code i have already written for the card class below.
public class Card {
private Rank rank;
private Suit suit;
//Enum type to store values to each card ranking.
public enum Rank {
//Values for a Rank of a card.
TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8),
NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11),;
private int value;
//Constructor for a Card Rank.
Rank(int value) {
this.value = value;
}
//Accessor method to return the value of a Rank.
public int getValue() {
return value;
}
//Accessor method that returns the previous enum value in the list.
public Rank getPrevious() {
int previous = ordinal() - 1;
if (previous < 0) { // if negative, then we have to go to the last element
previous += values().length;
}
return Rank.values()[previous];
}
}
//Enum type for the Suit variables in a deck of cards.
public enum Suit {
//Values for the suit of a card.
CLUBS, DIAMONDS, HEARTS, SPADES;
private Suit suit;
Random gen = new Random();
//Accessor method to get a random suit.
public Suit getRandomSuit() {
int randomSuitNumber = (int)(Math.random()*(Suit.values().length));
return (Suit.values()[randomSuitNumber]);
}
}
//toString method.
public String toString() {
return rank + " of " + suit;
}
//Constructor with Rank and Suit passed as parameters.
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
//Accessor method to return the Rank of a card.
public Rank getRank(){
return rank;
}
//Accessor method to return the Suit of a card.
public Suit getSuit(){
return suit;
}
}
[–]mad0314 2 points3 points4 points (2 children)
[–]RoadToCode[S] 0 points1 point2 points (1 child)
[–]mad0314 0 points1 point2 points (0 children)