I am Looking for some insight on how to make my card game know when the decks I have created have run out of cards.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CardDealer {
public static void main(String[] args) {
System.out.println("Welcome to Shuffle!");
Scanner console = new Scanner(System.in);
Map<Integer, Deck> decks = new HashMap<Integer, Deck>();
int deckNum = getNumberOfDecks(console);
int cardNum = getNumberOfCards(console);
String readString = console.nextLine();
while(readString!=null)
{
System.out.println(readString);
if(readString.equals(""))
for(int i = 0; i < deckNum; i++){
decks.put(i, new Deck(cardNum));
}
for(int key : decks.keySet()){
System.out.println("Deck # "+ key + " " +decks.get(key).toString());
if(key >= 52)
key = key + cardNum;
else if(key == 51)
System.out.println("All GONE!");
}
if(console.hasNextLine())
readString = console.nextLine();
else
readString = null;
}
}
/**
* Get number of decks
* #param console
* #return
*/
public static int getNumberOfDecks(Scanner console){
int tempNumDecks = 0;
boolean isOk = false;
System.out.println("How many decks would you like to use?");
do{
try{
String userInput = console.nextLine();
tempNumDecks = Integer.parseInt(userInput);
} catch(Exception e){
System.out.println ("Invalid input. Enter a number > 0");
System.out.println("Enter an integer greater than 0:");
}
isOk = true;
}while(isOk == false);
return tempNumDecks;
}
/**
* Get number of cards
* #param console
* #return
*/
public static int getNumberOfCards(Scanner console){
int tempNumCards = 0;
boolean isOk = false;
System.out.println("How many cards would you like to deal?");
do{
try{
String userInput = console.nextLine();
tempNumCards = Integer.parseInt(userInput);
if(tempNumCards <= 0) {
System.out.println ("Invalid input. Enter a number > 0");
} else{
isOk = true;
}//end else
}//end try
catch(Exception e){
System.out.println("Enter an integer greater than 0:");
}//end catch
}while(isOk == false);
return tempNumCards;
}//end getNumberOfCards()
}
Deck class:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
public class Deck {
/** Cards instance **/
private List<Card> cards;
/** Random generator **/
private Random r;
public Deck(int numberOfCards)
{
cards = new ArrayList<Card>(numberOfCards);
int tempCardIndex = 0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards.add( new Card(a,b) );
}
}
/** Shuffle this deck **/
shuffle(cards);
/** Slice the deck by our number of cards **/
cards = cards.subList(0, numberOfCards);
}
/**
* Shuffle a list
* #param list
*/
public void shuffle(List<Card> list) {
if (r == null) {
r = new Random();
}
shuffle(list, r);
}
/**
* Shuffle a list with Random rnd
* #param list
* #param rnd
*/
public void shuffle(List<Card> list, Random rnd) {
int size = list.size();
Object[] arr = list.toArray();
// Shuffle array
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
// Dump array back into list
ListIterator<Card> it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set((Card) arr[i]);
}
}
/**
* Swap elements in List
* #param list
* #param i
* #param j
*/
public void swap(List<Card> list, int i, int j) {
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
/**
* Swap Elements in Array
* #param arr
* #param i
* #param j
*/
private static void swap(Object[] arr, int i, int j) {
Object tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
/**
* Draw a card
* #return
*/
public Card drawFromDeck()
{
return cards.remove(0);
}
/** Get total Cards
*
* #return cards size
*/
public int getTotalCards()
{
return cards.size();
}
/**
* to String override
*/
public String toString(){
return cards.toString();
}
}
Card Class:
public class Card
{
private int rank, suit;
private final String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private final String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
public Card(int suit, int rank)
{
this.rank=rank;
this.suit=suit;
}
public #Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
}
So this prints a hashmap of the user specified amount of cards in a user specified amount of decks.
Like so:
Deck # 0 [Jack of hearts, 3 of spades, 7 of spades, 6 of hearts]
Deck # 1 [6 of hearts, 9 of clubs, King of spades, 2 of clubs]
Deck # 2 [5 of spades, King of clubs, Queen of clubs, King of spades]
Deck # 3 [5 of spades, 6 of hearts, 9 of clubs, 5 of diamonds]
It does this every time the user presses enter. My problem is, I want it to stop allowing the user to draw more cards from the deck(s) when all of the cards from each of the decks runs out. Is this possible? I have been told by a colleague to use a boolean somehow to make this work but I'm stumped.
Thanks for the help guys.
You could simply call the method that checks the number of cards, getTotalCards() before calling drawFromDeck(). Then if it's 0, you know that no cards are left.
Note that with such questions, it's always best to show us your attempt to solve it as part of your question, else you're just cheating yourself out of a learning opportunity, and preventing us from seeing what may be wrong with your logic.
Related
I am creating an instance deck of cards that should display all 52 cards when built, then it should shuffle the cards and deal 5. After this the menu should prompt for either another 5 to be dealt, or the deck to be reshuffled, which adds all the drawn cards back in, or to exit the application. However, when I run the app, it just displays null for all 52 cards, and each card that is dealt. The application worked and almost met all the specification before I talked with my instructor, and what I have now is where I got during/ after our talk. I'm not sure what I'm doing wrong and could really use the help. Just for clarification I will post the requirements, then the code. I appreciate the help.
At startup, it constructs a Deck of Cards
52 distinct cards running from Ace - King, of suit Heart, Club, Diamond, or Spade
Shuffle the cards!!
For proof, print out all 52 cards to the console in a useful, readable way.
Then, deal the top five cards to the console (meaning print them to the console)
After all of this, allow the user choose between dealing the next five cards, reshuffling the deck, or quitting the application.
If the user chooses to deal the next 5 cards, do so based on which cards have not yet been dealt. DO NOT RESHUFFLE.
If the user chooses to reshuffle, simply repeat the process of shuffling, printing the deck, and dealing the top five cards.
If the user chooses to quit the application, simply end the app.
The code I'm practicing encapsulation so I will post each class broken up as is in eclipse.
Driver
public class Driver {
public static void main(String[] args) throws IOException {
DeckRun.run();
}
}
DeckRun
public class DeckRun {
static Card[] d1 = new Card[52];
public static void run() throws IOException {
printDeck();
System.out.println("");
Deal.dealCards(d1);
System.out.println("");
menu();
}
public static void printDeck() {
for (Card c : d1) {
System.out.println(c);
}
}
public static void menu() throws IOException{
BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));
int menu = 0;
do {
System.out.println("Press 1 to be dealt 5 random cards.");
System.out.println("Press 2 to shuffle all the cards back into the deck.");
System.out.println("Press 3 to quit the application.");
String input = readRacer.readLine();
menu = Integer.parseInt(input);
switch (menu) {
case 1: Deal.dealCards(d1);
break;
case 2: System.out.println("The deck has been shuffled.");
Deck[] d1 = new Deck[52];
break;
case 3: System.out.println("I'm not bad, I'm just drawn that way.");
break;
}
} while (menu != 3);
}
}
Card
public class Card {
private Rank rank; // Variable to assign a card its rank.
private Suit suit; // Variable to assign a card its suit.
public Card (Rank rank, Suit suit) { // Constructor to build a card.
this.rank = rank;
this.suit = suit;
}
public Rank getRank() { // Retrieves the card's rank from the enum Rank.
return rank;
}
public Suit getSuit() { // Retrieves the card's suit from the enum Suit.
return suit;
}
#Override
public String toString() { //
return rank + " OF " + suit;
}
}
Deck
public class Deck {
private Card[] cards;
public Deck() {
int numberOfRanks = 13;
int numberOfSuits = 4;
int numberOfCards = numberOfRanks * numberOfSuits;
Rank[] rank = Rank.values();
Suit[] suit = Suit.values();
cards = new Card[numberOfCards];
for (int i = 0; i < numberOfRanks; i++) {
for (int j = 0; j < numberOfSuits; j++) {
cards[j * numberOfRanks + i] = new Card(rank[i], suit[j]);
}
}
}
public void shuffleCards() {
Random rand = new Random();
for (int c = rand.nextInt(6) + 5; c > 0; c--) {
for (int i = cards.length - 1; i > 0; i--) {
int index = rand.nextInt(i + 1);
Card card = cards[index];
cards[index] = cards[i];
cards[i] = card;
}
}
}
}
Deal
public class Deal {
private static int counter = 0;
public static void dealCards(Card[] d1) {
for (int i = 0; i < 5; i++) {
counter++;
System.out.println(d1[counter]);
if (counter == 50) {
System.out.println("Almost all cards have been used, please reshuffle.");
}
}
}
}
Rank (Enum)
public enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN, KING, }
Suit (Enum)
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES, }
Not really sure where the issue is, but what I've changed today is after realizing I need an instance of the deck, my professor helped me understand that instead of a method for the deck of cards in class Deck, i needed a constructor. I believe I'm going down the right path but I am just utterly stumped. Any help is appreciated and if you can explain my errors so I may learn and progress even more thanks.
In main you are calling DeckRun.run(), let's take a look at your definition of this method. It calls printDeck which iterates over an array of cards (d1), which is never populated. That's why you are getting a bunch of NULLs.
Your DeckRun should have an instance of Deck instead of cards array
public class DeckRun {
private Deck deck = new Deck();
public static void run() throws IOException {
printDeck();
System.out.println("");
Deal.dealCards(deck.getCards());
System.out.println("");
menu();
}
public static void printDeck() {
for (Card c : deck.getCards()) {
System.out.println(c);
}
}
public static void menu() throws IOException{
BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));
int menu = 0;
do {
System.out.println("Press 1 to be dealt 5 random cards.");
System.out.println("Press 2 to shuffle all the cards back into the deck.");
System.out.println("Press 3 to quit the application.");
String input = readRacer.readLine();
menu = Integer.parseInt(input);
switch (menu) {
case 1:
Deal.dealCards(deck.getCards());
break;
case 2:
System.out.println("The deck has been shuffled.");
deck.shuffleCards();
break;
case 3:
System.out.println("I'm not bad, I'm just drawn that way.");
break;
}
} while (menu != 3);
}
}
you also need to add a getter for cards in Deck
so your problem lays in this loop
for (int i = 0; i < numberOfRanks; i++) {
for (int j = 0; j < numberOfSuits; j++) {
cards[j * numberOfRanks + i] = new Card(rank[i], suit[j]);
}
}
J*numOfRanks +i is basically saying 0*4 = 0+0 = 0
and so on, causing problems. To make this simpler i would recommend a 2D array, making 4 columns for the suits and 13 rows for the ranks then a similar loop would work perfectly. please let me know if this helps!
Okay guys. The answer was simple. I deleted the deal class, and moved the method to the deck class. I initialized the counter as a private, class-level int, which fixed the repetition issue. I will post the finished code below, so you can see.
DeckRun Class
public class DeckRun {
static Deck d1 = new Deck();
public static void run() throws IOException {
printDeck();
System.out.println("");
d1.dealCards();
System.out.println("");
menu();
}
public static void printDeck() {
System.out.println(d1.toString());
}
public static void menu() throws IOException{
BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));
int menu = 0;
do {
System.out.println("Press 1 to be dealt 5 random cards.");
System.out.println("Press 2 to shuffle all the cards back into the deck.");
System.out.println("Press 3 to quit the application.");
String input = readRacer.readLine();
menu = Integer.parseInt(input);
switch (menu) {
case 1: d1.dealCards();
System.out.println("");
break;
case 2: System.out.println("The deck has been shuffled.");
d1.shuffleCards();
System.out.println("");
break;
case 3: System.out.println("I'm not bad, I'm just drawn that way.");
break;
}
} while (menu != 3);
}
}
Deck Class
public class Deck {
private Card[] cards;
public Deck() {
int numberOfRanks = 13;
int numberOfSuits = 4;
int numberOfCards = numberOfRanks * numberOfSuits;
Rank[] rank = Rank.values();
Suit[] suit = Suit.values();
cards = new Card[numberOfCards];
for (int i = 0; i < numberOfRanks; i++) {
for (int j = 0; j < numberOfSuits; j++) {
cards[i * numberOfSuits + j] = new Card(rank[i], suit[j]);
}
}
}
public void shuffleCards() {
Random rand = new Random();
counter = 0;
for (int c = rand.nextInt(6) + 5; c > 0; c--) {
for (int i = cards.length - 1; i > 0; i--) {
int index = rand.nextInt(i + 1);
Card card = cards[index];
cards[index] = cards[i];
cards[i] = card;
}
}
}
private int counter = 0;
public void dealCards() {
try{
for (int i = 0; i < 5; i++) {
counter++;
System.out.println(cards[counter]);
if (counter == 50) {
System.out.println("Almost all cards have been used, please reshuffle.");
// Either return 1 card or an array of 5 cards.
}
}
} catch (ArrayIndexOutOfBoundsException aioobe){
System.out.println("Caught an ArrayIndexOutOfBoundsException. Reshuffling deck.");
shuffleCards();
}
}
#Override
public String toString() {
String deckOfCards = "";
for (Card c : cards) {
deckOfCards += c.toString() + "\n";
}
return deckOfCards;
}
}
That is all I had to fix, but I appreciate the help. Thanks everyone.
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);
}
}
I have an assignment to create a deck of cards and deal five random cards. I finally got it "working" but I need help with a couple things. One I just read that java now has a shuffle method to shuffle a list. We're on arrays right now so I think arrays were wanted is there a way to use the shuffle method with an array? If not can someone guide me in switching to a list? I think it will help with answer my next question. The only thing left for me to do is somehow state how many cards are left in the deck after the 5 random cards are dealt. However, seeing as how I'm using an array and you cannot remove items from an array because it's fixed (right?) I wanted to see if there was a way to simulate removing the cards from the array so that they aren't dealt again? Or would it just be easier to turn the arrays into a list/collection?
Here is my code. Any help is greatly appreciated. Also, I'll take any suggestions on cleaning the code up too...
public class CardGame {
public static void main (String [] args) {
DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle(1000);
Card b;
for (int i = 0; i < 5; i++) {
b = deck.deal();
System.out.println("Deal a card: " + b);
}
}
}
class Card {
public static final int SPADE = 4;
public static final int HEART = 3;
public static final int CLUB = 2;
public static final int DIAMOND = 1;
private static final String[] Suit = {"*", "Hearts", "Clubs", "Spades", "Diamonds"};
private static final String[] Rank = {"*", "*", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
private int cardSuit;
private int cardRank;
public Card( int suit, int rank ) {
if ( rank == 1 )
cardRank = 14; // Give Ace the rank 14
else
cardRank = (int) rank;
cardSuit = (int) suit;
}
public int suit() {
return ( this.cardSuit );
}
public String suitStr() {
return( this.Suit[ this.cardSuit ] );
}
public int rank() {
return ( this.cardRank );
}
public String rankStr() {
return ( Rank[ this.cardRank ] );
}
public String toString() {
return ( Rank[ this.cardRank ] + " of "+ Suit[ this.cardSuit ] );
}
}
class DeckOfCards {
public static final int NEWCARDS = 52;
private Card[] deckOfCards; // Contains all 52 cards
private int currentCard; // deal THIS card in deck
public DeckOfCards( ) {
deckOfCards = new Card[ NEWCARDS ];
int i = 0;
for (int suit = Card.DIAMOND; suit <= Card.SPADE; suit++)
for ( int rank = 1; rank <= 13; rank++ )
deckOfCards[i++] = new Card(suit, rank);
currentCard = 0;
}
//shuffle(n): shuffle the deck
public void shuffle(int n) {
int i, j, k;
for ( k = 0; k < n; k++ ){
i = (int) ( NEWCARDS * Math.random() ); // Pick 2 random cards
j = (int) ( NEWCARDS * Math.random() ); // in the deck
//swap these randomly picked cards
Card temp = deckOfCards[i];
deckOfCards[i] = deckOfCards[j];
deckOfCards[j] = temp;
}
currentCard = 0; // Reset current card to deal
}
//deal(): deal deckOfCards[currentCard] out
public Card deal() {
if (currentCard < NEWCARDS) {
return ( deckOfCards[currentCard++] );
}
else{
System.out.println("Out of cards error");
return ( null ); // Error;
}
}
public String toString() {
String s = "";
int k;
k = 0;
for ( int i = 0; i < 4; i++ ) {
for ( int j = 1; j <= 13; j++ )
s += (deckOfCards[k++] + " ");
s += "\n";
}
return (s);
}
}
After all the help I decided to rewrite my code and here is what I got but I'm having a little trouble bringing it all together and making it work!
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CardGame {
public static void main (String [] args) {
DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle();
Card b;
for (int i = 0; i < 5; i++) {
b = deck.deal();
System.out.println("Deal a card: " + b);
}
}
}
class Card {
enum Suit {
HEARTS(1),
CLUBS(2),
DIAMONDS(3),
SPADES(4);
private int suitValue;
private Suit (int suitValue)
{
this.suitValue = suitValue;
}
public int getSuitValue()
{
return suitValue;
}
}
private Suit suit;
private Value value;
public Card (Suit suit, Value value)
{
this.suit = suit;
this.value = value;
}
public Suit getSuit() {
return suit;
}
public Value getValue() {
return value;
}
/*
public int compareTo(Card o) {
return 0;
}
} */
class DeckOfCards
{
private List<Card> cards = new ArrayList<Card>();
public DeckOfCards () {
for (Suit suit : Suit.values()) {
for (Value value : Value.values()) {
Card card = new Card(suit, value);
cards.add(card);
}
}
}
public List<Card> getCards() {
return cards;
}
public void shuffleDeckOfCards() {
Collections.shuffle(cards);
}
public String toString() {
return this.Value + " of "+ this.Suit;
}
}
Here is a fully working sample program which demonstrates all the things you need - Shuffling and removing cards from an array.
Sample Card class:
public class Card {
private String value = "";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Card(String value) {
super();
this.value = value;
}
}
Sample program:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CardGame {
public static void main(String[] args) {
List<Card> pack = new ArrayList<Card>();
Card c1 = new Card("c1");
Card c2 = new Card("c2");
Card c3 = new Card("c3");
pack.add(c1);
pack.add(c2);
pack.add(c3);
System.out.print("List : ");
CardGame.displayCardList(pack);
Card[] packArr = cardListToArray(pack);
System.out.print("Array : ");
CardGame.displayCardArray(packArr);
// http://stackoverflow.com/questions/4228975/how-to-randomize-arraylist
long seed = System.nanoTime();
Collections.shuffle(pack, new Random(seed));
System.out.print("Shuffle List : ");
CardGame.displayCardList(pack);
packArr = cardListToArray(pack);
System.out.print("Shuffle Array : ");
CardGame.displayCardArray(packArr);
System.out.print("Remove Card from Array : ");
CardGame.removeCardFromArray(packArr, new Card("c1"));
CardGame.displayCardArray(packArr);
}
public static boolean removeCardFromArray(Card[] packArr, Card card) {
boolean cardFound = false;
for (int i = 0; i < packArr.length; i++) {
if (packArr[i].getValue().equalsIgnoreCase(card.getValue())) {
packArr[i] = null;
cardFound = true;
break;
}
}
return cardFound;
}
public static Card[] cardListToArray(List<Card> pack) {
Card[] packArr = new Card[pack.size()];
for (int i = 0; i < pack.size(); i++) {
packArr[i] = pack.get(i);
}
return packArr;
}
public static void displayCardList(List<Card> pack) {
for (Card c : pack) {
System.out.print(c.getValue() + ", ");
}
System.out.println();
}
public static void displayCardArray(Card[] packArr) {
for (Card c : packArr) {
if (c == null) {
System.out.print("REMOVED, ");
} else {
System.out.print(c.getValue() + ", ");
}
}
System.out.println();
}
}
Card
In the Card class the suits should be an enum. You can create the enum like this.
enum Suit {
SPADE,
HEART,
CLUB,
DIAMOND
};
Then the class should hold a variable of it's current suit like
Suit suit;
You can change the value of it very easily.
suit = Suit.HEART;
If you must use an index to access a suit you could use
suit = Suit.values()[index];
When getting the rank of the card you could use
return Rank[suit.ordinal()];
The name of the functions in the Card class are not the typical names you should use. Some of these should be getters. I would recommend names like getSuit() and getSuitName(), you want names that just by reading them you can understand what they do. Names like suit() are a little confusing, they don't tell you what they are doing with suit.
DeckOfCards
In the DeckOfCards class, when initializing the deck use a for each loop for the suits.
for (Suit suit : Suits.values()) {
...
}
This will loop over every suit.
If you wanted to switch over to a List you should declare your private variable deckOfCards to be an ArrayList.
private List<Card> deckOfCards = new ArrayList<>();
If you haven't worked with lists before, this is creating a list of Card objects. The diamond operator (<>) assumes the object type from the type you specified before (Card) This is a java 7 feature, if you are working with older version you would use
new ArrayList<Card>();
Instead of tracking your position in the deck, you add a card with the add() function
deckOfCards.add(card);
This would allow you to use Collections.shuffle(deckOfCards) to shuffle the cards around randomly.
To access a card, you use the get(int i) function
deckOfCards.get(index);
If you are still using arrays, when you are shuffling use a Random object. Throw a
Random random = new Random();
in the begging of your class. The inside of the for loop could look like this
int i = random.nextInt(NEWCARDS);
int j = random.nextInt(NEWCARDS);
Card temp = deckOfCards[i];
deckOfCards[i] = deckOfCards[j];
deckOfCards[j] = temp;
In the shuffle function I would change the names of i, j, and k. Variable K should be called i because it is the iterator. Variable j and k should be renamed to more meaningful names.
If you want to convert Arrays to lists you can use
Arrays.asList(new ArrayList<Card>());
And the opposite of that, to convert back to an array.
list.toArray();//This will return an array of Objects[]
Card[] cards = new Card[NEWCARDS];
list.toArray(cards)//This will store the list into the array cards.
//Note if the lists is bigger than the array it is storing it in, the function will return a new array that can hold all of the elemnents in the list.
Note: In these example I have been using ArrayList, there are other implementations of the List class which you may want to research.
Personal Preference
I would remove the parenthesis off of the return statements except on your toString() for the Card class.
You have some extra spaces when using parenthesis in your code such as your DeckOfCards constructor. I would make it like so
public DeckOfCards() {
There also shouldn't be spaces surrounding the stuff inside parenthesis such as
i = (int) ( NEWCARDS * Math.random() );
//should be
i = (int) (NEWCARDS * Math.random());
I have to write a program to play a game of poker between two 'human' players. I have the main method (dealer) set up to ask for the first player's bet but I haven't been able to figure out how to call upon the Deck class to the deal method. I tried deck.deal(), etc. but nothing seems to go through. Ill post the classes that are involved below and I've taken note of where the code should call upon the deck in the Dealer class
public class Dealer
{
private Deck deck;
private Player[] players;
private String n;
Scanner scan = new Scanner(System.in);
public static final int NUMBER_OF_PLAYERS = 2;
/**
* Default constructor - creates and populates the Player array and the
* Deck object.
*/
public Dealer()
{
players = new Player[ NUMBER_OF_PLAYERS ]; //IMPORTANT players is the array
deck = new Deck();
// populate the array of players
} // constructor
/**
* Outermost level of abstraction for the poker game
*/
public void play()
{
System.out.println("Welcome to Poker!");
//Getting First Names
System.out.println("\nEnter first player's name: ");
String name = scan.nextLine();
players[0] = new Player(name);
players[0].getName(name);
//Getting Second Name
System.out.println("Enter second player's name: ");
name = scan.nextLine();
players[1] = new Player(name);
players[1].getName(name);
//First Player Goes
System.out.println(players[0].getName(n) + "'s Turn\n");
//Bet
System.out.println("Enter bet: ");
int bet = players[0].getBet(); //IMPORTANT players is the array and can call Player method as own
//Able to bet?
while(!players[0].bet(bet) || !players[1].bet(bet))
{
bet = 20;
if(!players[0].bet(bet) || !players[1].bet(bet))
{
bet = 1;
}
}
//Draw Cards/Deck
//*****NEED TO DEAL CARDS HERE*****
//*****DEAL AN ARRAY OF CARD[5] TO EACH PLAYERS[0] AND [1]******
//Compare
//Add to winner
//Add to loser
//Winner Goes ^^
} // method play
}
public class Deck
{
private int pointer = 0; // indicates the current position in the deck.
// This should begin with 0 (the first call)
// and increment every time a card is dealt.
private Card deck[] = new Card[CARDS_IN_DECK];
private Card tempDeck[] = new Card[CARDS_IN_DECK];
private Card Cards[] = new Card[5];
public static final int CARDS_IN_DECK = 52;
/**
* Instantiate an array of Cards and populate the array with 52 Card
* objects. The face values of the cards should be between 2 - 14.
* Values 2 - 10 represent the number cards. Values 11 - 14 represent
* Jack, Queen, King, and Ace, respectively. The suits should be as
* follows: 100 = Clubs, 200 = Diamonds, 300 = Hearts, and 400 = Spades.
* See the Card class for more information.
*
* You should both shuffle and cut the cards before this method
* concludes.
*/
public Deck()
{
int i = 0;
for(int a = 1; a <= 5; a++)
{
for(int b = 2; b <=14; b++)
{
deck[i] = new Card(a,b);
}
}
shuffle();
cut();
} // default constructor
/**
* Cut the deck. Choose a point in the deck (this can be either random
* or fixed) and re-arrange the cards. For example, if you choose to
* cut at position 26, then the 27th - 52nd cards will be placed in the
* 1st - 26th positions. The 1st - 26th cards will be placed in the
* 27th - 52nd positions.
*/
public void cut()
{
int cut = 26;
int a = 0;
int b = 0;
for(int i = 0 ; i<cut; i++)
{
tempDeck[i] = new Card(a,b);
tempDeck[i] = deck[i+26];
tempDeck[i+26] = deck[i];
}
deck = tempDeck;
}
// method cut
/**
* Deal 5 cards from the deck. Deal out the next 5 cards and return
* these in an array. You will need to maintain a pointer that lets
* you know where you are in the deck. You should make sure also
* to reshuffle and cut the deck and start over if there are not enough
* cards left to deal a hand.
*
* #return an array of 5 cards
*/
public Card[] deal(int[] args)
{
int i = 0;
int a = 0;
int b = 0;
Cards[i] = new Card(a,b);
for(i = 0; i < 1; i++)
{
Cards[pointer] = deck[pointer];
pointer++;
}
return Cards;
// this is a stub only - replace this!!!!
} // method deal
/**
* Get a card from the deck
*
* #param the position of the card you are retrieving
* #return the card object
*/
public Card getCard( int card )
{
Card oneCard = deck[pointer];
deck[pointer] = null;
pointer +=1;
return oneCard; // this is a stub only - replace this!!!
} // method getCard
/**
* Shuffle the deck. Randomly re-arrange the cards in the deck. There
* are plenty of algorithms for doing this - check out Google!!!
*/
public void shuffle()
{
int i, j, k;
int n = 15;
for ( k = 0; k < n; k++ )
{
i = (int) ( CARDS_IN_DECK * Math.random() ); // Pick 2 random cards
j = (int) ( CARDS_IN_DECK * Math.random() ); // in the deck
tempDeck[j] = deck[i];
tempDeck[i] = deck[j];
deck = tempDeck;
}
pointer = 0; // Reset current card to deal
} // end shuffle
} // class Deck
public class Player
{
private int bet;
private int chips;
private int totalChips = 0;
private Hand hand;
private String name;
public static final int START_CHIPS = 100;
public static final int WINNING_CHIPS = 200;
Scanner scan = new Scanner(System.in);
/**
* Sets the player's name and the starting number of chips.
*
* #param the player's name
*/
public Player( String n )
{
name = n;
totalChips = START_CHIPS;
} // constructor
/**
* Sets the amount of the bet and decreases the number of chips that
* the player has by the number of chips bet. Do not allow bets if
* there are not enough chips left.
*
* #param the number of chips bet
* #return true if the bet was successful (there were enough chips)
*/
public boolean bet( int bet )
{
int chipsAB;
boolean canBet;
//Get Bet
getBet();
//See if player has enough chips for bet
if(totalChips >= bet)
{
canBet = true;
}
else
{
canBet = false;
}
return canBet; // this is a stub only - replace this!!!!
} // method bet
/**
* Return the number of chips bet
*
* #return the number of chips bet
*/ //DONE
public int getBet()
{
int bet;
bet = scan.nextInt();
while (bet < 1 || bet > getChips())
{
System.out.println("Error. Re-enter bet: ");
bet = scan.nextInt();
}
return bet; // this is a stub only - replace this!!!!
} // method getBet
/**
* Return the number of chips currently held
*
* #return the number of chips held
*/
public int getChips()
{
for(int i = 0; i<1; )
{
totalChips = START_CHIPS;
}
return totalChips; // this is a stub only - replace this!!!!
} // method getChips
public int setChips()
{
int playersChips = getChips();
return playersChips;
}
/**
* Return the player's hand
*
* #return the player's hand object
*/
public Hand getHand()
{
return new Hand(); // this is a stub only - replace this!!!!
} // method getHand
/**
* Return the player's name
*
* #return the player's name
*/
public String getName(String name)
{
String n = name;
return n; // this is a stub only - replace this!!!!
} // method getName
/**
* Indicates whether this player has won
*
* #return true if the player has more than the number of winning points
*/
public boolean hasWon()
{
boolean won = false;
if(chips == 0)
{
won = true;
}
return won; // this is a stub - replace this!!!
} // method hasWon
/**
* Set the Hand object to the incoming Hand object (this comes from the
* Dealer)
*
* #param the hand dealt by the dealer
*/
public void setHand( Hand h )
{
} // method setHand
/**
* Return the player's name & the number of chips
*
* #return the players name & number of chips
*/
public String toString()
{
String nameChips;
nameChips = (name + totalChips);
return nameChips; // this is a stub only - replace this!!!
} // method toString
/**
* We won the hand, so increase chips
*
* #param the number of chips won
*/
public int winHand()
{
int chipsAB = 0;
//if(hand.beats(other))
{
chipsAB = getChips() + getBet();
}
//else
chipsAB = getChips() - getBet();
return chipsAB;
} // method winHand
} // class Player
The code compiles for me, but I had to
create a class called Card with a constructor Card(int a, int b)
create a class called Hand
ignore the Javadoc errors.
After #param you should have the name of the parameter not the ...
The main method wasn't included, but other than that it compiles with a few changes.
I think I can get you going in the right direction but I don't have time to do all the work. I went a bit further and used Java collections to clarify / ease some of the use. A bunch of code updates here. Key points are Suit enum, Collections.shuffle, use of ArrayLists for the deck and hands, and the changes in the cut function. There is also a lot of opportunity for more error and bounds checking.
Stylistically, I'd suggest thinking about your object layout. Dealer is a specialization of Player, shuffle and cut might better be methods on Dealer (a Deck doesn't do those things to itself). Anyhow, for this update I didn't touch those things, just something to be aware of.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Dealer {
public static final int NUMBER_OF_PLAYERS = 2;
private Deck deck;
private List<Player> players = new ArrayList<Player>(NUMBER_OF_PLAYERS);
Scanner scan = new Scanner(System.in);
/**
* Default constructor - creates and populates the Player array and the Deck object.
*/
public Dealer() {
deck = new Deck();
} // constructor
/**
* Outermost level of abstraction for the poker game
*/
public void play() {
System.out.println("Welcome to Poker!");
// Getting First Names
System.out.println("\nEnter first player's name: ");
String name = scan.nextLine();
players.add(new Player(name));
// Getting Second Name
System.out.println("Enter second player's name: ");
name = scan.nextLine();
players.add(new Player(name));
// First Player Goes
System.out.println(players.get(0).getName() + "'s Turn\n");
// Bet
System.out.println("Enter bet: ");
int bet = players.get(0).getBet();
// Able to bet?
while (!players.get(0).bet(bet) || !players.get(1).bet(bet)) {
bet = 20;
if (!players.get(0).bet(bet) || !players.get(0).bet(bet)) {
bet = 1;
}
}
// Draw Cards/Deck
// *****NEED TO DEAL CARDS HERE*****
// *****DEAL AN ARRAY OF CARD[5] TO EACH PLAYERS[0] AND [1]******
for (Player player : players) {
player.setHand(deck.deal(5));
}
// Compare
// Add to winner
// Add to loser
// Winner Goes ^^
} // method play
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Player {
private int chips;
private int totalChips = 0;
private List<Card> hand = new ArrayList<Card>();
private String name;
public static final int START_CHIPS = 100;
public static final int WINNING_CHIPS = 200;
Scanner scan = new Scanner(System.in);
/**
* Sets the player's name and the starting number of chips.
*
* #param the
* player's name
*/
public Player(String n) {
name = n;
totalChips = START_CHIPS;
} // constructor
/**
* Sets the amount of the bet and decreases the number of chips that the player has by the number of chips bet. Do not allow
* bets if there are not enough chips left.
*
* #param the
* number of chips bet
* #return true if the bet was successful (there were enough chips)
*/
public boolean bet(int bet) {
boolean canBet;
// Get Bet
getBet();
// See if player has enough chips for bet
if (totalChips >= bet) {
canBet = true;
} else {
canBet = false;
}
return canBet; // this is a stub only - replace this!!!!
} // method bet
/**
* Return the number of chips bet
*
* #return the number of chips bet
*/
// DONE
public int getBet() {
int bet;
bet = scan.nextInt();
while (bet < 1 || bet > getChips()) {
System.out.println("Error. Re-enter bet: ");
bet = scan.nextInt();
}
return bet; // this is a stub only - replace this!!!!
} // method getBet
/**
* Return the number of chips currently held
*
* #return the number of chips held
*/
public int getChips() {
for (int i = 0; i < 1;) {
totalChips = START_CHIPS;
}
return totalChips; // this is a stub only - replace this!!!!
} // method getChips
public int setChips() {
int playersChips = getChips();
return playersChips;
}
/**
* Return the player's hand
*
* #return the player's hand object
*/
public List<Card> getHand() {
return hand;
} // method getHand
/**
* Return the player's name
*
* #return the player's name
*/
public String getName() {
return name;
} // method getName
/**
* Indicates whether this player has won
*
* #return true if the player has more than the number of winning points
*/
public boolean hasWon() {
return (chips > WINNING_CHIPS);
} // method hasWon
/**
* Set the Hand object to the incoming Hand object (this comes from the Dealer)
*
* #param the
* hand dealt by the dealer
*/
public void setHand(List<Card> dealtHand) {
hand = dealtHand;
} // method setHand
/**
* Return the player's name & the number of chips
*
* #return the players name & number of chips
*/
public String toString() {
return "Name: " + name + ", Chips: " + chips;
} // method toString
/**
* We won the hand, so increase chips
*
* #param the
* number of chips won
*/
public int winHand() {
int chipsAB = 0;
// if(hand.beats(other))
{
chipsAB = getChips() + getBet();
}
// else
chipsAB = getChips() - getBet();
return chipsAB;
} // method winHand
} // class Player
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Deck {
public static final int CARDS_IN_DECK = 52;
private List<Card> deck = new ArrayList<Card>(CARDS_IN_DECK);
/**
* Constructor
*
* Instantiate an array of Cards and populate the array with 52 Card objects. The face values of the cards should be between 2 -
* 14. Values 2 - 10 represent the number cards. Values 11 - 14 represent Jack, Queen, King, and Ace, respectively.
*
* You should both shuffle and cut the cards before this method concludes.
*/
public Deck() {
for (Suit suit : Suit.values()) {
// I made the Ace equal to 1, but it's just preference
for (int value = 2; value <= 14; value++) {
deck.add(new Card(suit, value));
}
}
Collections.shuffle(deck);
cut();
} // default constructor
/**
* Cut the deck. Choose a point in the deck (this can be either random or fixed) and re-arrange the cards. For example, if you
* choose to cut at position 26, then the 27th - 52nd cards will be placed in the 1st - 26th positions. The 1st - 26th cards
* will be placed in the 27th - 52nd positions.
*/
public void cut() {
Random rand = new Random();
int cutPoint = rand.nextInt(deck.size()) + 1;
List<Card> newDeck = new ArrayList<Card>(deck.size());
newDeck.addAll(deck.subList(cutPoint, deck.size()));
newDeck.addAll(deck.subList(0, cutPoint));
deck = newDeck;
}
// method cut
/**
* Deal 5 cards from the deck. Deal out the next 5 cards and return these in an array. You will need to maintain a pointer that
* lets you know where you are in the deck. You should make sure also to reshuffle and cut the deck and start over if there are
* not enough cards left to deal a hand.
*
* #return an array of 5 cards
*/
public List<Card> deal(int size) {
List<Card> hand = new ArrayList<Card>(size);
hand.add(deck.remove(0));
return hand;
} // method deal
/**
* Get a card from the deck
*
* #param the
* position of the card you are retrieving
* #return the card object
*/
public Card getCard(int position) {
if (position >= 0 && position < deck.size()) {
return deck.remove(position);
}
else {
return null; // or throw an exception, or print an error or whatever
}
} // method getCard
} // class Deck
public class Card {
private Suit suit;
private int value;
public Card(Suit suit, int value) {
this.suit = suit;
this.value = value;
}
public Suit getSuit() {
return suit;
}
public int getValue() {
return value;
}
}
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES;
}
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);
}
}