Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 53
  1. #21
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up Ta da!!

    Quote Originally Posted by gcalvin View Post
    You could reduce that by two more lines, but I'll leave that as an exercise.
    -
    Java Code:
    public void shuffleDeck() {
            for (int k = cards.size(); k > 0; k--) {
                    cards.add(cards.remove(rgen.nextInt(k)));
            }
    }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  2. #22
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Well done! Now you'll see that dealing is much easier than shuffling. Then do that Total Beginner tutorial series.

    -Gary-

  3. #23
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Well done! Now you'll see that dealing is much easier than shuffling. Then do that Total Beginner tutorial series.

    -Gary-
    i thought it would be, BUT!!!
    i kinda sort of forgot about my playing card class,
    i used it to great objects to store in the deck,
    i can find the top card in the deck,
    i can create a temporay object from it,
    i can remove it from the deck,

    but i have to think about how to return that object from the array to the CardGame() class,
    here are my basic,, still muddled,,, ideas before i go to bed, but ive missed something, which i hope to get mt head around tomorrow. need sleep!
    Java Code:
    public PlayingCard dealTopCard() {
    		PlayingCard topCard = new PlayingCard(cards.get(0));
    		println(cards.get(0));
    		cards.remove(cardsget(0));
    		return topCard; 
            
            }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  4. #24
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    No, you don't need a new PlayingCard at all. The 52 cards already exist, so you're just returning the one that's currently "on top" of the deck -- just as it would be if you were playing with real cards. The only trick is that once a card is dealt, it's not on top anymore, but you don't really want to remove it either. (This is true in Real Life as well, but it may not seem so obvious. When you are playing a card game, you are playing with one deck of cards, and all the cards in play are still part of that deck, right?) So... how are you going to keep track of which card is on top? How will you know if there are no cards left to deal, and what should you do in that situation?

    -Gary-

  5. #25
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Can I just say, it's always nice to have someone who reads the API docs, and also takes suggestions on board?

    It's alarmingly rare here, I feel...which makes Tolls sad..:(

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcalvin View Post

    Java Code:
    for (Card.Suit suit : Card.Suit.values()) {
    	for (Card.Rank rank : Card.Rank.values()) {
    I noticed something I don't quite like: the values() method generates a new array every time it is called; in your example an array of 13 elements is creates four times plus an array of 4 values is created once; as a premature optimization one could do:

    Java Code:
    Card.Suit[] suits= Card.Suit.values();
    Card.Rank[] ranks= Card.Rank.values();
    for (Card.Suit suit : suits) {
    	for (Card.Rank rank : ranks) {
    Now only two arrays are generated but then again I find that solution quite mechanical. I wonder why that method was implemented this way.

    kind regards,

    Jos

  7. #27
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Well, if the Enum stored an array of it's values and returned that then:

    Java Code:
    Card.Suit[] suits = card.Suit.values();
    for (int i = 0; i < suits.length; i++) {
        suits[i] = Card.Suit.HEARTS;
    }
    might cause a few problems...

    ETA: Oops, got the index wrong...fixed now.

  8. #28
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Well, if the Enum stored an array of it's values and returned that then:

    Java Code:
    Card.Suit[] suits = card.Suit.values();
    for (int i = 0; i < suits.length; i++) {
        suits[i] = Card.Suit.HEARTS;
    }
    might cause a few problems...
    That's true; on the other hand: code sabotaging itself doesn't deserve better ;-)

    kind regards,

    Jos

  9. #29
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Well, it would be best for it to return an iterator, but still.
    You should never expect something that returns an array to return anything other than a copy of it's array. Because returning a copy is good coding practice.

  10. #30
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by Tolls View Post
    Can I just say, it's always nice to have someone who reads the API docs, and also takes suggestions on board?

    It's alarmingly rare here, I feel...which makes Tolls sad..:(
    heh heh, i'll take all the suggestions I can get. ;)
    How else am I suppose to learn this stuff?!,,

    SAMMS Teach yourself to be an idiot in 21 days:rolleyes:

    Quote Originally Posted by JosAH View Post
    I noticed something I don't quite like:
    :eek: I have this familiar feeling that in about half an hours time or so by brain is going to be reduced to jelly and oozing out of my ears!! :D
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  11. #31
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    No, you don't need a new PlayingCard at all. The 52 cards already exist, so you're just returning the one that's currently "on top" of the deck -- just as it would be if you were playing with real cards. The only trick is that once a card is dealt, it's not on top anymore, but you don't really want to remove it either. (This is true in Real Life as well, but it may not seem so obvious. When you are playing a card game, you are playing with one deck of cards, and all the cards in play are still part of that deck, right?) So... how are you going to keep track of which card is on top? How will you know if there are no cards left to deal, and what should you do in that situation?

    -Gary-
    I can count the cards i have dealt which tells me when all the cards are gone.
    but if i dont remove the card its still on the top....

    but does that matter? perhaps it does Vegas... but not in Java!
    i think I have a new Plan!:D
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  12. #32
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Well, it would be best for it to return an iterator, but still.
    You should never expect something that returns an array to return anything other than a copy of it's array. Because returning a copy is good coding practice.
    It could use the Collections.unmodifiableList( ... ) method and simply return a (unique) list of enum values.

    kind regards,

    Jos

  13. #33
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Well, you could, but you'd still need to create whatever Collection, since it's held as an array...at least, it's not a Collection.

  14. #34
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Well, you could, but you'd still need to create whatever Collection, since it's held as an array...at least, it's not a Collection.
    That's what I'm saying: returning a fresh array all the time is a waste; returning one and the same array over and over again has its weak issues; returning an unmodifiableList is an answer.

    kind regards,

    Jos

  15. #35
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Depends how it's held. I get the impression it's something native (just looking at the EnumSet stuff anyway...can't find the values() code). Not sure what actually goes on under there.

  16. #36
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Depends how it's held. I get the impression it's something native (just looking at the EnumSet stuff anyway...can't find the values() code). Not sure what actually goes on under there.
    The static values() method isn't a native method itself; I applied a bit of reflection trickery:

    Java Code:
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    
    
    public class AAA {
    
    	enum T { A, B, C };
    	
    	public static void main(String[] args) {
    		Method[] methods=T.class.getMethods();
    		
    		for (Method m : methods) {
    			System.out.println(Modifier.isNative(m.getModifiers())+" : "+m);
    		}
    	}
    }
    kind regards,

    Jos

  17. #37
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Actually, come to think of it, I'm not sure the EnumSet one is either...:)

    Wonder where the code is for it?
    I'd be curious to know what it is they're doing...

  18. #38
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Actually, come to think of it, I'm not sure the EnumSet one is either...:)

    Wonder where the code is for it?
    I'd be curious to know what it is they're doing...
    Sun often plays tricks on us; the EnumSet uses a class named:

    Java Code:
    import sun.misc.SharedSecrets;
    They don't distribute the source code for that class and they're having lots of fun in it ;-) We'll never know what they're doing in there ...

    kind regards,

    Jos

  19. #39
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    I knew this would happen!!!

    brain dribbling out all over the key board!!

    okay I havent had much luck trying to use
    Java Code:
    public List dealOne() {
            return Collections.unmodifiableList(cards);
    but at any rate this is going to return a list and not a card. but if i could get a list then finding a card in that list is not such a big leap.

    so refering to the above posts i'm thinking that maybe i have to first make a copy of the array list. this seems a daft idea at first, it would be more simple to say show me card 0, show me card 1, show card 2 etc..
    but i can also see some sense.
    summarising the deck class
    decalre random gen (if i dont use collections.shuffle)
    declare empty aRRAYlist
    declare cardindex

    construct the deck of PlayingCards.
    initialise the cardIndex to zero

    void shuffle() the deck. the shuffled deck now needs to stay static. at least until the next round of the CardGame()

    List deal(){
    new arraylist copy = Collections.unmodifiableList(cards)
    in the copy swap the CardIndex with card 0
    remove the cards from 1 to 51
    return Collections.unmodifiableList(cardsCopy)
    cardIndex++
    }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  20. #40
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sonny View Post
    I knew this would happen!!!

    brain dribbling out all over the key board!!
    If I remember well they sell those plastic protective anti-brain-drivel keyboard covers ... ;-)

    Quote Originally Posted by sonny View Post
    List deal(){
    new arraylist copy = Collections.unmodifiableList(cards)
    in the copy swap the CardIndex with card 0
    remove the cards from 1 to 51
    return Collections.unmodifiableList(cardsCopy)
    cardIndex++
    }
    What do you think that 'unmodifiable' means?

    kind regards,

    Jos

    ps. what is it exactly you want to accomplish?

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 03:01 AM
  2. stopping second time round
    By silverspoon34 in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 03:04 PM
  3. How to Draw Round Rectangles in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:25 PM
  4. How to round a double?
    By Valeriano in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 04:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •