cards out of wack

Discussion in 'Java' started by Luke Bergen, Apr 7, 2004.

  1. Luke Bergen

    Luke Bergen Guest

    hi, I'm trying to put together the constructs of a card game. this is
    what I have so far, given that System.out.print and System.out.println
    are functions that I've imported that output to screen:

    public class dealer
    {
    public static void main(String args[])
    {
    card myCard;
    for (int i = 0 ; i < 20 ; i++)
    {
    myCard = new card();
    myCard.printCard();
    }
    }
    }
    ////////////////// EOF \\\\\\\\\\\\\\\\\\


    /////////// card.java \\\\\\\\\\\\\\\\\
    import java.util.Random;

    class card
    {
    final public int JACK = 11; // these next 8 variabls will
    final public int QUEEN = 12; // just make life simpler
    final public int KING = 13; // later on, rather than remembering
    final public int ACE = 1; // what number was what suit etc.
    final public int SPADE = 0; // we are making them public so
    final public int DIAMOND = 1; // that whatever program imports
    final public int CLUB = 2; // this class won't have to worry
    final public int HEART = 3; // about the numbers either.

    // face, ace = 1, 2 = 2, 3 = 3, etc...
    private int face;
    // we will make suit be a random number 0, 3
    // fourtunatly though, we don't really even need
    // to remember which is which suit though
    private int suit;

    Random gen;

    // I know it's not totally neccisary now, but just so it
    // will be easyer to expand on, I'm keeping all the initializing
    // in the home-made constructer
    public card ()
    {
    gen = new Random();
    face = (gen.nextInt(13) + 1); // 1 = ace, and so on
    suit = gen.nextInt(3); // set the suit
    }

    public void printCard()
    {
    String sFace; // must be string in case of jack, ace, queen, or
    king
    switch (face)
    {
    case JACK:
    sFace = "Jack";
    break;
    case QUEEN:
    sFace = "Queen";
    break;
    case KING:
    sFace = "King";
    break;
    case ACE:
    sFace = "Ace";
    break;
    default: // it's none of the weird cards so
    sFace = "N"; // we will test for "N" in output
    break; // and output acordingly if need be
    }

    // now for the suit
    String outSuit = "";
    switch (suit)
    {
    case SPADE:
    outSuit = "Spades";
    break;
    case DIAMOND:
    outSuit = "Diamonds";
    break;
    case CLUB:
    outSuit = "Clubs";
    break;
    case HEART:
    outSuit = "Hearts";
    break;
    default:
    outSuit = "oops, I'm sorry, an error occured somewhere";
    break;
    }
    if (sFace == "N")
    System.out.print(face + " of ");
    else
    System.out.print(sFace + " of ");
    System.out.println(outSuit);
    }
    }
    ///////////////////////////// EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\



    but for some reason my output has a random card, but it will be that
    same random card for every iteration of the loop, sometimes it will
    change to a different card midway through the loop but usially it's
    the same the whole way. now whats weird is if I run this program
    twice, really fast it will sometimes have the same random card between
    runs, in other words
    run1:
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    run2: run just after run1 has finished
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    ace of spades
    some other card
    some other card
    etc.

    it's weird, I don't understand why it's not working the way I want it
    to.
    any thoughts on this would be greatly appreciated, thanks in advance.
    - Traddles
    Luke Bergen, Apr 7, 2004
    #1
    1. Advertising

  2. Luke Bergen

    Lee Weiner Guest

    It's because you're recreating the Random object every time you create one of
    your cards. The Random object uses the computer's time as a seed and,
    normally, you're twenty cards are being dealt with the same random seed, hence
    the same values produced. Sometimes, the clock ticks in the middle of your
    loop, and the values change. The solution is to instantiate the Random object
    in main(), not in the card class, and pass a reference to it to the card class
    in the constructor. Now in the card class, you'll get new numbers every time
    you call nextInt().

    Lee Weiner
    lee AT leeweiner DOT org

    In article <>,
    (Luke Bergen) wrote:
    >hi, I'm trying to put together the constructs of a card game. this is
    >what I have so far, given that System.out.print and System.out.println
    >are functions that I've imported that output to screen:
    >
    >public class dealer
    >{
    > public static void main(String args[])
    > {
    > card myCard;
    > for (int i = 0 ; i < 20 ; i++)
    > {
    > myCard = new card();
    > myCard.printCard();
    > }
    > }
    >}
    >////////////////// EOF \\\\\\\\\\\\\\\\\\
    >
    >
    >/////////// card.java \\\\\\\\\\\\\\\\\
    >import java.util.Random;
    >
    >class card
    >{
    > final public int JACK = 11; // these next 8 variabls will
    > final public int QUEEN = 12; // just make life simpler
    > final public int KING = 13; // later on, rather than remembering
    > final public int ACE = 1; // what number was what suit etc.
    > final public int SPADE = 0; // we are making them public so
    > final public int DIAMOND = 1; // that whatever program imports
    > final public int CLUB = 2; // this class won't have to worry
    > final public int HEART = 3; // about the numbers either.
    >
    > // face, ace = 1, 2 = 2, 3 = 3, etc...
    > private int face;
    > // we will make suit be a random number 0, 3
    > // fourtunatly though, we don't really even need
    > // to remember which is which suit though
    > private int suit;
    >
    > Random gen;
    >
    > // I know it's not totally neccisary now, but just so it
    > // will be easyer to expand on, I'm keeping all the initializing
    > // in the home-made constructer
    > public card ()
    > {
    > gen = new Random();
    > face = (gen.nextInt(13) + 1); // 1 = ace, and so on
    > suit = gen.nextInt(3); // set the suit
    > }
    >
    > public void printCard()
    > {
    > String sFace; // must be string in case of jack, ace, queen, or
    >king
    > switch (face)
    > {
    > case JACK:
    > sFace = "Jack";
    > break;
    > case QUEEN:
    > sFace = "Queen";
    > break;
    > case KING:
    > sFace = "King";
    > break;
    > case ACE:
    > sFace = "Ace";
    > break;
    > default: // it's none of the weird cards so
    > sFace = "N"; // we will test for "N" in output
    > break; // and output acordingly if need be
    > }
    >
    > // now for the suit
    > String outSuit = "";
    > switch (suit)
    > {
    > case SPADE:
    > outSuit = "Spades";
    > break;
    > case DIAMOND:
    > outSuit = "Diamonds";
    > break;
    > case CLUB:
    > outSuit = "Clubs";
    > break;
    > case HEART:
    > outSuit = "Hearts";
    > break;
    > default:
    > outSuit = "oops, I'm sorry, an error occured somewhere";
    > break;
    > }
    > if (sFace == "N")
    > System.out.print(face + " of ");
    > else
    > System.out.print(sFace + " of ");
    > System.out.println(outSuit);
    > }
    >}
    >///////////////////////////// EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    >
    >
    >
    >but for some reason my output has a random card, but it will be that
    >same random card for every iteration of the loop, sometimes it will
    >change to a different card midway through the loop but usially it's
    >the same the whole way. now whats weird is if I run this program
    >twice, really fast it will sometimes have the same random card between
    >runs, in other words
    >run1:
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >run2: run just after run1 has finished
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >ace of spades
    >some other card
    >some other card
    >etc.
    >
    >it's weird, I don't understand why it's not working the way I want it
    >to.
    >any thoughts on this would be greatly appreciated, thanks in advance.
    >- Traddles
    Lee Weiner, Apr 7, 2004
    #2
    1. Advertising

  3. (Lee Weiner) wrote in message news:<gVLcc.84856$w54.501827@attbi_s01>...
    > The solution is to instantiate the Random object
    > in main(), not in the card class, and pass a reference to it to the card class
    > in the constructor.


    IMNSHO, proper OO design dictates that main() shouldn't be creating
    the card objects' Random instance. The better approach is to make the
    Random object a static member of the card class:

    class card
    {
    ...

    static Random gen = new Random();

    // I know it's not totally neccisary now, but just so it
    // will be easyer to expand on, I'm keeping all the initializing
    // in the home-made constructer
    public card ()
    {
    face = (gen.nextInt(13) + 1); // 1 = ace, and so on
    suit = gen.nextInt(3); // set the suit
    }

    ...
    Karl von Laudermann, Apr 7, 2004
    #3
  4. Luke Bergen

    Luke Bergen Guest

    (Karl von Laudermann) wrote in message news:<>...
    > (Lee Weiner) wrote in message news:<gVLcc.84856$w54.501827@attbi_s01>...
    > > The solution is to instantiate the Random object
    > > in main(), not in the card class, and pass a reference to it to the card class
    > > in the constructor.

    >
    > IMNSHO, proper OO design dictates that main() shouldn't be creating
    > the card objects' Random instance. The better approach is to make the
    > Random object a static member of the card class:
    >
    > class card
    > {
    > ...
    >
    > static Random gen = new Random();
    >
    > // I know it's not totally neccisary now, but just so it
    > // will be easyer to expand on, I'm keeping all the initializing
    > // in the home-made constructer
    > public card ()
    > {
    > face = (gen.nextInt(13) + 1); // 1 = ace, and so on
    > suit = gen.nextInt(3); // set the suit
    > }
    >
    > ...


    thanks, this was exactly the help I was looking for. thanks for you
    guys's help and reply's
    Luke Bergen, Apr 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kapil Sachdeva

    Re: credit cards

    Kapil Sachdeva, Oct 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    361
    Vincent V
    Oct 16, 2003
  2. Scott M.

    Accepting credit cards in ASP.NET

    Scott M., Jan 20, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    326
    Michael Pearson
    Jan 21, 2004
  3. Preston Crawford

    Credit cards with netplugin?

    Preston Crawford, Jan 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    298
    Preston Crawford
    Jan 23, 2004
  4. VB Programmer

    Accepting Credit Cards

    VB Programmer, Nov 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    302
    Steve C. Orr [MVP, MCSD]
    Nov 14, 2004
  5. =?Utf-8?B?QXJuZQ==?=

    Credit Cards

    =?Utf-8?B?QXJuZQ==?=, Jan 17, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    440
    George
    Jan 18, 2005
Loading...

Share This Page