質問

includes a pile of cards in a player’s hand and two actions: TAKE and DISCARD. TAKE puts a card on the top of a player’s pile of cards when that player receives a card from a dealer. DISCARD removes a card from the top of a player’s pile of cards when a player plays it against another player in the game. Each player receives 16 cards from the dealer at the beginning of a game........

I tried my code like this which gives me nothing

public class play {

    int noOfCards = 16;

    static void TAKE(Stack st, int a) {
        st.push(new Integer(a));
    }

    static void DISCARD(Stack st) {
        Integer a = (Integer) st.pop();
    }

    public static void main(String args[]) {
        for(int i=0; i<=16; i++) {     
            Stack st = new Stack();
            st.take();
            st.discard();
        }
    }
}

I am new to this concepts ....give me a path to solve this question

役に立ちましたか?

解決

I'm sorry but I didn't fully understand the whole process you're trying to do. But I still gave it a shot.

public class Play 
{

    private static int noOfCards = 16;
    private static Stack<Integer> player1 = new Stack<Integer>();
    private static Stack<Integer> player2 = new Stack<Integer>();

    static void takeCard(Stack<Integer> st,int cardValue){
        st.push(cardValue);
    }

    static int discardCard(Stack<Integer> st){
        return st.pop();
    }

    static int getRandomValue(int min, int max){
        Random r = new Random();
        return r.nextInt((max - min) + 1) + min;
    }

    //Filled card pile with random values from 1-13
    public static void main(String[] args)
    {
        for(int i = 0 ; i < noOfCards; i++){
            takeCard(player1,getRandomValue(1,13));
            takeCard(player2,getRandomValue(1,13));
        }

        //player 1 takes a card!
        //Size of player1's pile before and after taking card
        System.out.println("Before:" + player1.size());
        takeCard(player1, getRandomValue(1, 13));
        System.out.println("After" + player1.size());
        //player 2 discards a card and outputs the card's value
        System.out.println("Player 2 discards: " + discardCard(player2));
    }
}

他のヒント

Not sure what you want it to do. Are you just trying to implement TAKE and DISCARD? Despite not knowing exactly what you're trying to do, there are lots of issues with your code.

One immediate issue is the fact that your Stack initialization is inside your for loop meaning that every time you execute that loop, you create a brand new Stack, which will be empty. So, essentially, your main method does this:

  1. Create a new Stack (it's empty)
  2. Add a card to the stack
  3. Remove a card from the stack
  4. Repeat steps 1-3 16 times

Another issue is that Java is case sensitive so calling st.take() doesn't match up with TAKE().

Yet another issue is that st.take() is like saying "Call the 'take()' method on my instance of Stack". Stack doesn't define a method called take() - it has methods like push() and pop() (look here: http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html). Your take and discard methods are on the class Play, so you'd want to invoke them like this:

Play.TAKE(st, 3);
Play.DISCARD(st);

Another issue: when you try to invoke keep or discard, you don't send any parameters, but you've defined these methods to take parameters. See above.

Is discard supposed to return the value of the card being discarded? You retrieve that value and store it into the local variable "a" and then you don't do anything with it. If you're not going to return it, you don't need to create "a". If you are going to return it, your return type shouldn't be void.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top