Results 1 to 9 of 9

Thread: List of chars

  1. #1
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default List of chars

    So im making a card game application for computer science and my question is how do you get it to print out all the chars take the four suits of the cards chars put them in an array and randomly pick one from that array and print it out. i have made a pretty desperate attempt at it and this is what i got.

    BTW this is just trying to print all of the chars so i can pick the four.

    Java Code:
    package hello;
    
    public class hello2 {
    	public static void main(String[] args)
    	{
    	  char card=' ';
    	  int lcv;
    	  for(lcv=0;lcv<=card;lcv++)
    	  {
    		  System.out.print(card+" ");
    	  }
    	  
    	}
    
    }
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    If you want a char array you should use something like:
    Java Code:
    char[] carray = new char[5];
    char[0] = 'i';
    char[1] = 'q';
    char[2] = 'T';
    etc.
    
    OR
    
    char[] carray = {'i','q','T','r','4'};
    If you want them in a string just use something like
    Java Code:
    String s = "hello";
    char char1 = s.charAt(0);
    char char2 = s.charAt(1);
    etc.
    Hope this helps. If it doesn't, I don't think I clearly understood your question.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    that helps a little bit but there are four chars that are the four card suits. im trying to put those in an array and then randomly pick one of them from that array. but i dont how to get those chars so i thought i would use a for loop to print out all of the chars then i could get those four and store them

    hope that clears up the question
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Usually with Cards, we use enums for the suits and the values, and then create a Card class that holds a Suit and a value field. Since with most playing card apps, you usually randomly remove a card from a Deck, meaning you can't pick the same card twice (unless your deck holds duplicates such as in casino blackjack) the best collection for this is to place your Card objects into an ArrayList<Card>. Then to randomize this deck, you shuffle it with Collections.shuffle(...). Then a call to remove on the arraylist will both return a Card object and remove it from the list. Since this is such a common assignment, there are lots of code examples of this to be found in this and in the other major Java fora.

  5. #5
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    i have never used enums before are they different from classes?
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Enums
    Including a card example. :)
    It's better if you use them, for clearity and such, but not necessary.

    To pick something random, use Math.random(), which returns a random double number between zero and one (excluding 1). Than use:
    Java Code:
    int randNumb = (int)(Math.random()*4)
    Where 4 is the maximum number, minus one. So 3 in this case.

    Good night :)
    I die a little on the inside...
    Every time I get shot.

  7. #7
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    so could i use math.random on an array of chars
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  8. #8
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Yes.
    Java Code:
    char randomChar = charArray[(int)(Math.random()*charArray.length)];
    I die a little on the inside...
    Every time I get shot.

  9. #9
    gcampton Guest

    Default

    I just stumbled across this post and funnily enough this is what I was doing today, as I have only ever made the basic String Cards. While I don't particularly want to solve the solution for you I will post some sample code so you can get an idea of how I solved this problem. (generic and simple is the key)

    An enum is usually in a class of it's own, this makes it easier to use as a type and compilers prefer it this way.
    Suit.java
    Java Code:
    public enum Suit 
    {
    	HEARTS, DIAMONDS, CLUBS, SPADES
    }
    Being that Hearts, clubs etc are not strings they are of type Suit, you need to use the enum method .valueOf() when passing in a string representation of the enumerated type.
    Deck.java
    Java Code:
    public class Deck
    {
    	private ArrayList<Cards> aDeck;
    
            public Deck(ArrayList<Cards> deck)
    	{
    		setDeck(deck);
    		populateDeck();
    	}
    	/**
    	 * Set by constructor
    	 * @param aDeck sets the current ArrayList
    	 */
    	private void setDeck (ArrayList<Cards> aDeck)
    	{
    		this.aDeck = aDeck;
    	}
    	/**
    	 * @return the ArrayList
    	 */
    	public ArrayList<Cards> getDeck()
    	{
    		return aDeck;
    	}
    	/**
    	 * Populates the deck of cards Ace-King of each suit.
    	 */
    	public void populateDeck()
    	{
    		getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("ACE")));
    		getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("KING")));
    		getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("QUEEN")));
    		getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("JACK")));
    		getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("TEN")));
    ...
    ... // and so on...
    Card.java
    Java Code:
    public class Cards 
    {
    	private Suit suit;   // create a private enum of Suit for setters/getters
    	private Value value;
    
            public Cards(Suit s, Value v)
    	{
    		setSuit(s);
    		setValue(v);
    	}
    	/**
    	 * @param suit sets the current card suit
    	 */
    	public void setSuit(Suit suit) 
            {
                    this.suit = suit;
             }
    	// typical setters and getters
    	public Suit getSuit() 
    	public void setValue(Value value) 
    	public Value getValue() 
    	
    	@Override
    	public String toString()
    	{
    		return String.format("%s of %s", getValue(), getSuit());
    	}
    }
    Enum types take a bit of getting used to, but if you consider them a class declaration of a type much like Integer String Double etc. then it's not too hard, consider this piece of code:

    Java Code:
    enum Apple { 
      A(10), B(9), C, D(15), E(8); 
     
      private int price; // price of each apple 
     
      // Constructor 
      Apple(int p) { price = p; } 
     
      // Overloaded constructor 
      Apple() { price = -1; } 
     
      int getPrice() { return price; } 
    }
    Last edited by gcampton; 01-05-2010 at 12:41 PM.

Similar Threads

  1. Searching index with special chars
    By noorws in forum Lucene
    Replies: 1
    Last Post: 03-29-2010, 09:13 PM
  2. filter special chars
    By Strubbl in forum Lucene
    Replies: 0
    Last Post: 08-17-2009, 10:23 AM
  3. Replacing the chars within a string.
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 04:00 AM
  4. chars
    By whosadork in forum New To Java
    Replies: 6
    Last Post: 10-03-2008, 09:40 PM
  5. Can we append more than 255 chars to <a href>??
    By freddieMaize in forum Advanced Java
    Replies: 22
    Last Post: 07-18-2008, 04:04 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
  •