Using method on arraylist references have unwanted results, Need help!
hello im doing a school project for my ap computer programming class and we have to build a program that can deal 20 random cards( doesnt have to be of a 52 card deck) i have it set up so it will create 20 card object containing suit and value and reference them in an arraylist. then uses the deal_one method on each object individually and then prints each card in the array. the problem i am having is that each card printed is exactly the same, not sure why.below is my code.
i plan on using some graphics with it later but now im just trying to get the logic of it right.
Code:
/**
* this is the driver class for a random card generator that generates 20 random cards
*
* @author ___ _ __ _ _
* / __\ |__ __ _ ___ ___ / _\(_)___| | ___ _ __
* / / | '_ \ / _` / __|/ _ \ \ \ | / __| |/ _ \ '__|
* / /___| | | | (_| \__ \ __/ _\ \| \__ \ | __/ |
* \____/|_| |_|\__,_|___/\___| \__/|_|___/_|\___|_|
* @version (2/29/2012)
*/
import java.util.ArrayList;
public class Driver
{
public void main (String[] args)
{
ArrayList deck = new ArrayList();
Card crd = new Card();
Card crd1 = new Card();
Card crd2 = new Card();
Card crd3 = new Card();
Card crd4 = new Card();
Card crd5 = new Card();
Card crd6 = new Card();
Card crd7 = new Card();
Card crd8 = new Card();
Card crd9 = new Card();
Card crd10 = new Card();
Card crd11 = new Card();
Card crd12 = new Card();
Card crd13 = new Card();
Card crd14 = new Card();
Card crd15 = new Card();
Card crd16 = new Card();
Card crd17 = new Card();
Card crd18 = new Card();
Card crd19 = new Card();
Card crd20 = new Card();
deck.add (crd1);
deck.add (crd2);
deck.add (crd3);
deck.add (crd4);
deck.add (crd5);
deck.add (crd6);
deck.add (crd7); // <<-- fills the arraylist with 20 Card objects
deck.add (crd8);
deck.add (crd9);
deck.add (crd10);
deck.add (crd11);
deck.add (crd12);
deck.add (crd13);
deck.add (crd14);
deck.add (crd15);
deck.add (crd16);
deck.add (crd17);
deck.add (crd18);
deck.add (crd19);
deck.add (crd20);
for (int x=0;x<20;x++)
{
Card y = (Card)deck.get(x); //
y.deal_one(); // <<-- uses deal_one method to randomize each card in array
deck.add (x,y); // then replaces the card back to the same location
}
for (int x=0;x<20;x++)
{
Card current_card = (Card)deck.get (x);
int val = current_card.get_value();
System.out.println (val);
}
}
}
Code:
/**
* the card class has card object
*
* @author ___ _ __ _ _
* / __\ |__ __ _ ___ ___ / _\(_)___| | ___ _ __
* / / | '_ \ / _` / __|/ _ \ \ \ | / __| |/ _ \ '__|
* / /___| | | | (_| \__ \ __/ _\ \| \__ \ | __/ |
* \____/|_| |_|\__,_|___/\___| \__/|_|___/_|\___|_|
* @version (2/29/2012)
*/
import java.util.Random;
public class Card
{
public Random generator = new Random();
private int value, suit;
public Card()
{
suit = 1;
value = 1;
}
public void deal_one()
{
suit = generator.nextInt(4) + 1;
value = generator.nextInt(13)+1;
}
public String get_suit()
{
String show = "nothing";
int x = suit;
if (x == 0){show = "Ace";}
if (x == 1){show = "Heart";}
if (x == 2){show = "Spade";}
if (x == 3){show = "Club";}
return show;
}
public int get_value()
{
return value;
}
}
Edit:Also i forgot to ask if there is a better way to instantiate all those objects
Re: Using method on arraylist references have unwanted results, Need help!
If it were my program, I wouldn't put any randomization in my Card class but instead would just have that class be the blueprint for creating a single Card object and that's it. I'd not give it a parameterless constructor but rather would have two parameters passed in so that the card rank and suit are determined in the constructor.
Re: Using method on arraylist references have unwanted results, Need help!
ok thank you ill try that now and report the results. i like that idea better
Re: Using method on arraylist references have unwanted results, Need help!
It Works!! thanks , what do you think about my last question? is there anyway to shorten this?
Card crd2 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd3 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd4 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd5 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd6 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd7 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd8 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd9 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd10 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd11 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd12 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd13 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd14 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd15 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd16 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd17 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd18 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd19 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
Card crd20 = new Card(b,a);
deck.add (crd1);
deck.add (crd2);
deck.add (crd3);
deck.add (crd4);
deck.add (crd5);
deck.add (crd6);
deck.add (crd7); // <<-- fills the arraylist with 20 Card objects
deck.add (crd8);
deck.add (crd9);
deck.add (crd10);
deck.add (crd11);
deck.add (crd12);
deck.add (crd13);
deck.add (crd14);
deck.add (crd15);
deck.add (crd16);
deck.add (crd17);
deck.add (crd18);
deck.add (crd19);
deck.add (crd20);
Re: Using method on arraylist references have unwanted results, Need help!
Yes, use arrays and a for loop.
Re: Using method on arraylist references have unwanted results, Need help!
Sorry i have only been coding for about a month, could you give a little example
Re: Using method on arraylist references have unwanted results, Need help!
It may be a bit over your head now, but read this tutorial; it has a nice implementation for playing cards.
kind regards,
Jos
Re: Using method on arraylist references have unwanted results, Need help!
Thanks! this seems like it will be really useful i cant quite follow it at the moment but im going to book mark it and keep reading it until i understand it.
thanks again everyone i will defiantly be coming back to this forum.
Re: Using method on arraylist references have unwanted results, Need help!
Since you are not using the 'crd<n>' variables themselves, simply adding them to the array, Fubarable is suggesting you just do a for loop 20 times, generating a card and adding it to the 'deck'.
You already have a for loop in your original code that would do the job, just change the code inside it to create a single card and add it to the deck.