Instead of translating I've added comment in the code. But its mainly strings that are in Swedish.
I have trouble adding a feature where the game asks the user the amount of decks (each deck containing the standard 52 cards.)
Just like how the game ask if the deck should be shuffled or not the user should be asked how many decks the game should contain, the rest can be as it is for now.
I've tried (but ofc it didnt work):
int n = //amount of decks
int[] deck = new int[52*n];
Here is the program:
import java.util.Scanner;
public class KortSpel {
public static void main(String[] args) {
Boolean fortsatt = true;
while(fortsatt){
Scanner scan = new Scanner(System.in);
int[] deck = new int[52];
String[] suits = {"Hjärter", "Spader", "Ruter", "Klöver"}; //the suits
String[] ranks = {"Ess", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Knäckt", "Drottning", "Kung"}; //the rank of the cards
for( int i = 0; i < deck.length; i++) deck[i] = i;
System.out.print("Skriv dra för att dra korten annars avsluta."); //ask the user if he want to keep playing or not
String svar2 = scan.nextLine();
if (svar2.equalsIgnoreCase("Avsluta")){
fortsatt = false;
System.out.println("Du har nu avslutat."); //tells the user he has exit
}
else {
System.out.print("Vill du bland korten? (ja/nej) "); //ask the user if he want to shuffle the cards Y/N
String svar = scan.nextLine();
if (svar.equalsIgnoreCase("ja")) { // if shuffled
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;
}
for( int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Kort nummer " + deck[i] + ": " + suit + " " + rank);
}
}
else { //if not suffled
for( int i = 0; i < deck.length; i++) deck[i] = i;
for( int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Kort nummer " + deck[i] + ": " + suit + " " + rank);
}
}
}
}
}
}
All help is appreciated and since Im not very good with java, simplicity is preferred.
Thanks in advance!
All this gets a lot easier if you take up a formal Object Oriented model instead of informally defining a card.
What is a card? In this context, that's rather simple; it's a pair of values, one of which is a suit, and the other is a rank. So long as the suit and rank are both valid, both are cards. This gives us the following class:
public class Card{
public final String suit;
public final String rank;
public Card(String s, String r){
suit = s;
rank = r;
}
}
Before we go further, I would like to note here that using the type String for suit and rank is not a great idea; both have a finite set of potential values and as such can be better represented as an enumerated type (or Enum). We can define the Enums like this (though obviously you could change the words to be swedish):
public enum Suit{
HEARTS,
SPADES,
CLUBS,
DIAMONDS
}
public enum Rank{
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
}
Which changes our Card class to look like this:
public class Card{
public final Suit suit;
public final Rank rank;
public Card(Suit s, Rank r){
suit = s;
rank = r;
}
}
From here, creating a deck class is easy:
public class Deck{
private ArrayList<Card> deck; //An array of cards that represents this deck.
public Deck(){
deck = new ArrayList<Card>();
//For every combination of suit and rank, create and add a card to this deck
for(Suit s : Suit.values()){
for(Rank r : Rank.values()){
deck.add(new Card(s, r));
}
}
}
}
From here you can add functionality to the deck class for shuffling, drawing cards, etc.
For your original question, creating multiple decks is just a question of creating many single decks and adding all of their contents together. We can create a constructor that does that, and add that to our deck class:
public class Deck{
private ArrayList<Card> deck; //An array of cards that represents this deck.
public Deck(){
deck = new ArrayList<Card>();
//For every combination of suit and rank, create and add a card to this deck
for(Suit s : Suit.values()){
for(Rank r : Rank.values()){
deck.add(new Card(s, r));
}
}
}
/** Creates a deck that is the sum of all the cards in the input deck(s) */
public Deck(Deck... decks){
deck = new ArrayList<Card>();
for(Deck d : decks){
deck.addAll(d.deck);
}
}
}
Related
I am trying to create a Blackjack game. The game itself is pretty easy (at least my version). A player draws a card from a shuffled deck and calculates the sum of the cards which has been drawn. I have a decklist of cards with the type String and that is now a big problem for me. I have no clue how I can calculate the sum since they are of the type String. Do you have any guidelines on what I can do? The only solution I've figured out is really bad and is to compare the card with a String and give it a value. For example, drawnCard.equals("Four of hearts") = "4";
public class Player {
private String nickName;
private int playerNumOfCards;
ArrayList<Card> playerHand = new ArrayList<>();
public Player (String name){
this.nickName = name;
}
public String getNickName() {
return nickName;
}
public void addCard(Card aCard){
playerHand.add(aCard);
this.playerNumOfCards++;
}
public void getHandSum(){
}
public void getPlayerHand(){
for(Card cards: playerHand){
System.out.println(cards.toString());
}
}
}
public class DeckOfCards {
private Card[] deck;
private static final Random random = new Random();
private int currentCard; //index of next Card to be deal (0-51)
private static int NUMBER_OF_CARDS = 52; //Constant number of cards
public DeckOfCards(){
String [] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack","Queen", "King"};
String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
deck = new Card [NUMBER_OF_CARDS]; // create array with Cards (52)
currentCard = 0;
//Populate deck with Cards
for(int count = 0; count < deck.length; count++)
deck [count] = new Card(faces [count % 13], suits [count / 13]);
}
public void shuffleDeck(){
currentCard = 0;
for (int first = 0; first < deck.length; first++){
int second = random.nextInt(NUMBER_OF_CARDS); //Select a random card from number 0-51 (Number_of_cards)
//Loops through all the cards and swaps it with the "Second" card which is randomly chosen card from hte same list.
Card temp = deck[first];
deck [first] = deck [second];
deck [second] = temp;
}
}
public void getCardDeck(){
int start = 1;
for(Card k : deck) {
System.out.println("" + start + "/52 " + k);
start++;
}
}
public Card dealNextCard(){
//Get the top card
Card topCard = this.deck[0];
//shift all the subsequent cards to the left by one index
for(int currentCard = 1; currentCard < NUMBER_OF_CARDS; currentCard ++){
this.deck[currentCard-1] = this.deck[currentCard];
}
this.deck[NUMBER_OF_CARDS-1] = null;
//decrement the number of cards in our deck
this.NUMBER_OF_CARDS--;
return topCard;
}
}
public class Card {
private String face; //Face of card, i.e "King" & "Queen"
private String suit; //Suit of card, i.e "Hearts" & "diamonds"
public Card (String cardFace, String cardSuit){ //Constructor which initializes card's face and suit
this.face = cardFace;
this.suit = cardSuit;
}
public String toString(){ //return String representation of Card
return face + " of " + suit;
}
}
public class BlackJackGame {
public static void main(String[] args) {
DeckOfCards deck1 = new DeckOfCards();
Player player1 = new Player("mille");
deck1.shuffleDeck();
}
}
Create an enum to represent Face
enum Face {
Ace(number), //I don't know what number is for Ace and others.
Deuce(number),
Three(3),
Four(4),
Five(5),
Six(6),
Seven(7),
Eight(8),
Nine(9),
Ten(10),
Jack(number),
Queen(number),
King(number);
private final int number;
Faces(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
Change type of face from String to Face.
class Card {
private Face face;
private String suit;
public Card (Face cardFace, String cardSuit){card's face and suit
this.face = cardFace;
this.suit = cardSuit;
}
public String toString(){
return face + " of " + suit;
}
public int getNumber() {
return face.getNumber();
}
}
Add a method to get the face number from card class and then use it accordingly. You will need to change other parts or your project as well but I will leave that for you to do.
Also suggest using enum for Suit.
I couldn't see the deck list there, but if your card Strings always follow the same naming convention (i.e. "four of hearts") you could make your job a little easier by splitting each string by the " " space fields and then comparing the first word in the string to get the number (or corresponding 10 for king/queen etc..)
String cardName = "four of hearts" (or whatever the variable name is);
String[] parts = string.split(" ");
String number = parts[0];
thus number would equal "four" only instead of having to compare "four of hearts" to return the number 4.
Hope that helps
Recently, I've tried to create a Spades game in java. Ive managed to make the card and the deck class, but whenever I try to print out either a random card or a deck of cards, sans 2 of diamonds and clubs, I have an unexpected result. Here is my code:
Main class
package com.star.spades;
import com.star.cards.Card;
import com.star.cards.Deck;
public class Spades {
public static void main(String[] args) {
Deck deck = new Deck();
Card randCard = deck.drawCard();
System.out.println(deck);
System.out.println(randCard);
}
}
Card Class:
package com.star.cards;
public class Card
{
private int suit;
private int value;
private String [] suits = {"Diamonds", "Clubs", "Spades", "Hearts", };
private String [] values = {"Ace", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};
private String [] joker = {"Joker"};
private String [] jokerValue = {"Little", "Big"};
public Card(int suit, int value)
{
this.suit = suit;
this.value = value;
}
public String outputCard()
{
return value + "of" + suit;
}
public String outputJoker()
{
return value + " " + suit;
}
Deck Class:
package com.star.cards;
import java.util.ArrayList;
import java.util.Random;
public class Deck {
private int numberOfSuits = 4;
private int numberOfValues = 52;
private int jokers = 1;
private int jokerSize = 2;
private ArrayList<Card> deck;
private ArrayList <Card> hands;
public Deck()
{
deck = new ArrayList<Card>();
for(int i = 0; i < numberOfSuits; i++)
for(int j =0; j < numberOfValues; j++)
{
deck.add(new Card(i, j));
}
for(int i = 0; i < jokerSize; i++)
for(int j = 0; j < jokers; j++)
{
deck.add(new Card(i, j));
}
deck.remove(1);
deck.remove(13);
}
//draws random card and removes it from deck.
public Card drawCard()
{
Random rand = new Random();
int index = rand.nextInt(deck.size());
return deck.remove(index);
}
//deals 13 random cards to the player
public ArrayList<Card> dealCards()
{
for(int i = 0; i < 13; i++)
{
hands.add(drawCard());
}
return hands;
}
}
and here is the result:
com.star.cards.Deck#143b9a5f
com.star.cards.Card#5513dd59
Could you let me know what I need to do to print out my deck and cards on the console?
You are trying to print objects directly. By default, the toString() method, which is called by print, will print out the reference to your object. So that weird thing you are getting is actually code for the location in memory where your object is stored. If you want to print out something else (for example some properties of the class) you must override the toString() method in your object's class.
You need to either implement toString in your classes or call a specific method that returns a string representation. Currently when you do System.out.println(card) it prints card's instance toString but since you didn't implement it yourself, it uses the parent class that did implement it - Object. Since Object has no information about your specific object, it prints a generic name and reference location address string.
If you choose to implement toString, all you have to do is add the method to your classes, for instance in Card class:
#Override
public String toString() {
return value + " " + suit;
}
And the same for your Deck class (not sure what you want to print there). Your Spades class is left untouched.
You need to implement/override the toString method in your classes. You will find many examples online for how to do that. For your Card class, a simple version could look like this:
#Override
public String toString()
{
return "Card[suit=" + suits[suit] + ",value=" + values[value] + "]";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have most of my program, but I have hit the wall near the end, and I would love the help!
I have to write a program that picks four cards out of a deck of 52 and computes the sum. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11 respectively. The program should display the number of pick that yields the sum of 24.
What I have so far:
public class Exercise07_29 {
public static void main(String[] args){
//initialize everything
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"};
//initialize the cards
for(int i = 0; i< deck.length; i ++)
deck[i] = i;
//shuffle the cards
for(int i = 0; i < deck.length; i++){
//generate an index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
//display the four cards
for(int i = 0; i < 4; i++){
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println(rank + " of " + suit);
}
//initialize Ace Jack Queen King
int Ace, Jack, Queen, King;
//Assign a point vale to each
int[] points = {Ace = 1, Jack = 11, Queen = 12, King = 13};
//add the cards together and show output
}
}
I tried a loop for the addition, but am having trouble when it comes to adding a random output together....
Any and all help would be greatly appreciated! :)
import java.util.*;
public class Exercise07_29 {
public static void main(String[] args){
//initialize everything
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"};
List<String> pickedCards = new ArrayList<String>();
//initialize the cards
for(int i = 0; i< deck.length; i ++)
deck[i] = i;
//shuffle the cards
for(int i = 0; i < deck.length; i++){
//generate an index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
//display the four cards
for(int i = 0; i < 4; i++){
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println(rank + " of " + suit);
pickedCards.add(rank);
}
//initialize Ace Jack Queen King
int Ace, Jack, Queen, King;
//Assign a point vale to each
int[] points = {Ace = 1, Jack = 11, Queen = 12, King = 13};
//add the cards together and show output
int sum = 0;
int jack = 11;
int queen = 12;
int king = 13;
int ace = 1;
Iterator<String> iterator = pickedCards.iterator();
while(iterator.hasNext()) {
String rank = iterator.next();
System.out.println(rank);
if(rank.equalsIgnoreCase("Jack")){
sum = sum+jack;
}
else if(rank.equalsIgnoreCase("Queen")){
sum = sum+queen;
}
else if(rank.equalsIgnoreCase("King")){
sum = sum+king;
}
else if(rank.equalsIgnoreCase("Ace")){
sum = sum+ace;
}
else {
sum = sum+Integer.parseInt(rank);
}
}
System.out.println("Sum of picked cards is : "+sum);
}
}
I am not sure what is your problem here. If it is that you are wondering how to map the Ace, Jack, Queen and King to points then I would suggest the following.
Map<Integer, String> cardToPoints = new HashMap<Integer, String>();
cardToPoints.add(1, "Ace");
cardToPoints.add(11, "Jack");
cardToPoints.add(12, "Queen");
cardToPoints.add(13, "King");
Then if you need to print the text just search for the number in the map and if there is entry print it otherwise print the number you have.
As variation to this you can create class Card containing points for the card and it string representation and fill the map with this cards.
For example:
cardPoints.add(1, new Card(1, "Ace of Spades");
....
cardPoints.add(14, new Card(1, "Ace of Hearts");
cardPoints.add(15, new Card(2, "2 of Hearts");
Then just get the card from the map get the points and sum + print the exact cards. Easy and clean( apart of the 52 entries for the map initilization );
Java's an object-oriented language. You're probably a beginner, but here's an example for you to think about.
package cards;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Exercise07_29
* #author Michael
* #link https://stackoverflow.com/questions/31639964/pick-four-cards-and-compute-their-sum-java
* #since 7/26/2015 1:42 PM
*/
public class Exercise07_29 {
public static final int NUM_CARDS = 4;
public static void main(String[] args) {
Deck deck = new Deck();
List<Card> hand = new ArrayList<>();
int score = 0;
for (int i = 0; i < NUM_CARDS; ++i) {
Card card = deck.deal();
hand.add(card);
score += card.getRank().getValue();
}
System.out.println(hand);
System.out.println(score);
}
}
enum SUIT {
CLUB, DIAMOND, HEART, SPADE;
}
enum RANK {
ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);
private final int value;
RANK(int value) { this.value = value; }
public int getValue() {
return value;
}
}
class Card implements Comparable<Card> {
private final SUIT suit;
private final RANK rank;
public Card(SUIT suit, RANK rank) {
if (suit == null) throw new IllegalArgumentException("suit cannot be null");
if (rank == null) throw new IllegalArgumentException("rank cannot be null");
this.suit = suit;
this.rank = rank;
}
public SUIT getSuit() {
return suit;
}
public RANK getRank() {
return rank;
}
#Override
public int compareTo(Card other) {
if (this.getRank().equals(other.getRank())) {
return this.getSuit().compareTo(other.getSuit());
} else {
return this.getRank().getValue() - other.getRank().getValue();
}
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
return suit == card.suit && rank == card.rank;
}
#Override
public int hashCode() {
int result = suit.hashCode();
result = 31 * result + rank.hashCode();
return result;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("Card{");
sb.append("suit=").append(suit);
sb.append(", rank=").append(rank);
sb.append('}');
return sb.toString();
}
}
class Deck {
private List<Card> deck;
private Random random;
public Deck() {
this.init();
this.random = new Random();
}
public Deck(long seed) {
this.init();
this.random = new Random(seed);
}
private void init() {
this.deck = new ArrayList<Card>();
for (SUIT suit: SUIT.values()) {
for (RANK rank: RANK.values()) {
this.deck.add(new Card(suit, rank));
}
}
}
public Card deal() { return this.deal(true); }
public Card deal(boolean removeCard) {
int value = this.random.nextInt(this.deck.size());
return removeCard ? this.deck.remove(value) : this.deck.get(value);
}
}
The first class "cards" creates cards of different ranks and suits. It also includes a method to compare ranks of 2 cards.
public class card implements Comparable
{
private int suit;
private int rank;
public card(int card)
{rank = card % 13;
suit = card / 13;
}
public int getSuit()
{return suit;}
public int getRank()
{ return rank;}
public int compareTo(Object other)
{
card c = (card) other;
if (getRank() > c.getRank())
return 1;
else if (getRank() <c.getRank())
return -1;
return 0;
}
public String toString()
{
String[] ranks = {"2", "3", "4" , "5", "6", "7" , "8", "9" , "10", "Jack", "Queen", "King",
"Ace" };
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
return ranks[getRank()] + " of " + suits[getSuit()];
}
}
The second class gives me 10 random cards in the hand1 ArrayList.
import java.util.Collections;
import java.util.ArrayList;
import java.util.Random;
public class Gin
{
public static void main(String []args)
{
ArrayList<card> deck = new ArrayList<card>();
Random r = new Random();
for (int i = 0; i <52; i++)
deck.add (new card(i));
//deal 10 cards to a hand from the deck
ArrayList<card> hand1 = new ArrayList<card>();
card c;
for (int i=0; i<10;i++)
{
int pickACard = r.nextInt (deck.size());
c = deck.remove(pickACard);
hand1.add(c);
System.out.println (c);
}
System.out.println (" ");
}
}
If the hand is not in order I'll use Collections.shuffle(hand1) to bogo sort.But how can I check if
element 0 >= element 1 >= element 2>= element 3>= ect... in hand1? I tired writing a smaller
portion of the code that will compare element 0 and 1 in hand1 below:
if(hand1.get(0).compareTo(hand1.get(1)== 1))
System.out.print(hand1.get(0));
else if (hand1.get(0).compareTo(1)==-1)
System.out.print (hand1.get(1));
else
System.out.print ("Equal");
However I got the error "Incompatible types: card and int. Any help would be appreciated.
if(hand1.get(0).compareTo(hand1.get(1)== 1))
Here you're comparing hand1.get(0), which is the first alement of the list of cards, and is thus a card, with hand1.get(1)== 1. What you want is
if (hand1.get(0).compareTo(hand1.get(1)) > 0)
(because a comparison can return any positive number to mean first > second).
else if (hand1.get(0).compareTo(1)==-1)
And not you're comparing hand1.get(0), which is the first alement of the list of cards, and is thus a card, with1`, which is an int. It doesn't make sense either. You want
else if (hand1.get(0).compareTo(hand1.get(1)) < 0)
Also, note that yoy should use generics for your class. It should be declared as a Comparable<card>, and not as a raw Comparable:
public class card implements Comparable<card>
public int compareTo(card other) {
...
}
}
And of course, it should also respect the Java naming conventions: classes start with an uppercase letter:
public class Card implements Comparable<Card>
public int compareTo(Card other) {
...
}
}
I have created my deck of cards that deals every card and a suit until there is no card remaining. For my project, I need to split it up into 3 classes which includes a driver class. I first created one class with everything so I knew how to make it all work.
public class DeckOfCards2 {
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"};
// Initialize cards
for (int i = 0; i < deck.length; i++) {
deck[i] = i;
}
// Shuffle the 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 the all the cards
for (int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println( rank + " of " + suit);
}
}
}
Now trying to split it up into 3 classes. I am getting red sqiggle lines on ALL my deck/suit variables on my DeckOfCards class. I dont know how to fix it.
public class DeckOfCards {
private Card theCard;
private int remainingCards = 52;
DeckOfCards() {
theCard = new Card();
}
public void shuffle(){
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;
remainingCards--;
}
}
public void deal(){
for (int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println( rank + " of " + suit);
System.out.println("Remaining cards: " + remainingCards);
}
}
}
Card class:
public class Card {
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"};
Card() {
for (int i = 0; i < deck.length; i++) {
deck[i] = i;
}
}
}
Dealer class
public class Dealer {
public static void main(String[]args){
System.out.println("The deck will randomly print out a card from a full deck each time");
DeckOfCards player = new DeckOfCards();
player.deal();
}
}
As somebody else already said, your design is not very clear and Object Oriented.
The most obvious error is that in your design a Card knows about a Deck of Cards. The Deck should know about cards and instantiate objects in its constructor. For Example:
public class DeckOfCards {
private Card cards[];
public DeckOfCards() {
this.cards = new Card[52];
for (int i = 0; i < ; i++) {
Card card = new Card(...); //Instantiate a Card
this.cards[i] = card; //Adding card to the Deck
}
}
Afterwards, if you want you can also extend Deck in order to build different Deck of Cards (for example with more than 52 cards, Jolly etc.). For Example:
public class SpecialDeck extends DeckOfCards {
....
Another thing that I'd change is the use of String arrays to represent suits and ranks. Since Java 1.5, the language supports Enumeration, which are perfect for this kind of problems. For Example:
public enum Suits {
SPADES,
HEARTS,
DIAMONDS,
CLUBS;
}
With Enum you get some benefits, for example:
1) Enum is type-safe you can not assign anything else other than predefined Enum constants to an Enum variable. For Example, you could write your Card's constructor as following:
public class Card {
private Suits suit;
private Ranks rank;
public Card(Suits suit, Ranks rank) {
this.suit = suit;
this.rank = rank;
}
This way you are sure to build consistent cards that accept only values of your enumeration.
2) You can use Enum in Java inside Switch statement like int or char primitive data type (here we have to say that since Java 1.7 switch statement is allowed also on String)
3) Adding new constants on Enum in Java is easy and you can add new constants without breaking existing code.
4) You can iterate through Enum, this can be very helpful when instantiating Cards. For Example:
/* Creating all possible cards... */
for (Suits s : Suits.values()) {
for (Ranks r : Ranks.values()) {
Card c = new Card(s,r);
}
}
In order to not invent again the wheel, I'd also change the way you keep Cards from array to a Java Collection, this way you get a lot of powerful methods to work on your deck, but most important you can use the Java Collection's shuffle function to shuffle your Deck. For example:
private List<Card> cards = new ArrayList<Card>();
//Building the Deck...
//...
public void shuffle() {
Collections.shuffle(this.cards);
}
This is my implementation:
public class CardsDeck {
private ArrayList<Card> mCards;
private ArrayList<Card> mPulledCards;
private Random mRandom;
public enum Suit {
SPADES,
HEARTS,
DIAMONDS,
CLUBS;
}
public enum Rank {
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE;
}
public CardsDeck() {
mRandom = new Random();
mPulledCards = new ArrayList<Card>();
mCards = new ArrayList<Card>(Suit.values().length * Rank.values().length);
reset();
}
public void reset() {
mPulledCards.clear();
mCards.clear();
/* Creating all possible cards... */
for (Suit s : Suit.values()) {
for (Rank r : Rank.values()) {
Card c = new Card(s, r);
mCards.add(c);
}
}
}
public static class Card {
private Suit mSuit;
private Rank mRank;
public Card(Suit suit, Rank rank) {
this.mSuit = suit;
this.mRank = rank;
}
public Suit getSuit() {
return mSuit;
}
public Rank getRank() {
return mRank;
}
public int getValue() {
return mRank.ordinal() + 2;
}
#Override
public boolean equals(Object o) {
return (o != null && o instanceof Card && ((Card) o).mRank == mRank && ((Card) o).mSuit == mSuit);
}
}
/**
* get a random card, removing it from the pack
* #return
*/
public Card pullRandom() {
if (mCards.isEmpty())
return null;
Card res = mCards.remove(randInt(0, mCards.size() - 1));
if (res != null)
mPulledCards.add(res);
return res;
}
/**
* Get a random cards, leaves it inside the pack
* #return
*/
public Card getRandom() {
if (mCards.isEmpty())
return null;
Card res = mCards.get(randInt(0, mCards.size() - 1));
return res;
}
/**
* Returns a pseudo-random number between min and max, inclusive.
* The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* #param min Minimum value
* #param max Maximum value. Must be greater than min.
* #return Integer between min and max, inclusive.
* #see java.util.Random#nextInt(int)
*/
public int randInt(int min, int max) {
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = mRandom.nextInt((max - min) + 1) + min;
return randomNum;
}
public boolean isEmpty(){
return mCards.isEmpty();
}
}
Here is some code. It uses 2 classes (Card.java and Deck.java) to accomplish this issue, and to top it off it auto sorts it for you when you create the deck object. :)
import java.util.*;
public class deck2 {
ArrayList<Card> cards = new ArrayList<Card>();
String[] values = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] suit = {"Club", "Spade", "Diamond", "Heart"};
static boolean firstThread = true;
public deck2(){
for (int i = 0; i<suit.length; i++) {
for(int j=0; j<values.length; j++){
this.cards.add(new Card(suit[i],values[j]));
}
}
//shuffle the deck when its created
Collections.shuffle(this.cards);
}
public ArrayList<Card> getDeck(){
return cards;
}
public static void main(String[] args){
deck2 deck = new deck2();
//print out the deck.
System.out.println(deck.getDeck());
}
}
//separate class
public class Card {
private String suit;
private String value;
public Card(String suit, String value){
this.suit = suit;
this.value = value;
}
public Card(){}
public String getSuit(){
return suit;
}
public void setSuit(String suit){
this.suit = suit;
}
public String getValue(){
return value;
}
public void setValue(String value){
this.value = value;
}
public String toString(){
return "\n"+value + " of "+ suit;
}
}
Very Simple code to generate deck off Card:
class Card{
private final String suit;
private final String rank;
public Card(String suit, String rank){
this.suit = suit;
this.rank = rank;
}
#Override
public String toString() {
return "Card [suit=" + suit + ", rank=" + rank + "]";
}
}
class DeckOfCard{
private static final String suits[] = {"club", "diamond", "heart", "spade"};
private static final String ranks[] = {null,"ace", "deuce", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"};
private final ArrayList<Card> cards;
public DeckOfCard(){
cards = new ArrayList<Card>();
for (int i = 0; i<suits.length; i++) {
for(int j=0; j<ranks.length; j++){
this.cards.add(new Card(suits[i],ranks[j]));
}
}
//Shuffle after the creation
Collections.shuffle(this.cards);
}
public ArrayList<Card> getCards() {
return cards;
}
}
public class CardPuzzle {
public static void main(String[] args) {
DeckOfCard deck = new DeckOfCard();
ArrayList<Card> cards = deck.getCards();
for(Card card:cards){
System.out.println(card);
}
}
}
There is something wrong with your design. Try to make your classes represent real world things. For example:
The class Card should represent one card, that is the nature of a "Card". The Card class does not need to know about Decks.
The Deck class should contain 52 Card objects (plus jokers?).
First you have an architectural issue with your classes. You moved the property deck inside your class Card. But of couse it is a property of the card deck and thus has to be inside class DeckOfCards. The initialization loop should then not be in the constructor of Card but of your deck class. Moreover, the deck is an array of int at the moment but should be an array of Cards.
Second, inside method Deal you should refer to suits as Card.suits and make this member static final. Same for ranks.
And last, please stick to naming conventions. Method names are always starting with a lower case letter, i.e. shuffle instead of Shuffle.
There are many errors in your code, for example you are not really calling your deck by just typing deck in your Shuffle method. You can only call it by typing theCard.deck
I have changed your shuffle method:
public void Shuffle(){
for (int i = 0; i < theCard.deck.length; i++) {
int index = (int)(Math.random()*theCard.deck.length );
int temp = theCard.deck[i];
theCard.deck[i] = theCard.deck[index];
theCard.deck[index] = temp;
remainingCards--;
}
}
Also, as it is said you have structural problem. You should name classes as you understand in real life, for example, when you say card, it is only one card, when you say deck it is supposed to be 52+2 cards. In this way your code would be more understandable.
There is a lot of error in your program.
Calculation of index. I think it should be Math.random()%deck.length
In the display of card. According to me, you should make a class of card which has rank suit and make the array of that class type
If you want I can give you the Complete structure of that but it is better if u make it by yourself
I think the solution is just as simple as this:
Card temp = deck[cardAindex];
deck[cardAIndex]=deck[cardBIndex];
deck[cardBIndex]=temp;
public class shuffleCards{
public static void main(String[] args) {
String[] cardsType ={"club","spade","heart","diamond"};
String [] cardValue = {"Ace","2","3","4","5","6","7","8","9","10","King", "Queen", "Jack" };
List<String> cards = new ArrayList<String>();
for(int i=0;i<=(cardsType.length)-1;i++){
for(int j=0;j<=(cardValue.length)-1;j++){
cards.add(cardsType[i] + " " + "of" + " " + cardValue[j]) ;
}
}
Collections.shuffle(cards);
System.out.print("Enter the number of cards within:" + cards.size() + " = ");
Scanner data = new Scanner(System.in);
Integer inputString = data.nextInt();
for(int l=0;l<= inputString -1;l++){
System.out.print( cards.get(l)) ;
}
}
}
import java.util.List;
import java.util.ArrayList;
import static java.lang.System.out;
import lombok.Setter;
import lombok.Getter;
import java.awt.Color;
public class Deck {
private static #Getter List<Card> deck = null;
final int SUIT_COUNT = 4;
final int VALUE_COUNT = 13;
public Deck() {
deck = new ArrayList<>();
Card card = null;
int suitIndex = 0, valueIndex = 0;
while (suitIndex < SUIT_COUNT) {
while (valueIndex < VALUE_COUNT) {
card = new Card(Suit.values()[suitIndex], FaceValue.values()[valueIndex]);
valueIndex++;
deck.add(card);
}
valueIndex = 0;
suitIndex++;
}
}
private enum Suit{CLUBS("Clubs", Color.BLACK), DIAMONDS("Diamonds", Color.RED),HEARTS("Hearts", Color.RED), SPADES("Spades", Color.BLACK);
private #Getter String name = null;
private #Getter Color color = null;
Suit(String name) {
this.name = name;
}
Suit(String name, Color color) {
this.name = name;
this.color = color;
}
}
private enum FaceValue{ACE(1), TWO(2), THREE(3),
FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT (8), NINE(9), TEN(10),
JACK(11), QUEEN(12), KING(13);
private #Getter int cardValue = 0;
FaceValue(int value) {
this.cardValue = value;
}
}
private class Card {
private #Getter #Setter Suit suit = null;
private #Getter #Setter FaceValue faceValue = null;
Card(Suit suit, FaceValue value) {
this.suit = suit;
this.faceValue = value;
}
public String toString() {
return getSuit() + " " + getFaceValue();
}
public String properties() {
return getSuit().getName() + " " + getFaceValue().getCardValue();
}
}
public static void main(String...inputs) {
Deck deck = new Deck();
List<Card> cards = deck.getDeck();
cards.stream().filter(card -> card.getSuit().getColor() != Color.RED && card.getFaceValue().getCardValue() > 4).map(card -> card.toString() + " " + card.properties()).forEach(out::println);
}
}