Question - making rummy card game applet

Hi, I am not sure if this is the right place to be putting this, if not please let me know. I'm trying to make an applet for the card game rummy for my final project for school. I was wondering if I could get some help on how to load a card image from a file and randomizing the images for the deal button or if not that then I am also trying a much simpler way due to time constraints. I was trying to make two arrays of strings: one for the facevalue of the card and another for the suit value, then randomize those and draw a string to the correct places for the dealt hand. I can't seem to get this to work well so far. Any help is greatly appreciated. Here is the code for the basic gui that I have now.

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import java.util.Random;

import java.lang.*;

public class CardGame extends Applet implements ActionListener {

Button Deal;

Button NewGame;

Button PutDown;

Color bgColor;

Color rectColor;

Image deck;

MediaTracker mt;

CheckboxGroup radioGroup;

Checkbox radio1;

Checkbox radio2;

Checkbox radio3;

Checkbox radio4;

Checkbox radio5;

Checkbox radio6;

Checkbox radio7;

**Its hard to tell but the arrays are commented out for now.

//String[] faceValue = {"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"};

//String[] suitValue = {"CLUBS", "HEARTS", "SPADES", "DIAMONDS"};

public void init()

{

setLayout(null);

mt = new MediaTracker(this);

Deal = new Button("Deal");

NewGame = new Button("New Game");

PutDown = new Button("Put Down");

Deal.setBounds(750, 450, 75, 25);

NewGame.setBounds(750, 475, 75, 25);

PutDown.setBounds(750, 425, 75, 25);

add(Deal);

add(NewGame);

add(PutDown);

Deal.addActionListener(this);

NewGame.addActionListener(this);

PutDown.addActionListener(this);

radioGroup = new CheckboxGroup();

radio1 = new Checkbox(" ", radioGroup, false);

radio2 = new Checkbox(" ", radioGroup, false);

radio3 = new Checkbox(" ", radioGroup, false);

radio4 = new Checkbox(" ", radioGroup, false);

radio5 = new Checkbox(" ", radioGroup, false);

radio6 = new Checkbox(" ", radioGroup, false);

radio7 = new Checkbox(" ", radioGroup, false);

add(radio1);

add(radio2);

add(radio3);

add(radio4);

add(radio5);

add(radio6);

add(radio7);

radio1.setBounds(143, 525, 10, 10);

radio2.setBounds(222, 525, 10, 10);

radio3.setBounds(301, 525, 10, 10);

radio4.setBounds(380, 525, 10, 10);

radio5.setBounds(459, 525, 10, 10);

radio6.setBounds(538, 525, 10, 10);

radio7.setBounds(617, 525, 10, 10);

}

public void Randomize()

{

//this is where I need the help mostly.

}

public void Stop()

{

}

public void actionPerformed(ActionEvent evt)

{

if (evt.getSource()== Deal)

{

setBackground(Color.green); //test

}

else

{

Deal.setLabel("Not there, here!"); //another test

}

if(radio1.getState())

{

setBackground(Color.blue); //just a test to make sure I know how to use the radio buttons

}

}

public void paint(Graphics g) {

g.drawString("Welcome to Rummy!!", 300, 50 );

g.drawRect(100, 100, 600, 440);

g.drawRect(110, 420, 75, 100);

g.drawRect(190, 420, 75, 100);

g.drawRect(270, 420, 75, 100);

g.drawRect(350, 420, 75, 100);

g.drawRect(430, 420, 75, 100);

g.drawRect(510, 420, 75, 100);

g.drawRect(590, 420, 75, 100);

g.drawRect(110, 120, 75, 100);

g.drawRect(190, 120, 75, 100);

g.drawRect(270, 120, 75, 100);

g.drawRect(350, 120, 75, 100);

g.drawRect(430, 120, 75, 100);

g.drawRect(510, 120, 75, 100);

g.drawRect(590, 120, 75, 100);

g.drawRect(300, 250, 75, 100);

g.drawRect(380, 250, 75, 100);

g.drawString("A S", 220, 445);

}

}

[4180 byte] By [mphamma8a] at [2007-10-2 21:46:02]
# 1

Before going into the minor details such as loading images, sit back and plan the structure of the programme.

For starters take an Object Orientated approach and define a "playingCard" class, then any randomisation would be to select a "playingCard"

Just at a quick glance:

How are you going to keep track of cards?

How will the programme "notice" a run by comparing String rather than numbers?

I think you may have rushed into the project; but just as I would have done at high school.

bamkin-ov-lestaa at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 2

Yes I probably did rush into it, but you bring up a good point about tracking the cards. I need to do this unless they want to play with duplicate cards! I will figure that out and another question how do you use different classes when creating an applet. Would you use the same way to invoke the class into the main applet class. I was much better at VB lol.

mphamma8a at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 3
Same as an application.You're programming in java..make use of the OO nature or your code will start to look like.....well.....your code :-)
Norweeda at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 4

I would make a subclass of the cards

public class rummy extends Applet implements ActionListener

{

playingCard[] cards=new playingCard[52];//assuming there is only one deck;

int[][] inHand;

int[] inDeck;

int topCard;

public void init()

{

int upto=0;

for (int suit=0 ; suit<4 ; suit++)

for (int num=0 ; num<13 ; num++)

{

cards[upto]=new playingCard(suit, num);//card[0] is always aceOfHearts ect

}

//rest of init;

inHand=new int[numberOfPlayers][7];//space for 7 cards per player

inDeck=new int[52-(numberOfPlayers*7)];//space for all cards besides the ones belonging to players

topCard=1;//all cards in the inDeck array are in the deck (not in hand), the ones above the value of topCard are face down, the ones blow the value of topCard are faceUp (all cards are facedown except 1 at the begining of the game i.e.topCard=1

}

//rest of methods;

public class playingCard

{

static int SUIT_HEARTS=0;//if (playingCard.getSuit()==SUIT_HEARTS) it is a heart

static int SUIT_SPADES=1;

static int SUIT_DIAMONDS=2;

static int SUIT_CLUBS=3;

public playingCard(int suit, int num)

{

this.suit=suit;

this.num=num;

//tip: if you name the images with numbers you can load them automatically from here

}

public int getSuit(){return suit;}

}

Here is a structure for you to get started with

bamkin-ov-lestaa at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 5

thanks for that...could I use something like this for the playing card class though:

public class Card {

public final static int SPADES = 0,// Codes for the 4 suits.

HEARTS = 1,

DIAMONDS = 2,

CLUBS = 3;

public final static int ACE = 1, // Codes for the non-numeric cards.

JACK = 11,//Cards 2 through 10 have their

QUEEN = 12,//numerical values for their codes.

KING = 13;

private final int suit;// The suit of this card, one of the constants

//SPADES, HEARTS, DIAMONDS, CLUBS.

private final int value; // The value of this card, from 1 to 11.

public Card(int theValue, int theSuit) {

// Construct a card with the specified value and suit.

// Value must be between 1 and 13. Suit must be between

// 0 and 3. If the parameters are outside these ranges,

// the constructed card object will be invalid.

value = theValue;

suit = theSuit;

}

public int getSuit() {

// Return the int that codes for this card's suit.

return suit;

}

public int getValue() {

// Return the int that codes for this card's value.

return value;

}

public String getSuitAsString() {

// Return a String representing the card's suit.

// (If the card's suit is invalid, "?" is returned.)

switch ( suit ) {

case SPADES:return "Spades";

case HEARTS:return "Hearts";

case DIAMONDS: return "Diamonds";

case CLUBS:return "Clubs";

default:return "?";

}

}

public String getValueAsString() {

// Return a String representing the card's value.

// If the card's value is invalid, "?" is returned.

switch ( value ) {

case 1:return "Ace";

case 2:return "2";

case 3:return "3";

case 4:return "4";

case 5:return "5";

case 6:return "6";

case 7:return "7";

case 8:return "8";

case 9:return "9";

case 10: return "10";

case 11: return "Jack";

case 12: return "Queen";

case 13: return "King";

default: return "?";

}

}

public String toString() {

// Return a String representation of this card, such as

// "10 of Hearts" or "Queen of Spades".

return getValueAsString() + " of " + getSuitAsString();

}

} // end class Card

except now how would I make something like this random I know theres a bunch of work before that, but just curious. Thanks

mphamma8a at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 6

I would probably store them all into an array list in order and then randomly pick a card out of the list and add it to another array list. Delete it from the first array list and trim. repeat untill all cards are gone.

The second array list will be a randomized list of cards with no repeats.

Norweeda at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 7

Personally I don't see why the programme would need to 'know' wheather the card is an ACE or a card with a numeric value of 0 or wheather it matters whether the programme thinks that the THREEofHEARTS has a numberic value of 2 and a suit value of 0.

Similarly the programme need only 'know' that a card of suit 0 has its suit in common with another card of suit 0, the 'meaning' of 0 is irrelevent.

I have not yet seen a cards programme that ever says "Three of Hearts", the programme does not nessarily process the card in the same way we do - from an image.

Norweed's solution is the most suitable if required

int[] humanRecognisableValue=new int[13]

humanRecognisableValue[0]="ACE"

humanRecognisableValue[1]="ONE"

//ect

As I said before the cards need a numeric value so that the programme can 'recognise' runs. It cannot tell that "THREE", "FOUR" and "FIVE" are sequential; only that 3, 4 and 5 are

bamkin-ov-lestaa at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 8

thanks for all the help...one more question....my dad being a programmer for 20 years helped me out a bit last night and I'm still using string output instead of the image output for the cards due to time constraints. I was able to create and shuffle a string of cards and output 7 to the screen when the deal button is pressed, but when they are outputted to the applet it's just in one line, making it overlap onto other cards. I don't want to make the 'cards' larger because then it will look even dumber, but I would like to separate the string so that for instance 'Ace' would be on the top line, 'of' on the second, then 'clubs' on the third. Is there anyway to do this without creating separate strings and placing them at different coordinates? Escape sequences aren't working with the applet, but I assume there is a reason for that. Any help with this one greatly appreciated. Thanks

mphamma8a at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...
# 9
Again look at the problem from an OO view.Write a method that takes a string then splits it into the three words (dividing when there is a [space]) and draws onto 3 rows.Then you can call a method to draw a single string onto 3 rows.Are you using JLabels ?
bamkin-ov-lestaa at 2007-7-14 1:01:37 > top of Java-index,Java Essentials,Java Programming...