473,379 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

class whose instances represent a full deck of cards

10
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Deck {
  4.  
  5.     public static int numSuits = 4;
  6.     public static int numRanks = 13;
  7.     public static int numCards = numSuits * numRanks;
  8.  
  9.     private Card[][] cards;
  10.  
  11.     public Deck() {
  12.         cards = new Card[numSuits][numRanks];
  13.         for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
  14.             for (int rank = Card.ACE; rank <= Card.KING; rank++) {
  15.                 cards[suit-1][rank-1] = new Card(rank, suit);
  16.             }
  17.         }
  18.     }
  19.  
  20.     public Card getCard(int suit, int rank) {
  21.         return cards[suit-1][rank-1];
  22.     }
  23. }

Q:- Why this expression (cards[suit-1][rank-1] = new Card(rank, suit); ) is added?
Sep 18 '08 #1
1 2352
JosAH
11,448 Expert 8TB
Most likely Card.DIAMONDS and Card.ACE (the lowest values) are defined as 1.
Index values start at 0. Quite clumsy if you'd ask me.

kind regards,

Jos
Sep 18 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: JC | last post by:
I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me. Randomize For...
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
7
by: J. Campbell | last post by:
I'm trying to make a class that represents a deck of cards and a dealer for use in a card game. I'm fairly novice. The code compiles (Dev c++(gcc 3.3.1)) without complaint, however it acts...
1
by: sahm | last post by:
hi, i created a function ostream& operator<<(ostream& out, vector<Card>& deck) { int i = 0; for (int s = 1; s <= 4; s++) { for (int r = 1; r <= 13; r++, i++) { deck.setSuit(s);
5
by: kylemort | last post by:
I am currently using a class (Card) to represent a card in a deck of cards. The class has a constructor: Card::Card(int num, int su) { suit = su; number = num; } and I am trying to create...
15
by: sandy123456 | last post by:
At the moment im trying to write a hand class for a game poker patientnce But when i get to the part having to catergorise the difference of full house straight flush flush four of a kind and...
4
by: tvance929 | last post by:
Hey everyone, I created a theDeck class that creates a 52 card int List. Inside of this class I have a ShuffleCards method. I simply want 2 seperate decks that I can then shuffle and...
3
by: dave | last post by:
Hello, I'm currently on the class section of my self-taught journey and have a question about classes: is it possible to bring a object created inside the class definitions outside the class so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.