Hello! I'm trying to solve this problem and I've been at it for hours.
Write a method called handScore that takes an array of cards as an argument and
that adds up (and returns) the total score. You should assume that the ranks of the
cards are encoded according to the mapping in Section 11.2, with Aces encoded as
1.
I'm confused on how this array is presented, as in how can I generate a random hand?
I have a randInt method I built and I have a deckBuild method. I'll post what I have already done below.
Can someone explain this to me? Possibly step by step? I can do the coding myself, I'm just failing at problem solving right now.
package card;
/**
*
* @author Josh
*/
public class Card {
int suit, rank;
public Card () {
this.suit = 0;
this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
// prints all cards
public static void printCard (Card c) {
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"empty", "Ace", "2", "3", "4" ,"5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
}
public static void printDeck (Card[] deck) {
for (int i=0; i<deck.length; i++) {
printCard (deck[i]);
}
}
// constructs a deck of 52 cards
public static Card[] buildDeck () {
Card[] deck = new Card [52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
deck[index] = new Card (suit, rank);
index++;
}
}
return deck;
// produces a random integer
public static int randInt (int low, int high) {
double x = Math.random() * (high - low + 1);
return (int) x + low;
}