cards out of wack


L

Luke Bergen

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
 
Ad

Advertisements

L

Lee Weiner

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

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
 
K

Karl von Laudermann

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
}

...
 
Ad

Advertisements

L

Luke Bergen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top