8 Replies - 1357 Views - Last Post: 27 March 2015 - 11:02 AM Rate Topic: -----

#1 Spiked Penguin   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 107
  • Joined: 19-December 13

Deck of cards program (Which is more efficient)

Posted 23 March 2015 - 08:23 AM

So I wrote a program that randomly generates 4 different card sets before my books program to see how we made them differently. Now that I see the books I am not sure mine is to conventions.

Mine
import java.util.Random;


public class Main {
	
	public static void main(String args[]){
		Random rand = new Random();
		
		String[] cardNumber = {"Ace", "1", "2", "3", "4", "5", "6", "7", 
				"8", "9", "10", "King", "Queen", "Jack"};
		String[] cardType = {"Spades", "Hearts", "Diamonds", "Clubs"};
		String[] cardColor = {"Red", "Black"};
		
		int counter = 0;
				
		do{
			int cardNum = cardNumber.length; // 13
			cardNum = rand.nextInt(cardNum);
		
			System.out.print(cardNumber[cardNum] + " of ");
		
			cardNum = cardType.length; // 4
			cardNum = rand.nextInt(cardNum);
		
			System.out.print(cardType[cardNum] + " ");
		
			cardNum = cardColor.length; // 4
			cardNum = rand.nextInt(cardNum);
		
			System.out.print(cardColor[cardNum]);
			
			counter++;
			System.out.println();
		} while(counter < 4);
	}
}



Books

Quote

1 public class DeckOfCards {
2 public static void main(String[] args) {
3 int[] deck = new int[52];
4 String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
5 String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
6 "10", "Jack", "Queen", "King"};
7
8 // Initialize the cards
9 for (int i = 0; i < deck.length; i++)
10 deck[i] = i;
11
12 // Shuffle the cards
13 for (int i = 0; i < deck.length; i++) {
14 // Generate an index randomly
15 int index = (int)(Math.random() * deck.length);
16 int temp = deck[i];
17 deck[i] = deck[index];
18 deck[index] = temp;
19 }
20
21 // Display the first four cards
22 for (int i = 0; i < 4; i++) {
23 String suit = suits[deck[i] / 13];
24 String rank = ranks[deck[i] % 13];
25 System.out.println("Card number " + deck[i] + ": "
26 + rank + " of " + suit);
27 }
28 }
29 }


Sorry about the setup for the books.

This post has been edited by Spiked Penguin: 23 March 2015 - 08:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Deck of cards program (Which is more efficient)

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Deck of cards program (Which is more efficient)

Posted 23 March 2015 - 08:29 AM

You can have duplicate cards output with your current solution.
Was This Post Helpful? 1
  • +
  • -

#3 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Deck of cards program (Which is more efficient)

Posted 23 March 2015 - 09:37 AM

Also
17	            int cardNum = cardNumber.length; // 13
the length is 14, not 13. You have an ace as well as 1, there is no 1 in a deck of cards.
Was This Post Helpful? 0
  • +
  • -

#4 Spiked Penguin   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 107
  • Joined: 19-December 13

Re: Deck of cards program (Which is more efficient)

Posted 23 March 2015 - 09:54 AM

View Postmike73, on 23 March 2015 - 09:37 AM, said:

Also
17	            int cardNum = cardNumber.length; // 13
the length is 14, not 13. You have an ace as well as 1, there is no 1 in a deck of cards.


Shows my knowledge about a deck of cards lol. So it seems as if the community is ok with both but, as Ryano pointed out mine can have duplicates. I am now reading about Shuffling so maybe next time I can try and use that.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,019
  • Joined: 11-December 07

Re: Deck of cards program (Which is more efficient)

Posted 23 March 2015 - 04:39 PM

Also, hearts and diamonds are always red. Spades and clubs are always black. It can also be confusing to reuse variables so it's best to just create new ones. How about this:

import java.util.Random;


public class Main {
	
	public static void main(String args[]){
		Random rand = new Random();
		
		String[] cardNumber = {"Ace", "1", "2", "3", "4", "5", "6", "7", 
				"8", "9", "10", "King", "Queen", "Jack"};
		String[] cardType = {"Spades", "Hearts", "Diamonds", "Clubs"};
		String[] cardColor = {"Black", "Red", "Red", "Black"};
		
		int counter = 0;
				
		do{
			int numberLength = cardNumber.length;
			int numberIndex = rand.nextInt(numberLength);
			String number = cardNumber[numberIndex];
			System.out.print(number + " of ");
		
			int typeLength = cardType.length;
			int typeIndex = rand.nextInt(typeLength);
			String type = cardType[typeIndex];
			System.out.print(type + " ");
		
			String typeColor = cardColor[typeIndex];
			System.out.print(typeColor);
			
			counter++;
			System.out.println();
		} while(counter < 4);
	}
}




You can clean it up further by removing some of the variables, combining the print and using a for loop.

import java.util.Random;

public class Main {

	public static void main(String args[]){
		Random rand = new Random();

		String[] cardNumber = {"Ace", "1", "2", "3", "4", "5", "6", "7", 
				"8", "9", "10", "King", "Queen", "Jack"};
		String[] cardType = {"Spades", "Hearts", "Diamonds", "Clubs"};
		String[] cardColor = {"Black", "Red", "Red", "Black"};

		for(int i = 0; i < 4; i++) {
			int numberIndex = rand.nextInt(cardNumber.length);
			int typeIndex = rand.nextInt(cardType.length);
			String number = cardNumber[numberIndex];
			String type = cardType[typeIndex];
			String typeColor = cardColor[typeIndex];
			System.out.println(number + " of " + type + " " + typeColor);
		}
	}
}




This still doesn't change the fact that you can choose the same card twice. Can you think how to solve that?
Was This Post Helpful? 1
  • +
  • -

#6 Spiked Penguin   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 107
  • Joined: 19-December 13

Re: Deck of cards program (Which is more efficient)

Posted 23 March 2015 - 07:31 PM

Use an if statement to check the indexValue of the color to match the suit if they don't match do an else
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11316
  • View blog
  • Posts: 19,358
  • Joined: 19-March 11

Re: Deck of cards program (Which is more efficient)

Posted 23 March 2015 - 08:29 PM

You can clean things up a lot easier by forgetting about the names and numbers entirely for now. They're not important, and they're distracting you from the important thing about a deck: there are fifty-two cards in it, and each one is distinct. See if you can work out how to arrange 52 cards in to four hands of thirteen cards each, without repetition - and don't worry about what they're called!

And by now you've been around long enough to start to get an idea of what good answers tend to look like, and what answers look ugly. Use that intuition: look at your answer and tell us what you like about it and what you don't like. I doubt very much that you'll invent the correct solution - and there is one - on your own, but at least you should be able to start thinking about what makes solutions good, and what makes them bad.
Was This Post Helpful? 2
  • +
  • -

#8 Spiked Penguin   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 107
  • Joined: 19-December 13

Re: Deck of cards program (Which is more efficient)

Posted 27 March 2015 - 08:05 AM

Sorry about not getting back to this thread sooner. I get about 30 - 40 minutes a day to work on programming when I'm not working. I have come up with a better solution to make it so I don't encounter the same kind of cards anymore but, to me the program looks a bit more sloppy but, here it is I do an have ArrayIndexOutOfBounds on line 27 error.

/*
 * - Deck (52 cards) Each card has 1 color, 1 number and 1 suit
 * - card colors
 * - card number
 * - card suit
 */

import java.util.Random;

public class Main {
	public static void main(String[] args){
		String[] deck = new String[52];
		
		String[] cardNumber = {"Ace", "2", "3", "4", "5",
				"6", "7", "8", "9", "10", "King", "Queen", "Jack"}; // 13
		
		String[] cardSuit = {"Hearts", "Spades", "Diamonds", "Clubs"};
		
		int cs = 0;
		int card = 0;
		
		//For every 4 numbers ++;
		for(int i = 0; i < deck.length; i++){
			
			// assign each card a Suit and Number
			for (int counter = 0; counter < 4; counter++){
				deck[card] = cardNumber[i] + " of " + cardSuit[counter];
				card++;
			}
		}
		
		// Shuffle deck
		for(int i = 0; i < deck.length; i++){

		}
		
		//print deck
		for(int i = 0; i < deck.length; i++){
			if(i % 13 == 0){
				System.out.println();
			}
			System.out.println(deck[i]);
		}
		
	}

}



Why this makes sense to me Jon, is that I make a string array to store each card (52). Then, have my nested for loop to go through each card. The first loop to get position of the card (which isn't really needed if I just make an int and do a post increment operator. Then, nested for loop I am using to stay in a position of 4 to get the 4 cards of the same number and different suit. e.g Ace of Hearts, Ace of Spades, Ace of Diamonds, Ace of Clubs.

After that I would shuffle the deck and then go to my final for loop which will print the deck using post increment after shuffling so it will always be random.

The flaws for it to me are that I am using to many variables and ints that I don't really think I need If I gave this some more thought I think I could figure a better solution.

The code is unfinished as I ran out of time today I will get back to you guys tonight after work on your thougts, thank you for helping to understand the process of this a little better.
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11316
  • View blog
  • Posts: 19,358
  • Joined: 19-March 11

Re: Deck of cards program (Which is more efficient)

Posted 27 March 2015 - 11:02 AM

Okay, so here's a thing that will make your life easier: notice that you can go from a number in the range 0..51 to a unique pair of integers (face, suit), and you can use that unique pair to look up the values in your array. That means you can write a function that takes a card's number (0..51) and spits out its name (eg, Jack of Spades).

So this makes your life easier because now you just have to produce an ordering for an array of 52 integers, and you're pretty much done. Specifically, it gets rid of the need to assign names to the cards

So now you can concentrate on this problem: write a function that randomly assigns each of the values 0..51 to an array of length 52, without duplication or repetition. The index is the order in the deck, and the value is the identity of the card at that spot in the deck. Voila, you have a deck, and you can use the lookup function that you just wrote (in paragraph 1) to tell you which card is which.

This will probably simplify your code, which you correctly identify as a good and useful thing to do.

Quote

The flaws for it to me are that I am using to many variables and ints that I don't really think I need If I gave this some more thought I think I could figure a better solution.


This is exactly the right sort of thing to be concerned about. Good stuff.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1