Reputation: 21
I'm still new at this and I am having a hard time in this project. I need to shuffle the cards and get the first four cards and computing the sum of the four cards. I've done this task so far. Now my problem is I need to repeat this method until I can get the sum total of 24. I'm having a hard time on how to loop the method. Here's is the code I've done so far:
public class cards
{
public static void main(String[] args)
{
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int rankedVal[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int tot = 0;
if (tot != 24){
//Initialize cards
tot = 0;
for (int i = 0; i < deck.length; i++)
deck[i] = i;
//Shuffle cards
for (int i = 0; i < deck.length; i++)
{
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
//Display first four
for (int i = 0; i < 4; i++)
{
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);
}
//find the total
for (int i = 0; i<4;i++){
tot = tot + rankedVal[deck[i] % 13];
}
System.out.println("Total Hand = " + tot);
}
}
}
Upvotes: 2
Views: 286
Reputation: 730
That's why you should learn how to use methods and subroutines. And if you are using Java then definitely you should first learn a bit about Object Oriented Programming. If you don't know about OOP, I wouldn't recommend Java as a beginner language. Go for C or Python maybe.
public class cards
{
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int rankedVal[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
void init() {
// initialise logic
for (int i = 0; i < deck.length; i++)
deck[i] = i;
}
void shuffleCards() {
for (int i = 0; i < deck.length; i++) {
// Shuffle logic
}
}
void displayFirstFour() {
// display logic
}
int sumFirstFour() {
int sum = 0;
// logic to sum first four cards
return sum;
}
public static void main(String[] args)
{
init();
int total = 0;
// LOOP. NOT 'if' . 'if' simply tests condition once and moves on
// loop tests condition continually until true
while(total != 24) {
//Shuffle cards
shuffleCards();
//Display first four
displayFirstFour();
total = sumFirstFour();
}
System.out.println("Total Hand = " + total);
}
}
Upvotes: 1
Reputation: 15367
Without making a spoiler, you could use a condition in a while loop until 24 is reached, like:
do
{
// Get 4 cards and add sum.
} while (sum < 24);
Also you need to check you didn't run out of cards, so an improvement would be:
int cardsUsed = 0
do
{
// Get 4 cards and add sum.
cardsUsed += 4;
} ((sum < 24) or (cards_used >= 52));
Upvotes: 1
Reputation: 1165
I will use a little more generic code as different representations suit different programmers, but this should work with anything:
int sum = 0;
While (sum != 24)
{
Shuffle();
sum = deck[0].value + deck[2].value + deck[3].value + deck[4].value;
}
It would be better if you made deck an array of cards and exchanged between pairs when shuffling, and cards having an integer value within them.. Initialize once and then it's all swapping the objects. It's Java, isn't it?
Upvotes: 1