Hey, i am a bit of a noob. I'm trying to get my head around Objects and how they work.
I was going over a Card game tutorial and i'm a bit confused on what is going on in some of these examples.
package javacards; public class Card { private int rank, suit; //Declaring the Array of Cards. private static String[] suits = { "hearts", "spades", "diamonds", "clubs" }; private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; Card(int suit, int rank) { this.rank = rank; this.suit = suit; } public @Override String toString() { return ranks[rank] + " of " + suits[suit]; } public int getRank() { switch(rank) { case 0: return 0; case 1: return 1; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; case 6: return 6; case 7: return 7; case 8: return 8; case 9: return 9; case 10: return 10; case 11: return 10; case 12: return 10; default: return 0; } } public int getSuit() { return suit; } }package javacards; import java.util.Random; public class Deck { private Card[] cards; int i; Deck() { i=51; cards = new Card[52]; int x=0; for (int a=0; a<=3; a++) { for (int b=0; b<=12; b++) { cards[x] = new Card(a,b); x++; } } } public Card drawFromDeck() { Random generator = new Random(); int index=0; do { index = generator.nextInt( 52 ); } while (cards[index] == null); i--; Card temp = cards[index]; cards[index]= null; return temp; } public int getTotalCards() { return i; } }
package javacards; public class Main { public static void main(String[] args) { Deck deck = new Deck(); Card C; System.out.println( deck.getTotalCards() ); while (deck.getTotalCards()!= 0 ) { C = deck.drawFromDeck(); System.out.println( C.toString() ); } } }
Okay, so i just have a few questions;
1. Once i have outputted a cards name, how do i assign it a integer value? So for a 8 of clubs to = 8? I tried to use the switch statements.
2. Since this is a typical Blackjack game, working with an Ace will be tricky, since it can also be a 1 or 11. Should i declare that value in the object somehow? or worry about declaring its value when the users is promoted to take the 1 or 11.'
3. in main, i don't under stand what doing on when they wrote 'Card C;' does that mean Card is now a type with the value C, or C has a value of card?
Is this the same way Strings work? since im starting to think Strings are an object and not an actual value?
Sorry if i'm not clear, since im just confused as hell :S