Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trying to wrap my head around Objects with this simple Card Game!

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Trying to wrap my head around Objects with this simple Card Game!

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to wrap my head around Objects with this simple Card Game!

    1. As objects, instances of the Card class already have a value or should have. The basic Card attributes or characteristics are stored in the object's instance variables, including its value

    Personally, I think the Card class example you posted is a bad example, or at least it's not a very general example. A Card object should know about its own suit, value, face graphic, and name and nothing about any other card object. Much of the code in the Card class you posted should be in the Deck class.

    2. The rules of the game dictate whether the value of an Ace is 1 or 11 just as there are special rules about Jacks IN THAT GAME. That logic belongs in the game, not in the Card object. The Card object doesn't care or know the rules of game being played. The simpler the Card object can be, the better.

    3. You have general confusion about Object Oriented Programming that is not unusual. It takes a while for the light to come on. Classes, including String, define 'types' in Java. Card is a type. String is a type. int is a primitive data type. The statement

    Card card;

    is a declaration that specifies the variable 'card' to be of type Card. After card is initialized with

    card = new Card();

    card becomes a Card object.

    Type and class names in Java begin with capital letters, variables and method names begin with lowercase letters. The declaration:

    Card C;

    violates Java's naming conventions and just because of that causes confusion.

    Keep plugging away, ask questions when you get confused, and eventually it'll click. Don't give up.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Euphoraz (April 22nd, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to wrap my head around Objects with this simple Card Game!

    Thanks dude, that helps a lot but i'm still stuck on Question 1.

    I cant work out how to assign the value or even add the values together. Since i need to draw 2 cards and add the values together.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to wrap my head around Objects with this simple Card Game!

    Use Card's getRank() method to get the card objects' values:

    in cardSum = card1.getRank() + card2.getRank();

    card1 and card2 are card objects, and in your program are probably kept in a Deck, so your statement will look different.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    Euphoraz (April 22nd, 2014)

  7. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to wrap my head around Objects with this simple Card Game!

    Thanks for the prompt reply.

    I feel a bit stupid asking that question after looking at the answer, i should have known :/
    Thanks again!

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to wrap my head around Objects with this simple Card Game!

    Go easy on yourself. The answer was likely hidden by your general confusion over objects. If you keep at it, SOON the fog will lift.

Similar Threads

  1. Replies: 2
    Last Post: July 13th, 2013, 05:00 AM
  2. Re: Trying to wrap my head around Java
    By Sylis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2012, 01:54 PM
  3. Trying to wrap my head around Java
    By Sylis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2012, 08:10 AM
  4. [SOLVED] My head Hurts... such a simple problem but I'm stuck!
    By Kassino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2010, 08:38 AM
  5. Simple Game In Java (Head and Tails).
    By drkossa in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2009, 11:40 AM