I am hoping to get some help even though I know this is probably a very simple bug that I cannot seem to find an answer to.
What I am trying to accomplish is to create a deck of cards, and I keep running into an out of bounds error.
Here is my code:
Card Class:
public class Card {
private String suit;
private int face;
public Card(int face, String suit){
face = this.face;
suit = this.suit;
}
public int getFace(){
return face;
}
public String getFaceAsString(int face){
int faceName = face;
String faceString = "";
if(faceName == 11){
faceString = "J";
} else if(faceName == 12){
faceString = "Q";
} else if(faceName == 13){
faceString = "K";
} else if(faceName == 14){
faceString = "A";
} else {
faceString = Integer.toString(faceName);
}
return faceString;
}
public String getSuit(){
return suit;
}
public void setSuit(String suit){
this.suit = suit;
}
}
This is my main class:
import java.util.Random;
import java.util.Scanner;
public class Hero_Game {
public static void main(String[] args) {
String[] suits = {"Spades","Clubs","Hearts","Diamonds"};
int[] faces = {2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int index = 0;
Card[] deck = new Card[52];
for(int i = 0; i<suits.length;i++){
for(int j = 0; j<faces.length;j++){
deck[index] = new Card(faces[i],suits[j]);
index++;
}
}
You switched the indexes for the faces and suits arrays in your for loops. It should be:
for(int i = 0; i<suits.length;i++){
for(int j = 0; j<faces.length;j++){
deck[index] = new Card(faces[j],suits[i]);
index++;
}
}
You also have an extra number in your "faces" array - you're trying to create 14 * 4 = 56 cards instead of 13 * 4 = 52.
Why do you have 14 faces instead of 13?
In any case, to more safely allocate the correct number of items in your deck array, use int numCardsInDeck = suits.length * faces.length; to calculate the total items in the deck.
Try this instead:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static class Card {
final int face;
final String suit;
public Card(int face, String suit) {
this.face = face;
this.suit = suit;
}
#Override
public String toString() {
return face + " of " + suit;
}
}
public static void main(String[] args) {
String[] suits = {"Spades","Clubs","Hearts","Diamonds"};
int[] faces = {2,3,4,5,6,7,8,9,10,11,12,13,14};
int index = 0;
int numCardsInDeck = suits.length * faces.length;
Card[] deck = new Card[numCardsInDeck];
for(int i = 0; i < suits.length; i++) {
for(int j = 0; j < faces.length; j++) {
deck[index] = new Card(faces[j],suits[i]);
index++;
}
}
System.out.println(Arrays.toString(deck));
}
}
int[] faces = {2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Get rid of 15 and do the loop as suggested by #Geshode
Related
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());
My other question got flagged duplicate couse I didn't explain well enough so i try again only a lot better.
So I'm making this java Black Jack console game and have stumbled to a small wall here. I've made a class called Card which is ready. Now i'm stumbeling a bit in creating all 52 cards in to an array of cards. Well i made code that works but it seems ugly to me having three nested for loops. Is there a "real way" of doing what I did here.
So here would be part of the class Card
int value;
String suit;
String name;
public void Card(int value, int suitNumber)
this.value=value;
switch(suitNumber){
case 1 : this.suit = "Heart";
break;
.......
}
switch(value){
case 1 : this.name="Ace";
......
}
Part of Pack class have a nested for loop like this:
Card[] packOfCards = new Card[51];
for(int k=0; k<52; ){
for(int i=1; i<5; i++){
for(int j=1; j<14; j++){
packOfCards[k] = Card(j, i);
k++;
// it seems stupid for me to have 3 nested
//loops like that
}
}
}
You can get same result using one for loop by saving more time complexity instead of three for loop and repeatation. Try,
for (int k = 1; k <= 52; k++) {
int i = k % 5;
int j = k % 14;
packOfCards[k-1] = new Card(j, i);
}
You either need to loop over:
Every slot in your deck
Every possible card value (Which probably requires a nested loop; get each value for each suit, or vice-versa)
At present you seem to be looping over both.
The following code uses the first approach:
for(int i = 0; i < 52; i++) {
int suit = (i % 13) + 1;
int value = (i / 13) + 1;
packOfCards[i] = new Card(value, suit);
}
And this code uses the second (two-loop) approach:
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 13; j++) {
int cardIndex = (13 * (i-1)) + j;
packOfCards[cardIndex] = new Card(j, i);
}
}
Also note that your Card class looks wrong. You've declared a void method when I think you wanted a constructor, and then you missed the new keyword when creating the new card (hint: the compiler will alert you to a lot of these mistakes).
As others have suggested, it would probably make more sense to use enums too as the type for your card value and suit, rather than juggling between int and String.
import java.util.ArrayList;
import java.util.List;
public class Card {
private final CardValue value;
private final Suit suit;
public Card(CardValue value, Suit suit) {
this.value = value;
this.suit = suit;
}
#Override
public String toString() {
return value + " of " + suit;
}
public static void main(String[] args) {
List<Card> deck = new ArrayList<Card>();
for (Suit s : Suit.values()) {
for (CardValue v : CardValue.values()) {
deck.add(new Card(v, s));
}
}
for (Card c : deck) {
System.out.println(c);
}
Card aceOfSpades = new Card(CardValue.withValue(1), Suit.SPADES);
System.out.println(aceOfSpades);
}
public enum Suit {
HEARTS ("Hearts"),
CLUBS ("Clubs"),
DIAMONDS ("Diamonds"),
SPADES ("Spades");
private final String name;
Suit(String name) {
this.name = name;
}
#Override
public String toString() {
return this.name;
}
}
public enum CardValue {
TWO(2, "Two"),
THREE(3, "Three"),
FOUR(4, "Four"),
FIVE(5, "Five"),
SIX(6, "Six"),
SEVEN(7, "Seven"),
EIGHT(8, "Eight"),
NINE(9, "Nine"),
TEN(10, "Ten"),
JACK(11, "Jack"),
QUEEN(12, "Queen"),
KING(13, "King"),
ACE(1, "Ace");
private final int value;
private final String name;
CardValue(int value, String name) {
this.value = value;
this.name = name;
}
public int getValue() {
return value;
}
public static CardValue withValue(int value) {
for (CardValue v : CardValue.values()) {
if (value == v.value) {
return v;
}
}
return null;
}
#Override
public String toString() {
return this.name;
}
}
}
You'll probably want to pull the enums out into seperate files, but for the sake of producing a relatively concisely working example I put them within the main class.
In my game's code, I am trying to add a card to hand. As soon as I do, my array is out of bounds. Everything looks right, but maybe I'm missing something.
FYI, one and two are Player instances. Relevant code from Main class (sorry about the formatting. i suck at transferring it to Stack Overflow):
import java.util.*;
public class Program {
public static void main(String args[]) {
String[] rank = {"two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "jack", "queen", "king", "ace"};
String[] suit = {"hearts", "diamonds", "spades", "clubs"};
Scanner scan = new Scanner(System.in);
String something = "yes", something2 = "yes"; //Use with while loop
String winner = "yes"; //Use for while loop
String temp; //Use with setting names
Card[] deck = new Card[52]; //Deck array
int playercount = 0;
Player one = new Player("temp");
Player two = new Player("temp");
Player three = new Player("temp");
Player four = new Player("temp");
while (something2.equals("yes") || playercount < 2) { //Add players to game
System.out.println("Would a(nother) player like to join?");
something2 = scan.nextLine();
System.out.println();
if (something2.equals("yes")) {
if (playercount <= 4) {
if (playercount == 0) {
System.out.println("What is your name: ");
Player one1 = new Player(scan.nextLine());
one = one1;
playercount++;
System.out.println();
}
else if (playercount == 1) {
System.out.println("What is your name: ");
Player two2 = new Player(scan.nextLine());
two = two2;
playercount++;
System.out.println();
}
else if (playercount == 2) {
System.out.println("What is your name: ");
Player three3 = new Player(scan.nextLine());
three = three3;
playercount++;
System.out.println();
}
else if (playercount == 3) {
System.out.println("What is your name: ");
Player four4 = new Player(scan.nextLine());
four = four4;
playercount++;
System.out.println();
}
else {System.out.println("Only four players are allowed.");
something2 = "no";}
}
}
else if (playercount < 2) {
System.out.println("You need at least two players...");
System.out.println();
}
else something2 = "no";
}
//Start game
while (something.equals("yes")) {
//Prepare game
Card.makeDeck(deck, rank, suit);
deck = Card.getDeck();
Card.shuffle(deck);
deck = Card.getDeck();
//Deal cards
if (playercount == 2) {
for (int i = 1; i < 8; i++) {
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else if (playercount == 3) {
for (int i = 1; i < 8; i++) {
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else {
for (int i = 1; i < 8; i++) {
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
four.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
}
}
}
Card class:
import java.util.*;
public class Card {
private String suit;
private String rank;
private static int temp = 0, temp2 = 0; //Use for reseting rank and suit
private static Card temp3; //Use for draw method
private static int temp4; //Use for shuffle method
private static Card[] deck = new Card[52];
//Constructors
public Card() {
this.rank = "two";
this.suit = "hearts";
}
public Card(String r, String s) {
this.rank = r;
this.suit = s;
}
//Mutators
//Make deck
public static void makeDeck(Card[] c, String[] r, String[] s) {
for (int i = 0; i < c.length; i++) {
c[i] = new Card(r[temp], s[temp2]);
temp++; temp2++;
//Reset rank and suit
if (temp > 12)
temp = 0;
if (temp2 > 3)
temp2 = 0;
}
deck = c;
}
//Accessors
//Return deck
public static Card[] getDeck() {
return deck;
}
//Shuffle
public static Card[] shuffle(Card[] c) {
for (int i = 0; i < c.length; i++) {
int rand = (int)(Math.random()*(i + 1));
//Don't let anything be in a slot that doesn't exist
while (rand > c.length) {
temp4 = (int)Math.random();
rand -= temp4;
}
if (rand < 0)
rand += temp4;
Card temp = c[i];
c[i] = c[rand];
c[rand] = temp;
}
deck = c;
return deck;
}
//Draw
public static Card draw(Card[] c) {
if (c != null) {
for (int i = 0; i < c.length; i++) {
if (c[i] != null) {
try {
return c[i];
} finally {
c[i] = null;} //Remove i from c
}
}
}
return null;
}
}
Player class:
import java.util.*;
public class Player {
private String name;
private Card[] hand = new Card[52];
private int handsize = 0;
//Constructor
public Player(String n) {
name = n;
}
//Mutators
public void addCard(Card c) {
hand[handsize] = c;
handsize++;
}
//Accessors
public String getName() {
return name;
}
public Card[] getHand() {
return hand;
}
}
Your draw method is broken.
// get the first non-null Card from the cards "c".
public static Card draw(Card[] c) {
if (c != null) {
for (int i = 0; i < c.length; i++) {
if (c[i] != null) {
try {
return c[i];
} finally {
// now remove element i from the `c` array.
c[i] = null;
}
}
}
}
return null;
}
The problem is with your loop
while (something.equals("yes"))
There's nothing that sets something to any other value, so this loop just goes around endlessly, until all the players have more than 52 cards. Once someone has more than 52 cards, adding a new card causes the exception.
I think you need to remove this while. The code inside it should only be run once.
I think the order of your code is incorrect (hard to tell with this code)
for (int i = 1; i < 8; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
}
maybe should be
for (int i = 1; i < 8; i++)
{
deck = Card.getDeck();
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
}
Update
Also
public void addCard(Card c) {
hand[handsize] = c;
handsize++;
}
handsize is never incremented - it is always 0
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am having problems getting this DeckOfCards class to compile. It keeps telling me that it cannot find the symbol when pointing to Deck2.Shuffle() and Deck2.deal(). Any ideas why?
import java.lang.Math;
import java.util.Random;
public class DeckOfCards {
private Cards[] Deck;
private int cardHold;
public DeckOfCards() {
Deck = new Cards[52];
int n = 0;
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 4; j++) {
Deck[n] = new Cards(i, j);
n = n + 1;
}
}
cardHold = -1;
}
public void Shuffle() {
// shuffles ands resets deck
int i = 0;
while (i < 52) {
int rando = (int) (5.0 * (Math.random()));
Cards temp = Deck[rando];
Deck[rando] = Deck[i];
Deck[i] = temp;
i++;
}
}
public Cards deal() {
// if there are any more cards left in the deck, return the next one and
// increment
// index; return null if all the cards have been dealt
// ***Question, increment before or
// after??***----------------------------------------
if (!hasMoreCards()) {
return null;
} else {
Cards temp = null;
temp = Deck[cardHold];
cardHold = cardHold + 1;
return temp;
}
}
public boolean hasMoreCards() {
// returns true if there are more cards left, else return false
if (cardHold == 0)
return false;
else
return true;
}
public static void main(String[] args) {
DeckOfCards Deck2 = new DeckOfCards();
Deck2.Shuffle();
for (int i = 0; i < 52; i++)
System.out.println(Deck2.deal());
}
}
Below class is Card class, maybe that is causing the issue?
public class Cards {
protected int rank;
protected int suit;
protected String[] sNames = { "Hearts", "Clubs", "Spades", "Diamonds" };
protected String[] rNames = { "Ace", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King" };
public Cards(int Rank, int Suit) {
suit = Suit;
rank = Rank;
}
public String toString() {
return ("Your card is: " + rNames[rank - 1] + " of " + sNames[suit - 1]);
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
}
Your compiler issues are behind you, but there are lots of issues with your code.
public class DeckOfCards {
private Cards[] deck;
private int cardHold;
public DeckOfCards() {
deck = new Cards[52];
int n = 0;
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 4; j++) {
deck[n] = new Cards(i, j);
n = n+1;
}
}
cardHold = -1;
}
public void shuffle() {
int i = 0;
while (i < 52) {
int rando = (int) (5.0*(Math.random()));
Cards temp = deck[rando];
deck[rando] = deck[i];
deck[i] = temp;
i++;
}
}
public Cards deal() {
return (hasMoreCards() ? deck[++cardHold] : null);
}
public boolean hasMoreCards() {
return (cardHold != 0);
}
public static void main(String[] args) {
DeckOfCards deck2 = new DeckOfCards();
deck2.shuffle();
for (int i = 0; i < 52; i++)
System.out.println(deck2.deal());
}
}
And:
public class Cards {
protected int rank;
protected int suit;
protected String[] sNames = {"Hearts", "Clubs", "Spades", "Diamonds"};
protected String[] rNames = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
public Cards(int rank, int suit) {
this.suit = suit;
this.rank = rank;
}
public String toString() {
return ("Your card is: "+rNames[rank-1]+" of "+sNames[suit-1]);
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
}
You are using an assignment operator in the if statement expression here:
if (cardHold = 0)
Replace with
if (cardHold == 0)
you may want to add import java.util.Date; and the shuffle function and call in the main is different one have 's'and the other 'S'.
Try this:
import java.lang.Math;
import java.util.Date;
import java.util.Random;
public class DeckOfCards {
private Cards[] Deck;
private int cardHold;
private Random rand;
public DeckOfCards() {
rand = new Random();
Date d = new Date();
rand.setSeed(d.getTime());
Deck = new Cards[52];
int n = 0;
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 4; j++) {
Deck[n] = new Cards(i, j);
n = n + 1;
}
}
cardHold = 0;
}
public void shuffle() {
// shuffles ands resets deck
int i = 0;
while (i < 52) {
// int rando = (int)(5.0 * (Math.random()));
int rando = rand.nextInt(52);
Cards temp = Deck[rando];
Deck[rando] = Deck[i];
Deck[i] = temp;
i++;
}
}
public boolean hasMoreCards() {
// returns true if there are more cards left, else return false
if (cardHold == 0)
return false;
else
return true;
}
public static void main(String[] args) {
DeckOfCards Deck2 = new DeckOfCards();
Deck2.shuffle();
for (int i = 0; i < 52; i++)
System.out.println(Deck2.Deck[i]);
}
}
I am trying to draw a Hand of Cards (in the Hand class) but have it shuffled. My problem is if I do shuffleDeck() in the initialDeal() method (where I need to draw a Hand of Cards but shuffled) it gives me an ArrayIndexOutOfBounds exception.
And I draw two narfs OF Clubs... Note, the narf is basically 0 and just a placeholder. It will not be used.
class Card {
int suit, rank;
public Card () {
this.suit = 0; this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit; this.rank = rank;
}
public int getSuit() {
return suit;
}
public int getRank() {
return rank;
}
public void printCard () {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
System.out.println (ranks[rank] + " of " + suits[suit]);
} //end printCard
} //end class
Card class(this is basically doing basic stuff) ^
import java.util.Random;
class Hand {
static Card[] deck = new Card[52];
Card[] hand = new Card[10];
static int index;
public static void createDeck () {
int m = 0;
for (int j = 0; j < 4; j++) {
for (int k = 1; k < 14; k++) {
deck[m] = new Card(j,k);
m++;
}
}
index = 0;
} // end createDeck
public static Card deal () {
Hand.index++;
return deck[index-1];
} // end deal
public static int compareCard (Card c1, Card c2) {
if (c1.getSuit() > c2.getSuit()) return 1;
if (c1.getSuit() < c2.getSuit()) return -1;
if (c1.getRank() > c2.getRank()) return 1;
if (c1.getRank() < c2.getRank()) return -1;
return 0;
} // end compareCard
public void printDeck (int size) {
int j = 0;
while (j < size) {
deck[j].printCard();
j++;
}
}// end printDeck
public static void shuffleDeck () {
Card tempCard;
Random rd = new Random();
for (index = 0; index < deck.length; index++) {
int r = rd.nextInt(deck.length);
tempCard = deck[index];
deck[index] = deck[r];
deck[r] = tempCard;
}
} // end shuffleDeck
public void initialDeal() {
hand[0] = null;
hand[1] = null;
hand[0] = deal();
hand[1] = deal();
}
public void printHand() {
initialDeal();
index = 0;
for(Card outputCard = new Card(); hand[index] != null; index++) {
outputCard.printCard();
}
}
}
Hand class^ (this is where I need to draw two cards from a shuffled deck)
class Dealer {
Hand dealer = new Hand();
public Dealer () {
dealer.createDeck();
dealer.shuffleDeck();
System.out.println("Dealer's Hand");
System.out.println("");
initDeal();
}
public void initDeal() {
dealer.initialDeal();
dealer.printHand();
}
} //end class
Dealer class.^ Calling the methods of Hand
class Driver {
public static void main (String[] args) {
Dealer dealer = new Dealer();
} //end main method
} //end class
^ Running everything basically
You're modifying the index in shuffleDeck, so after the shuffleDeck finishes, the index is shuffleDeck.length.
In shuffleDeck, do for (int index = 0; index < deck.length; index++)
(note the int index -> declare local variable, and not use the static one.)
Or you can (in initialDeal) set the index to 0.