?
Solved

Cards

Posted on 2005-04-05
22
Medium Priority
?
252 Views
Last Modified: 2010-03-31
Experts,

Good Evening All.  I've been trying to work through a tutorial example I found but i'm getting confused about the next step that im supposed to do.

Right now I show:

GAME 1
Cards Shuffled
Your cards: X and X
You have X points

... Now the next line says, "Would you like to exchange your low card for a new card? Y or N ...

So now,  I have to figure out which of the two cards are the low card. Then print those new cards and total.

I'll post what I have into a bin to show you what I have so far.  I ended up using completed source to create the deck in deck and card.

http://www.pastebin.com/267732

Any recommendations?

Regards,
Node
0
Comment
Question by:ildave1
  • 10
  • 6
  • 6
22 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13713961
Have your Card class implement Comparable, and add a compareTo() method that handles ranking the card to another
0
 
LVL 3

Author Comment

by:ildave1
ID: 13713968
Jesus objects, your fast... lol..
0
 
LVL 92

Expert Comment

by:objects
ID: 13713977
you'll need to adjust the following according to how you want to rank cards but it should give you the idea:

public int compareTo(Object o)
{
  Card c = (Card) o;
  if (value==c.value) return suit - c.suit;
  else return value - c.value;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:aozarov
ID: 13713981
you can get the cards value -> getValue() which is a plain int and check which one is bigger.

if (card1.getValue() >= card2.getValue())
....
else
...
0
 
LVL 92

Expert Comment

by:objects
ID: 13713989
it'll simplify implementation if you changed your suit constants to be in order suits should be ranked by.
0
 
LVL 92

Expert Comment

by:objects
ID: 13714003
You;'ll also need to implement equals() to retuyrn true if suit and value are equal.
0
 
LVL 3

Author Comment

by:ildave1
ID: 13714042
Hey objects.  I see that you offer tutoring.  Could you contact me on AIM => aim marine
0
 
LVL 92

Expert Comment

by:objects
ID: 13714097
once you have implemented Comparable you can use the following to compare two cards

if (card1.compareTo(card2)<0)
{
   // card1 is less than card2
}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13714122
You can go thru all this effort or just apply the logic I provided above:
f (card1.getValue() >= card2.getValue())
...

If your code needs further sorting  then probably the above suggestion is better for you.
0
 
LVL 92

Expert Comment

by:objects
ID: 13714132
The code used in the compareTo method would be the same as what you'd use to compare directly, just saves you adding it everytime you want to compare two cards.
0
 
LVL 3

Author Comment

by:ildave1
ID: 13714155
Luckly, I'll only need to do it once.  The game loops. Or its supposed too.. lol
0
 
LVL 92

Expert Comment

by:objects
ID: 13714158
And it results in much cleaner code :)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13714167
If it is used only once then by having compartor you will not get much cleaner code.
0
 
LVL 92

Accepted Solution

by:
objects earned 1400 total points
ID: 13714173
If you want to do the comparison in your code then the compareTo from above would map to something like:

if (card1.getValue() == card2.getValue())
{
   if (card1.getSuit()<card2.getSuit())
   {
      smallest = card1;
   }
   else
   {
      smallest = card2;
   }
}
else if (card1.getValue() < card2.getValue())
{
   smallest = card1;
}
else
{
   smallest = card2;
}


(See what I mean about a cleaner code)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13714183
If it is written once then it is no big deal (for sure less lines then the other solution).
As I said if need more then once then that putting that compare code in a function or using Comparable would be cleaner.
0
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 600 total points
ID: 13714198
Also, with different coding style it does not look as bad ;)

f (card1.getValue() == card2.getValue() && card1.getSuit()<card2.getSuit())
      smallest = card1;
else if (card1.getValue() < card2.getValue())
        smallest = card1;
else
        smallest = card2;
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13714202
or even better:
f (card1.getValue() < card2.getValue() || card1.getValue() == card2.getValue() && card1.getSuit()<card2.getSuit())
      smallest = card1;
else
      smallest = card2;
0
 
LVL 92

Expert Comment

by:objects
ID: 13714205
I know which way is the more readable :)
0
 
LVL 3

Author Comment

by:ildave1
ID: 13714273
Well.... Hows it looking?

Control::
            if (d1.getValue() < d2.getValue() || d1.getValue() == d2.getValue() && d1.getSuit()<d2.getSuit())
            smallest = d1;
                  else
            smallest = d2;
                                                
            }

View::
      public boolean displayNotice(){
            reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Would you like to exchange your low card for a new card? Y or N");
                  if(reader.readLine().equals("Y")){
                        smallest = d.dealCard();      
                        System.out.println("You new cards: " + d1 + " and " + d2);
                        System.out.println("Your new point total is: " + v.displayValue);
                  }
                  else
                  System.out.println();
      }
0
 
LVL 3

Author Comment

by:ildave1
ID: 13714348
This ended up working a LOT easier...

if (card1.getValue() == card2.getValue())
{
   if (card1.getSuit()<card2.getSuit())
   {
      smallest = card1;
   }
   else
   {
      smallest = card2;
   }
}
else if (card1.getValue() < card2.getValue())
{
   smallest = card1;
}
else
{
   smallest = card2;
}
0
 
LVL 92

Expert Comment

by:objects
ID: 13714369
Yes, readability it more important than the number of lines :)
0
 
LVL 3

Author Comment

by:ildave1
ID: 13714403
I agree 10 fold
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question