+ 1

PRINT AND SHUFFLE A DECK OF CARDS(HELP!)

//What am I missing and doing wrong? public class Card { 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; } public String printCard(Card c) { String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; System.out.println(ranks[c.rank] + " of " + suits[c.suit]); return ranks[this.rank] + " of " + suits[this.suit]; } public static void printCard(Card c) { String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] ranks = { "narf", "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 shuffleArray(Card[] cards) { for (int i = 0; i <= cards.length; i++) { swap(0, 100); } } public static void swap(Card[] cards, int i, int j) { Card temp = this.cards[i]; this.cards[i] = this.cards[j]; this.cards[i] = temp; } public static void printStrings(String[] strs){ for (int i = 0; 1 < strs.length; i++) { System.out.println(strs[i]); } } public static void main(String[] args){ Card[] cards = new Card[52]; shuffleArray(cards); printStrings(cards);

1st Dec 2017, 3:53 PM
Geena Nana
Geena Nana - avatar
3 Answers
+ 3
There are often just minor typos in the code. I am pretty sure you meant to write Card temp = this.cards[i]; this.cards[i] = this.cards[j]; this.cards[j] = temp; In your swap() method. Please notice the following: 1. You probably shouldn't put a class "Card" inside of another public class "Card". This only causes confusion. 2. Your cards are not initialized in your main() 3. Your way of shuffling isn't random at all.
1st Dec 2017, 4:06 PM
Pascal Huppert
Pascal Huppert - avatar
+ 1
I use a shuffle function in my C++ code here: https://code.sololearn.com/csRrg99rE15g It's really similar to java in many ways. I made two classes, one for cards and one is the deck, which handles everything else.
1st Dec 2017, 11:48 PM
Zeke Williams
Zeke Williams - avatar
+ 1
https://code.sololearn.com/cW5d1J7YjSTT/?ref=app I used a safe shuffle here, quick easy and reliable.
27th Dec 2017, 7:50 PM
Michael Simnitt
Michael Simnitt - avatar