Results 1 to 9 of 9
Thread: List of chars
- 01-05-2010, 12:37 AM #1
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
- 01-05-2010, 12:45 AM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
If you want a char array you should use something like:
If you want them in a string just use something likeJava Code:char[] carray = new char[5]; char[0] = 'i'; char[1] = 'q'; char[2] = 'T'; etc. OR char[] carray = {'i','q','T','r','4'};
Hope this helps. If it doesn't, I don't think I clearly understood your question.Java Code:String s = "hello"; char char1 = s.charAt(0); char char2 = s.charAt(1); etc.
I die a little on the inside...
Every time I get shot.
- 01-05-2010, 12:48 AM #3
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 questionAre you suggesting that Cocunuts migrate?!! -Monty Python
-
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.
- 01-05-2010, 01:03 AM #5
i have never used enums before are they different from classes?
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 01-05-2010, 01:10 AM #6
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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:
Where 4 is the maximum number, minus one. So 3 in this case.Java Code:int randNumb = (int)(Math.random()*4)
Good night :)I die a little on the inside...
Every time I get shot.
- 01-05-2010, 01:13 AM #7
so could i use math.random on an array of chars
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 01-05-2010, 11:48 AM #8
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Yes.
Java Code:char randomChar = charArray[(int)(Math.random()*charArray.length)];
I die a little on the inside...
Every time I get shot.
- 01-05-2010, 12:05 PM #9gcampton Guest
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
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.Java Code:public enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES }
Deck.java
Card.javaJava 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...
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: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()); } }
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
-
Searching index with special chars
By noorws in forum LuceneReplies: 1Last Post: 03-29-2010, 09:13 PM -
filter special chars
By Strubbl in forum LuceneReplies: 0Last Post: 08-17-2009, 10:23 AM -
Replacing the chars within a string.
By Mayur in forum New To JavaReplies: 2Last Post: 03-27-2009, 04:00 AM -
chars
By whosadork in forum New To JavaReplies: 6Last Post: 10-03-2008, 09:40 PM -
Can we append more than 255 chars to <a href>??
By freddieMaize in forum Advanced JavaReplies: 22Last Post: 07-18-2008, 04:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks