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 8 of 8

Thread: Card Program"War" Class help

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Card Program"War" Class help

    Hello, I am having trouble with a few methods in one of my classes (Deck)

    I have 2 classes; Deck & Cards

    Two methods in Deck that I am unsure how to create is;

    "add" which is suppose to (accepts a card as its parameter and adds it to the bottom). I am using an array and I am not sure how to figure out the bottom of the array after x amount of cards have been won(collected) because the player

    "getSize" - Since I am using an array I either need to make a loop to count each individual element or create a counter. A counter would be more efficient in this scenario because it would locate the bottom of the array to help out with "add" method.
    Thank you for your help!

    Here is my whole class Deck
    import java.util.Random; 
     
    /* Deck.java Class
     * Holds number of cards and maintains order
     */
    public class Deck {
     
    	private final int TOTALCARDS = 52; //total amount of cards placed in the array
    	private Card[] deck; // array to hold the cards
    	public final int topdeck = 0; // top of the deck
     
    	//makes a deck of cards with one of each card
    	public Deck(){
    		deck = new Card[52];
    				int count=1;
    		for (int s = 1; s<=4; s++) {
    			for(int v = 2; v<=14; v++) {
    				deck[count] = new Card(v,s);
    				count++;
    			}
    		}
    	}
     
    	//Shuffling of the array
    	public Shuffle()
    	{
    		Random generator = new Random();
    		//Shuffles deck 500 times 
    		for (int i = 0; i < 500; i++)
    		{
    			int RandomCard = generator.nextInt(i);
     
    		Card temp;
    		temp = deck[i];
    		deck[i] = deck[RandomCard];
    		deck[RandomCard] = temp;
    		}
    	}
     
    		//pre-conditions: Deck of at least 1 card
    		//post-conditions: Removes the first card from the top of the deck and returns it
    		public Draw()
    		{
    			int temp = topdeck;
    			topdeck++;
    			return deck[temp];
    		}
     
    //		public Add(Card)
    //				{
    //				
    //				}
     
    //				public getSize()
    //				{
    //					return topdeck;
    //				}
    //	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Card Program"War" Class help

    how to figure out the bottom of the array
    You need to have an int variable that you keep pointing to the next available/empty slot in the array.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Card Program"War" Class help

    I understand that i need a counter to do that; however, I don't know where to set the counter so that it can determine that empty spot. Where exactly it should be pointing within the method, class, or main.

    Sorry I am a bit new to classes

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Card Program"War" Class help

    As you add items to the array, increment the counter. Start it at 0.
    As you take items from the array, decrement the counter.

    Are you taking items from the end of the array or from anywhere it the array?

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Card Program"War" Class help

    It would be taking away from the top or beginning (index 0) which is in my Draw() method. Then if needed adding cards to the bottom of a deck of cards which could contain x amount
    Last edited by Usoda; February 16th, 2012 at 08:08 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Card Program"War" Class help

    If you take items from an array at any locations other than the end, you can't use a counter for the number of items in the array and use it as a control for indexing the array. I consider index=0 as the beginning of the array.
    I don't know what you mean by top and bottom.

    You should look at using an ArrayList which allows you to remove items from anywhere in the list of items.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Card Program"War" Class help

    I am not strongly familiar with ArrayList. As for what I mean as top and bottom I am looking at it as a playing deck of cards sitting on a table. 52 total. Where the top of the deck; if set on the table would be index=0 and for the bottom it would be the last index in the array that's open.

    However the array, like you said is constantly changing. It will in the beginning of my game be divided into two separate decks of 26 elements. Would I make a variable counter at 26 and increment it or decremented it in main and have it passed to add

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Card Program"War" Class help

    If the contents of the array is constantly changing, you will need to give the empty slots special values so you know that they are empty. When you want something from the array, you will have to scan in from one end or the other until you find what you are looking for.

    You can say either end of an array is the top. But when programming, it starts at 0 and ends with the length of the array. I have no idea how you intend to get elements from the array.

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  4. Replies: 2
    Last Post: October 29th, 2010, 03:14 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM

Tags for this Thread