Results 1 to 5 of 5

Thread: card game Rummy

  1. #1
    javafox's Avatar
    javafox is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default card game Rummy

    Im doing a project and my heads wrecked trying to get my method working properly
    in my test class I have a Array list of 7 cards
    for testing purposes its a winning hand three 1's and four 8's
    my return only says matchingCardCount 3
    matchingCardCount 3
    red codes not being read at all.
    Can anyone help thanks

    Java Code:
    public ArrayList checkForWinningValues()
        {
            sortByValue();
            int matchingCardCount = 1;
            valueCounter=matchingCardCount;
            
            // Loop through all of the card in the hand
            for(int i = 0; i<handOfCard.size(); i ++)
            {
                // Get the current card
                Card currentCard = (Card)handOfCard.get(i); 
                
                // reset the matching card count as we are now comparing to a new card
                matchingCardCount = 1;
                
                // Now we must compare all of the remaining cards in our hand 
                // to the current card 
                for(int next = i+1; next<handOfCard.size(); next ++)
                {                
                    // Get the next card
                    Card nextCard = (Card)handOfCard.get(next);
                    
                    //Compare the face values 
                    if (currentCard.getFaceValue() == nextCard.getFaceValue())
                    {
                            matchingCardCount ++;
                    }
                    // Do we have 3 matching cards? if we do add them to 
                    // the winniing hand and remove from the hand of cards
                    if ( matchingCardCount == 3)
                    {                          
                        // Add to the winning hand
                        winningCards.add((Card)handOfCard.get(next));  //3rd card                          
                        winningCards.add((Card)handOfCard.get(next - 1)); //2nd card
                        winningCards.add((Card)handOfCard.get(next - 2)); //1st card
                        
                        //remove from the hand of cards
                        handOfCard.remove(next);  //3rd card                          
                        handOfCard.remove(next - 1); //2nd card
                        handOfCard.remove(next - 2);
                        
                        System.out.println(matchingCardCount);
                    }
                    
                    //Is there another card that matches? max of 4 cards can match
                    [COLOR="Red"]if (matchingCardCount == 4)
                    {
                         winningCards.add((Card)handOfCard.get(next)); 
                         handOfCard.remove(next);
                        //Card fourthCard=(Card)handOfCard.get(next);
                        //winningCards.add(fourthCard);                    
                    }[/COLOR]
                    
                }//end for loop to see if same face value
            
                // We have three of a kind so add them to the winning hand                    
            }
            return winningCards;
        }
    :confused:.com

  2. #2
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    I personally would use a System.out.println in this method somewhere to make sure the variables are representing what they're supposed to. Some of my projects will have 100's of printlns. I always put the method name in the println also so that I can quickly see where its getting called from and it kind of gives me a step by step output of where the code is.

    I would start this method with a System.out.println("checkForWinningValues()");

    then use something like
    System.out.println("checkForWinningValues() - currentCard: "+currentCard);

    System.out.println("checkForWinningValues() - nextCard: "+nextCard);

    And put this before the red if statement
    System.out.println("checkForWinningValues() - matchingCard: "+matchingCard);

    This would let you know exactly where the code is and what is going wrong. I see you have 1 print statement, but I would use alot more to be able to trace the code better.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    four 8's
    mean first 8 got 3 matching cards
    matchingCardCount is then 3

  4. #4
    javafox's Avatar
    javafox is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    i know whats wrong it wont collect or add the last card into the winningcards array you can see it it become the current card but doesnt add to winningcard
    Im still:confused:.com
    I know for sure java isnt coffe cause I'd be awake by now the amount of time Ive spent trying to fix this ();

  5. #5
    javafox's Avatar
    javafox is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    public Boolean checkForWinning4Valuesxxx()
    {
    sortByValue();
    int matchingCardCount = 0;
    //valueCounter=matchingCardCount;
    System.out.println("checkForWinningValues()");

    // Loop through all of the card in the hand
    for(int i = 0; i<handOfCard.size(); i ++)
    {
    //add to winningCards first card
    winningCards.add((Card)handOfCard.get(i));
    System.out.println("printing the winningcards "+winningCards);
    handOfCard.remove(i);
    }
    for(int x = 0; x<winningCards.size(); x ++){
    // Get the currentcard
    Card currentCard = (Card)handOfCard.get(x);
    System.out.println("checkForWinningValues() - currentCard: "+currentCard);
    for(int y = 0; y<handOfCard.size(); y ++){
    Card nextCard = (Card)handOfCard.get(y);
    System.out.println("checkForWinningValues() - nextCard: "+nextCard);
    //Compare the face values
    if (currentCard.getFaceValue() == nextCard.getFaceValue())
    {
    matchingCardCount ++;
    //remove next card
    winningCards.add((Card)handOfCard.get(y));//next card
    System.out.println("printing the winningcards hand if 4"+winningCards);
    handOfCard.remove(y);
    }


    System.out.println("checkForWinningValues() - matchingCard up top: "+matchingCardCount+"\n");

    //Is there another card that matches? max of 4 cards can match
    if (matchingCardCount == 4)
    return true;
    }
    matchingCardCount=0;
    }
    return false;
    }

Similar Threads

  1. Can we access sim card from computer
    By rajasekharpv in forum Sun Java Wireless Toolkit
    Replies: 3
    Last Post: 09-03-2009, 03:22 PM
  2. Card Layout
    By Gilbee in forum NetBeans
    Replies: 3
    Last Post: 03-03-2009, 09:37 PM
  3. A Online Card Game
    By GonzaloP in forum Networking
    Replies: 0
    Last Post: 12-28-2008, 06:37 PM
  4. a little help needed for card drawing
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 01-09-2008, 03:40 AM
  5. Card program , need help thanks.
    By carlos123 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 07:23 AM

Posting Permissions

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