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;
}``````

Edited by Yutxz: n/a

2
Contributors
12
Replies
13
Views
7 Years
Discussion Span
Last Post by apines

In order to take a random number from the deck, you need to generate a random number that will be the cell of the array - deck[random cell] equals to a random card taken. Note that you need to know which cards have been already taken in order not to deal the same card twice - one option is to hold a boolean array such that if cell i in that array is true, it means that the card in cell i in the deck array was taken.

Would it be easier to build this hand separately in a different method and then add up the total score in another one?

It's a possible and logical implementation - one method to build the deck, one to deal the hand and one to add the score :)

Ok! I'll post back if I need some more help.

Update on my progress:

``````public static Card[] dealHand(int n) {
Card[] hand = new Card [n - 1];
for (int i = 0; i < hand.length; i++) {
int randomSuit = randInt (0, 3);
int randomRank = randInt (1, 14);
hand[i] = new Card (randomSuit, randomRank);
}
return hand;
}``````

Edited by Yutxz: n/a

Ok, quick question. I know my method doesn't take cards from the buildDeck method. But how would I do that exactly?

Okay, my sorry excuse for the handScore method. Combined both methods because I thought it was easier. Thanks for the help!

``````// produces a hand with number (n) cards
// and returns the total hand score
public static int handScore(int n) {
int score = 0;
Card[] hand = new Card [n];
// produces the card
for (int i = 0; i < hand.length; i++) {
int randomSuit = randInt (0, 3);
int randomRank = randInt (1, 14);
if (randomRank > 10) {
randomRank = 10;
}
// tallies the score
hand[i] = new Card (randomSuit, randomRank);
score += randomRank;
}
return score;
}``````

Looks like it will do the job, but I think separating the methods will be a better design - what if next exercise you will need to implement a two-players game? you will need to deal the hand, play some rounds, then count the score. In order to take cards from the deck, you can define the deck as a class member instead of a method variable, then you will be able to access it in all the methods.

The thing is, I don't exactly understand how to use the result from a dealHand method in my handScore method. Would this be done in main some how?

Assuming that you `dealHand(int n)` method returns `Card[]` , in order to use the output you will have to do

``Card[] hand = dealHand(int n);``

This will place the `Card[]` returned from `dealHand(int n)` into the variable `hand` .

Could I call for the result in handScore, or would this have to be done in main?

You can use the technique I've showed you anywhere in the code - it's a simple variable assignment, just instead of assigning it to another variable or to a constant, you assign it to the returning value of the method.

Edited by apines: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.