Assigning Int values to enum list object? a priori or post-coitum?  
Author Message
John queue Public





PostPosted: 2005-12-4 11:13:00 Top

java-programmer, Assigning Int values to enum list object? a priori or post-coitum? Hi.I'm a brainlocked javaholic. I haven't had a drink in.....
wah,wah,wah. ;-)

I'm writing a cardgame which creates card objects from enums,
shuffles them, then deals a hand, which then becomes interactive.
Then I'll GUI that, but first the nuts and bolts. Keep in mind that
this is in progress, so some stuff here may not even be needed....

I've shlepped toStrings in there for later (hopeful) assigning(with
wildcards, also hopeful) to card icons into a JFrame or other simple
layout.
What I need to do first is just a simple method that assigns int
values to the card objects for integer card-game comparisons,etc.

Do I need to assign a static int array for these objects beforehand?
Everything I've read seems to deal with arrays and lists that are
already assigned some value or other.

(I've also got a simple Hand class at the end, which I really should
plug in here somewhere, since it needs to compile after the main one,
but haven't done that yet.)

import java.util.Stack;
import java.util.Collections;
import java.util.Vector;
//import java.util.JList; don't need it yet,if ever-

enum Suit {Clubs, Diamonds, Hearts, Spades}
enum Rank // class masquerading as an enum

{Ace(1), Two(2), Three(3), Four(4), Five(5), Six(6),Seven(7),
Eight(8), Nine (9), Ten(10), Jack(10), Queen(10), King(10);

Rank (int RankValue)
{
this.RankValue = RankValue;
}
public int RankValue() // can I buy an int, Vanna?
{
return RankValue;
}
public int RankValue;
}

public class DeckCards
{

private Stack<Card> deck = new Stack<Card>();

/* enumeration with constants that represent the game status
// private enum Status { PLAY,HIT_CARD,STAND, WON, LOST }; //insert
a //switch to progress game. */

public DeckCards() //constructor
{

for(Suit suit : Suit.values())
for(Rank rank : Rank.values())

deck.push(new Card(rank, suit)); //push into the stack

Collections.shuffle(deck); // Shuffle the deck with shuffle
//randomizer

} /*insert switch block statements here to show options to
play, quit, etc. */

public class Hand
{
// Add a card to the hand
public void add(Card card)
{
hand.add(card);
}

public String toString()
{
StringBuilder str = new StringBuilder();
for(Card card : hand)
{
str.append(" "+ card);
}
return str.toString();
}

private Vector<Card> hand = new Vector<Card>(); // Stores a hand of
cards
}

public Hand dealHand() //deal openers -
{
Hand dealerHand = new Hand();
Hand playerHand = new Hand();


for(int i = 0; i<2; i++) //however many cards to fill a hand.

{
{
dealerHand.add((Card)deck.pop());/*this tests
for now to see if random hand object is created. They are*/
System.out.println("\nDealer hand is:\n" +
dealerHand );
}

{
playerHand.add((Card)deck.pop());
System.out.println("\nPlayer hand is\n" +
playerHand );
}
//return dealerHand;
}
return playerHand;
}

/* MAIN */

public static void main(String[] args)
{
System.out.println("\nLet's play a Game!\n");
//while(true)
DeckCards DeckCardsTest = new DeckCards();
DeckCardsTest.dealHand();
}
}


//Here's the Hand class:

// have to make DeckCards.class first before this will compile

public class Card
{
public Card(Rank rank, Suit suit)
{
this.rank = rank;
this.suit = suit;
}

public String toString()
{
return rank + " of " + suit;
}

private Suit suit;
private Rank rank;
}


 
John queue Public





PostPosted: 2005-12-5 5:38:00 Top

java-programmer >> Assigning Int values to enum list object? a priori or post-coitum? On Sat, 03 Dec 2005 21:12:55 -0600, John queue Public
<email***@***.com> wrote:

>Hi.I'm a brainlocked javaholic. I haven't had a drink in.....
> wah,wah,wah. ;-)
>
> I'm writing a cardgame which creates card objects from enums,
>shuffles them, then deals a hand, which then becomes interactive.
>Then I'll GUI that, but first the nuts and bolts. Keep in mind that
>this is in progress, so some stuff here may not even be needed....
>
> I've shlepped toStrings in there for later (hopeful) assigning(with
>wildcards, also hopeful) to card icons into a JFrame or other simple
>layout.
>What I need to do first is just a simple method that assigns int
>values to the card objects for integer card-game comparisons,etc.
>
> Do I need to assign a static int array for these objects beforehand?
>Everything I've read seems to deal with arrays and lists that are
>already assigned some value or other.
>
> (I've also got a simple Hand class at the end, which I really should
>plug in here somewhere, since it needs to compile after the main one,
>but haven't done that yet.)
>
>import java.util.Stack;
>import java.util.Collections;
>import java.util.Vector;
>//import java.util.JList; don't need it yet,if ever-
>
> enum Suit {Clubs, Diamonds, Hearts, Spades}
> enum Rank // class masquerading as an enum
>
> {Ace(1), Two(2), Three(3), Four(4), Five(5), Six(6),Seven(7),
> Eight(8), Nine (9), Ten(10), Jack(10), Queen(10), King(10);
>
> Rank (int RankValue)
> {
> this.RankValue = RankValue;
> }
> public int RankValue() // can I buy an int, Vanna?
> {
> return RankValue;
> }
> public int RankValue;
>}
>
>public class DeckCards
>{
>
> private Stack<Card> deck = new Stack<Card>();
>
> /* enumeration with constants that represent the game status
> // private enum Status { PLAY,HIT_CARD,STAND, WON, LOST }; //insert
>a //switch to progress game. */
>
> public DeckCards() //constructor
> {
>
> for(Suit suit : Suit.values())
> for(Rank rank : Rank.values())
>
> deck.push(new Card(rank, suit)); //push into the stack
>
> Collections.shuffle(deck); // Shuffle the deck with shuffle
>//randomizer
>
> } /*insert switch block statements here to show options to
>play, quit, etc. */
>
>public class Hand
>{
> // Add a card to the hand
> public void add(Card card)
> {
> hand.add(card);
> }
>
> public String toString()
> {
> StringBuilder str = new StringBuilder();
> for(Card card : hand)
> {
> str.append(" "+ card);
> }
> return str.toString();
> }
>
> private Vector<Card> hand = new Vector<Card>(); // Stores a hand of
>cards
>}
>
> public Hand dealHand() //deal openers -
> {
> Hand dealerHand = new Hand();
> Hand playerHand = new Hand();
>
>
> for(int i = 0; i<2; i++) //however many cards to fill a hand.
>
> {
> {
> dealerHand.add((Card)deck.pop());/*this tests
>for now to see if random hand object is created. They are*/
> System.out.println("\nDealer hand is:\n" +
>dealerHand );
> }
>
> {
> playerHand.add((Card)deck.pop());
> System.out.println("\nPlayer hand is\n" +
>playerHand );
> }
> //return dealerHand;
> }
> return playerHand;
> }
>
>/* MAIN */
>
> public static void main(String[] args)
> {
> System.out.println("\nLet's play a Game!\n");
> //while(true)
> DeckCards DeckCardsTest = new DeckCards();
> DeckCardsTest.dealHand();
> }
>}
>
>
>//Here's the Hand class:
>
>// have to make DeckCards.class first before this will compile
>
>public class Card
>{
> public Card(Rank rank, Suit suit)
> {
> this.rank = rank;
> this.suit = suit;
> }
>
> public String toString()
> {
> return rank + " of " + suit;
> }
>
> private Suit suit;
> private Rank rank;
>}
>

ehhh..never mind. Think I've got it. :0

regards-