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