Results 1 to 16 of 16
  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Card game (Solitaire)

    I'm making a card game program for a simple version of the card game Patience (game) - Wikipedia, the free encyclopedia It's quite easy to play, you pick a card from the deck at a time until the deck is empty. (There is of course more to the game than this, but this is all we need for now).

    The game is based on two classes, one class Card, and one class CardDeck. (Then there will of course also be a class for the main() program. The class "Card" may not be changed, it' supposed to be like it is, but only the class CardDeck. I need help how to implement the class CardDeck. In the class CardDeck an array should be used for the deck.

    Java Code:
    public class Card {
    	public static final int SPADES = 1;
    	public static final int HEARTS = SPADES + 1;
    	public static final int DIAMONDS = SPADES + 2;
    	public static final int CLUBS = SPADES + 3;
    	private int rank;
    	private int suit;
    	
    	/** Creates a game card with suit (SPADES, HEARTS, DIAMONDS, CLUBS) 
    	    and rank (1-13). */
    	public Card(int suit, int rank) {
    		this.suit = suit;
    		this.rank = rank;
    	}
    
    	public int getSuit() {
    		return suit;
    	}
    
    	public int getRank() {
    		return rank;
    	}
    
    	public String toString(){
    		String suitString = "";		
    		switch(suit) {
    			case Card.SPADES: suitString = "spades"; break;
    			case Card.HEARTS: suitString = "hearts"; break;
    			case Card.DIAMONDS: suitString = "diamonds"; break;
    			case Card.CLUBS: suitString = "clubs"; break;
    		}
    		String rankString = "";
    		switch(rank) {
    			case 1: rankString = "ace"; break;
    			case 11: rankString = "jack"; break;
    			case 12: rankString = "queen"; break;
    			case 13: rankString = "king"; break;
    			default: rankString = String.valueOf(rank);
    		}
    		return suitString + " " + rankString;
    	}
    }
    Java Code:
    import java.util.Random;
    
    public class CardDeck {
    
    	private Card[] cards;
    	private int current; // index for "next" card
    	private static Random rand = new Random();
    	
    	/** Creates a deck of cards. */ 
    	CardDeck() {
    		cards = new Card[52];
    		current = 0;
    	}
    	
    	/** Shuffles the deck. */
    	public void shuffle() {
    		
    	}
    	
    	/** Sees if there are more cards in the deck. */ 
    	public boolean moreCards() {
    		return true;
    	}
    
    	/** Draw the card in the top of the deck. */ 
    	public Card getCard() {
    		
    
    }
    Mostly I need help how to make the method getCard(). This method is supposed to take the "top" card from the deck. I'm thinking that when this method is performed it has to make the deck smaller all the time, but wouldn't that mean the array would get smaller aswell? Or is there any other way it can be solved? Please note that an array has to be used, not an arraylist or anything else.

    Since I have to use an array, I don't know how to solve this. Does anyone have an idea how it can be done?

  2. #2
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Card game (Solitaire)

    You don't really need to make the deck smaller when you pull a card you just should use an index that you increment when you pull a card.

  3. #3
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Card game (Solitaire)

    Quote Originally Posted by dvs_sebba View Post
    You don't really need to make the deck smaller when you pull a card you just should use an index that you increment when you pull a card.
    Ok, that sounds logical, but how?

    I tried this:
    Java Code:
    public Card getCard() {
    		if (current < 52) {
    			return cards[current];
    			current++;
    		}
    		else {
    			return null;
    		}
    	}
    But for the line "current++;" it says "Unreachable code". How could it be written?

  4. #4
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Card game (Solitaire)

    first of all that is becouse you increment the variable current after the return statment you should try smothing like:
    public Card getCard() {
    if (current < 52) {
    return cards[current++];
    }
    else {
    return null;
    }
    }
    and bythe way where didi you declared the current varible?

  5. #5
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Card game (Solitaire)

    Quote Originally Posted by dvs_sebba View Post
    first of all that is becouse you increment the variable current after the return statment
    Yeah, I thought the operation should be to first select a card, then AFTER that to increment the variable current? Or am I thinking wrong here?

    Quote Originally Posted by dvs_sebba View Post
    you should try smothing like:
    public Card getCard() {
    if (current < 52) {
    return cards[current++];
    }
    else {
    return null;
    }
    }
    and bythe way where didi you declared the current varible?
    I declared it at the beginning of the class as "private int current;", then in the constructor I wrote "this.current = 0;" to give it an initial value. This seems to work, thanks!
    Last edited by Stud1; 01-18-2014 at 07:22 PM.

  6. #6
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Card game (Solitaire)

    Yeah, I thought the operation should be to first select a card, then AFTER that to increment the variable current? Or am I thinking wrong here?
    the operation is ok, but you should know that return statment ends the method.
    so you would have ritten somthing like:
    if (current < 52) {
    Card risult = cards[current];
    curentt++;
    return risult;
    }
    else {
    return null;
    }
    }

    the post increment current++ is used as a short paht cose for the return statment you first get the card at cards[curent] and after it increments the current variable. you should take a look at usage of post or pre increment varibles in the tutorial.

    don't know if i was cleare enogh!

  7. #7
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Card game (Solitaire)

    Now I just need some help with the method shuffle(). I tried writing this:

    Java Code:
    public void shuffle() {
    		for (int i = current; i<52; i++) {
    			cards[i] = cards[rand.nextInt(51 - current)];
    		}
    	}
    But that doesn't work, it makes the same card appear in several array elements and some cards not appearing at all. The point of the method should be that there should still only be one unique card per array element but in shuffled order. Any ideas how to implement this?
    Last edited by Stud1; 01-18-2014 at 09:00 PM.

  8. #8
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Card game (Solitaire)

    Well for this you should use a suplementary array to which you save the card, even if would be more esyer to use lists.

  9. #9
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Card game (Solitaire)

    Quote Originally Posted by dvs_sebba View Post
    Well for this you should use a suplementary array to which you save the card, even if would be more esyer to use lists.
    How do you mean using a supplementary array? Could you give an example please?

  10. #10
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Card game (Solitaire)

    you could youse a new array where you put your card in and at the end of the shuffle method you pass this array to the class referance array

  11. #11
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Card game (Solitaire)

    Quote Originally Posted by dvs_sebba View Post
    you could youse a new array where you put your card in and at the end of the shuffle method you pass this array to the class referance array
    What is the "class reference array"?

  12. #12
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Card game (Solitaire)

    the card array you declered in your class

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Card game (Solitaire)

    Quote Originally Posted by Stud1 View Post
    What is the "class reference array"?
    You're not alone: that description is just as confusing to me as it is to you. Perhaps that dvs_sebba meant to say "card reference array" in stead of "class reference array".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: Card game (Solitaire)

    Just a suggestion. your constants for the suits and perhaps the ranks are begging for enums. And by doing so, you will learn about them if you don't already.

    Enum Types (The Java Tutorials > Learning the Java Language > Classes and Objects)

  15. #15
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Card game (Solitaire)

    That is possible. However there is still the problem that there won't be just one array index for each card, but there are some cards being generated more than once and some not at all. How can this be solved?

  16. #16
    ongsanb7 is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: Card game (Solitaire)

    K gạch ngồi hng. chuẩn bị nm hiii cho anh em bn luận

Similar Threads

  1. Card Game Feedback
    By Zelaine in forum New To Java
    Replies: 3
    Last Post: 09-27-2013, 04:13 AM
  2. Card Game
    By HarleyRowland in forum New To Java
    Replies: 7
    Last Post: 02-05-2013, 12:44 AM
  3. Card Game
    By abby0910 in forum New To Java
    Replies: 1
    Last Post: 07-24-2010, 12:38 AM
  4. please help me with this card game
    By noobinoo in forum New To Java
    Replies: 13
    Last Post: 03-28-2010, 02:07 PM
  5. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:53 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
  •