Java Card Game Help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Java Card Game Help

  1. #1
    Join Date
    Feb 2009
    Posts
    1

    Java Card Game Help

    Ok here is the game:

    You play with two decks.

    One deck of 52 is the Withdraw pile.

    The other deck is split in half between the two player (each receiving 26 cards).

    Player one draws a card from his deck of 26 and if the card's value is greater than 7, he draws that many cards from the withdraw pile. (Face cards count as 10 cards).

    Example: Draw a Jack, draw 10 cards from the withdraw pile.

    If the card is less than 7, you simply discard it.

    Then the other player goes, and so on.

    The game ends when the withdraw pile is gone.

    The players both add up how many cards they have and the person with the least wins.

    Simple enough.

    Here is the code: (It is sort of pseudocode around the .add and .remove parts (you will see)- this is where I really need help. I'm looking for a sort of cut and paste feature, where I will "cut" the cards from the withdraw pile and "paste" them back to the player deck.

    By the way, I already have card/deck/suit classes so that is where some of the features like .shuffle and .get are from. If you need to see any of those let me know...

    thanks.

    ------------------------------


    import java.util.*;

    public class Halfandhalf {

    public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);
    Deck player = new Deck();

    Deck withdraw = new Deck();
    //System.out.print(player); //tests the deck

    ArrayList<Card> playerOne = new ArrayList<Card>(); //Your hand
    ArrayList<Card> computer = new ArrayList<Card>(); //computer's hand
    ArrayList<Card> discard = new ArrayList<Card>(); //discard pile (empty)- fill with discarded cards (if < 7)

    withdraw.shuffle();//shuffle
    player.shuffle();

    for(int x = 0; x < 26; x++){
    playerOne.add(player.deal()); //deals 26 cards to each player
    computer.add(player.deal());
    }



    while (withdraw.size()>0){
    System.out.println("You draw");
    System.out.println(playerOne.get(0));
    if(playerOne.get(0).getRank()>7){ //if card value is greater than 7, draw that many cards from the withdraw pile into your hand

    if(playerOne.get(0).getRank()==8){ //fix this later
    withdraw.remove(8); //remove 8 cards from withdraw
    playerOne.add(8); //add 8 to player's hand
    }

    if(playerOne.get(0).getRank()==9){
    withdraw.remove(9);
    playerOne.add(9);
    }

    if(playerOne.get(0).getRank()==10){
    withdraw.remove(10);
    playerOne.add(10);
    }

    if(playerOne.get(0).getRank()==11 || 12 || 13) //king,queen,jack count as 10 cards
    withdraw.remove(10);
    playerOne.add(10);

    }

    else if(playerOne.get(0).getRank()<7){
    player.remove(0); //move the card to the discard arraylist
    discard.add(0);
    }


    System.out.println("Opponent draws");
    System.out.println(computer.get(0)); //basically the same thing for the opponent
    if(computer.get(0).getRank()>7){

    if(computer.get(0).getRank()==8){ //fix this later
    withdraw.remove(8); //remove 8 cards from withdraw
    computer.add(8); //add 8 to player's hand
    }

    if(computer.get(0).getRank()==9){
    withdraw.remove(9);
    computer.add(9);
    }

    if(computer.get(0).getRank()==10){
    withdraw.remove(10);
    computer.add(10);
    }

    if(computer.get(0).getRank()==11 || 12 || 13) //king,queen,jack count as 10 cards
    withdraw.remove(10);
    computer.add(10);

    }

    else if(computer.get(0).getRank()<7){ //Aces Count as 1
    computer.remove(0); //move the card to the discard arraylist
    discard.add(0);
    }



    }


    }

    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Card Game Help

    Rather than using an ArrayList to store the hands and withdraw pile you could use Queue type data structures.

    For the withdraw pile a Stack (LIFO queue) would be ideal.

    For the hands it depends on whether the cards from the withdraw pile go to the bottom or top of the hand ie is it a FIFO or LIFO queue that is required. For a FIFO queue you can use a LinkedList, which has methods to allow it to work as a FIFO queue (ie add and poll).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center