Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Damn it! My Card game doesn't wanna work
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ultra Jugulator




PostPosted: Fri Nov 25, 2005 2:10 am   Post subject: Damn it! My Card game doesn't wanna work

So, here is my program for my assignment due tomorrow:
code:
import java.util.Random;
import javax.swing.JOptionPane;
public class A2
{
    public static void main(String[]args)
    {
        Deck deck = new Deck();
        Player human = new Player();
        Player naive = new Player();
        Player smart = new Player();
        boolean clockWise = true;
        int number;
        Card lastCardPlayed;
        String input;
        int sum;
        Pile pile = new Pile();
       
        System.out.println("The Total Number of Cards in the Pile is:" + pile.getTotal() + "\n");
       
        while(clockWise)
        {
            for(int i = 0; i < NUMBER_OF_CARDS_IN_HAND; i++)
            {
                int n = i+1;
                System.out.println("The Human Card Number " + n + ": " + human.getCard(i));
            }
            input = JOptionPane.showInputDialog("What Card would you like to play? Please insert card number");
            number = Integer.parseInt(input);
            number--;
            lastCardPlayed = human.play(number);
            pile.setTotal(pile.add(lastCardPlayed.getValue()));
            if(lastCardPlayed.getValue() == 99)
                pile.setTotal(99);
            sum = pile.getTotal();
            System.out.println("The Last Card Played was:" + lastCardPlayed.getName() + "\n");
            System.out.println("The Total Number of Cards in the pile is:" + sum + "\n");
           
            if(sum > 99)
            {
                System.out.println("you lose....");
                System.exit(0);
            }
           
            if(lastCardPlayed.getName().equals("Four"))
            {
                System.out.println("The direction has been reversed");
                clockWise = !clockWise;
            }
           
            System.out.println("Naive player's turn\n");
           
            lastCardPlayed = naive.naivePlay(pile.getTotal());
            pile.setTotal(pile.add(lastCardPlayed.getValue()));
            if(lastCardPlayed.getValue() == 99)
                pile.setTotal(99);
            sum = pile.getTotal();
            System.out.println("The Last Card played was:" + lastCardPlayed.getName() + "\n");
            System.out.println("The Total Number of Cards in the Pile is:" + sum + "\n");
           
        }
       
    }
    public static final int NUMBER_OF_CARDS_IN_HAND = 5;
}

class Player extends Deck
{
    private Card[] hand;
    public Player()
    {
        hand = new Card[5];
        for(int i = 0; i < 5; i++)
        {
            hand[i] = draw();
        }
    }
   
    public String getCard(int i)
    {
        return hand[i].getName();
    }   
   
    public Card play(int i)
    {
        Card lastCard = discard(i);
        draw();
        return lastCard;       
    }
    public Card naivePlay(int i)
    {
        int cardNumber = 0;
        int sum = i;
        for(int j = 0; j < 5; j++)
        {
            if(sum < 99)
            {
                int temp = hand[j].getValue();
                sum+=temp;
                if(sum > 99)
                    sum-=temp;
                else
                {
                    cardNumber = j;
                    break;
                }
            }
           
            if(sum == 99)
            {
                int temp = hand[j].getValue();
                if(temp == 0)
                    cardNumber = j;
            }
        }
        if(sum == 99 && hand[0].getValue()!= 0 || hand[0].getValue()!= -10 || hand[1].getValue()!= 0 || hand[1].getValue()!= -10 || hand[2].getValue()!= 0 || hand[2].getValue()!= -10 || hand[3].getValue()!= 0 || hand[3].getValue()!= -10 ||hand[4].getValue()!= 0 || hand[4].getValue()!= -10)
        {
            Random generator = new Random();
            cardNumber = generator.nextInt(5);
            return hand[cardNumber];
        }
        return hand[cardNumber];
    }

   
    public Card smartPlay(int i)
    {
        Card card = discard(i);
        draw();
        return card;
    }
   
    public Card draw()
    {
        Card card = super.draw();
        return card;
    }
   
    public Card discard(int i)
    {
        Card card = hand[i];
        System.arraycopy(hand, i + 1, hand, i, hand.length - i - 1);
        return card;
    }
}

class Pile extends Player
{
    private int total;
    public Pile()
    {
        total = 0;
    }
   
    public int add(int i)
    {
        int sum = getTotal();
        int cardValue = i;
        sum += cardValue;
        return sum;
    }
   
    public void setTotal(int i)
    {
        total = i;
    }
   
    public int getTotal()
    {
        return total;
    }
}



class Deck
{
    private Card[] deck;
    private int card;
    public Deck()
    {
       
        deck = new Card[TOTAL_NUMBER_OF_CARDS];
       
        deck[0] = new Card("Ace", 1);
        deck[1] = new Card("Ace", 1);
        deck[2] = new Card("Ace", 1);
        deck[3] = new Card("Ace", 1);
       
        deck[4] = new Card("Two", 2);
        deck[5] = new Card("Two", 2);
        deck[6] = new Card("Two", 2);
        deck[7] = new Card("Two", 2);
       
        deck[8] = new Card("Three", 3);
        deck[9] = new Card("Three", 3);
        deck[10] = new Card("Three", 3);
        deck[11] = new Card("Three", 3);
       
        deck[12] = new Card("Four", 0);
        deck[13] = new Card("Four", 0);
        deck[14] = new Card("Four", 0);
        deck[15] = new Card("Four", 0);
       
        deck[16] = new Card("Nine", 0);
        deck[17] = new Card("Nine", 0);
        deck[18] = new Card("Nine", 0);
        deck[19] = new Card("Nine", 0);
       
        deck[20] = new Card("Ten", 10);
        deck[21] = new Card("Ten", 10);
        deck[22] = new Card("Ten", 10);
        deck[23] = new Card("Ten", 10);
       
        deck[24] = new Card("Jack", 10);
        deck[25] = new Card("Jack", 10);
        deck[26] = new Card("Jack", 10);
        deck[27] = new Card("Jack", 10);
       
        deck[28] = new Card("Queen", -10);
        deck[29] = new Card("Queen", -10);
        deck[30] = new Card("Queen", -10);
        deck[31] = new Card("Queen", -10);
       
        deck[32] = new Card("King", 99);
        deck[33] = new Card("King", 99);
        deck[34] = new Card("King", 99);
        deck[35] = new Card("King", 99);
    }
   
    public String getCard(int i)
    {
        return deck[i].getName();
    }
   
    public int shuffle()
    {
        Random generator = new Random();
        card = generator.nextInt(deck.length);
        return card;
    }
   
    public Card draw()
    {
        int cardSlot = shuffle();
        Card card = deck[cardSlot];
        System.arraycopy(deck , cardSlot + 1, deck, cardSlot, deck.length - cardSlot - 1);
        return card;
    }
   
    public int getLength()
    {
        return deck.length;
    }
   
    public final static int NUMBER_OF_SUITS = 4;
    public final static int NUMBER_OF_CARDS_IN_SUITE = 9;
    public final static int TOTAL_NUMBER_OF_CARDS = 36;
}

class Card
{
    private String name;
    private int value;
       
    public Card()
    {
        name = "";
        value = 0;
    }
   
    public Card(String aName, int aValue)
    {
        name = aName;
        value = aValue;
    }
   
    public int getValue()
    {
        return value;
    }
   
    public String getName()
    {
        return name;
    }
}

But it doesn't work as the way it's being ask to work like.

This is what my teacher asked me to do.

"The 99 Game"
99 is a children's game used to encourage fast addition. 99 is played with a standard deck
of playing cards. Any number can play but we will discuss three players. The rules of the
game are simple. Each player is dealt 5 cards. One card is turned face up and forms a
"pile" so that all players can see it. The players alternate putting cards on the pile and
adding the card's value to the piles total, and drawing a new card from the deck to replace
the one they discarded. The aim of the game is to continue to add cards to the pile as long
as the pile never has a total larger than 99.
Each card is worth the following;
Ace == 1
2 == 2
3 == 3
4 == reverse the
order of play
9 == 0
10 == 10
Jack == 10
Queen == -10
King == 99
For example if there were three players and a 2 were the first card on the pile and the first
player put a jack on top then the total would be 12. If the second player put a Queen on
the pile then the total would be 2 again. If the next card put on the pile by the third player
was a nine than the total would remain 2.
Play would continue with it being the first player's turn again. If the first player put a 4
on the pile, the total would remain 2 but the order of play would now reverse with it
being the third player's turn. The order of play remains reversed until another 4 card is
placed on the pile.
If one of the players puts a King on the pile the total immediately becomes 99. The only
valid cards that can be used by the other players at this point are 4, 9, Queen or King. The
first player to make the pile go over 99 loses and play continues with the other players
until only one player is left.
You are to write a Java class called "Deck" that contains at least the methods to;
"¢ Create the deck,
"¢ Shuffle the deck, and
"¢ Draw a card at a time from the deck.
You are to write a Java class called "Pile" that has methods to add a card to the pile and a
method that returns the total of the cards on the pile. Other methods are allowed if
needed.
You are to write a Java class called "Player" that contains at least the methods
"¢ "play" to draw a card from the deck and to discard a card onto the pile
"¢ "draw" to draw a card from the "Deck" class
"¢ "discard" to put a card on the "Pile" class
The constructor method for "Player" should allow you create 3 types of players;
1. A smart player that uses a defined strategy to win,
2. A naïve player that just plays by the rules, and
3. A human player where decisions are made by a human involved as one of the
players when it is their turn.
You are to write a Java driver class that:
"¢ Creates a deck of cards,
"¢ Creates 3 players, and
"¢ Runs a game of 99 between the players until a winner is found. Make sure that the
output from this driver can be easily understood by someone who wishes to
follow the game.

So, what exactily the program needs to work the game as it's being asked in the instructions? Confused
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: