Suggestions on figuing out Total score for card dealing program
<p>Hi all I nearly have this program done a friend of mine actually really helped me figure out how everything worked, but theres one thing i can't figure out. i really don't mean to post all this code for one of my java programs but i don't know exactly what code i should put so i'm deciding to post it all and maybe someone can give me suggestions. so the jist of this program is that two different players are dealt three cards, and a winner is chosen from those two players based on the what hand rank they were assigned to, based off of the cards they were dealt. the hand ranks are 1, 2 , 3. of course the handranks are compared and then the program checks to see who the winner is and the output in two different text areas shows the winner, the loser, and what handrank each player received. The thing i'm having trouble with though is that i need to add up all the points based on the hand ranks and output a total score for each player. I'm just lost on how to do it. Every time the user selects the deal button the total score from the previous handrank  should be summed. I would need to put the handranks in an array and then sum them up in a for loop right? also another trick to it is that the winner additionally receives one more point just for winning so i also need to incoroporate that into it. im pretty sure that would be an if statement of some sort? thanks and again sorry for posting all the code, thank you.</p>

<p>MainP3.java</p>

<pre><code>import javax.swing .*;
import java.awt .*;
import java.lang .*;


public class MainP3{
    CardFrame frame = new CardFrame();

    public MainP3(){

    frame.setTitle("Three Card Brag");
    frame.setSize(900, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);


    }
    public static void main(String[] args){
    MainP3 app = new MainP3(); 
    }


}
</code></pre>

<p>CardFrame.java</p>

<pre><code>import java.lang.*;
import javax.swing.*;               //for JFrame, JButton, JTextArea
import javax.swing.BorderFactory;   //for BorderFactory
import javax.swing.border.*;        //for CompoundBorder
import java.awt.*;                  //for GridLayout and Color
import java.util.Random;            //for randomize
import java.awt.event.*;            //for ActionListener
import java.awt.Font.*;

public class CardFrame extends JFrame{
    Card c = new Card();
    CardDeck deck = new CardDeck();

    private JLabel status;

    Player p1 = new Player();
    Player p2 = new Player();

    Card[] p1Hand = new Card[3];
    Card[] p2Hand = new Card[3];

    private JTextArea p1TextArea = new JTextArea();
    private JTextArea p2TextArea = new JTextArea();

    int player1 = 0;
    int player2 = 0;

    String playerName = "";
    String player2Name = "";

    JPanel panelA;
    JPanel panelB;
    JPanel panelC;
    JPanel bgPanel;

    ImageIcon rd = new ImageIcon(".//images/b2fv.png", "rd");

    JLabel Card1;
    JLabel Card2;
    JLabel Card3;
    JLabel pCard1;
    JLabel pCard2;
    JLabel pCard3;

    JButton deal;
    JButton winner;
    JButton help;
    JButton newGame;

    Border blackline = BorderFactory.createLineBorder(Color.black);
    TitledBorder tempTitle;

    public CardFrame(){
            IntroMess();
            playerName = JOptionPane.showInputDialog(null,"First Player Enter your Name: ", "Jim Batzer ");
            player2Name = JOptionPane.showInputDialog(null,"Second Player Enter your Name: ","John Acosta ");
            p1.setName(playerName);
            p2.setName(player2Name);
            p1.setCardsInHandForGame(3);
            p2.setCardsInHandForGame(3);
            InitComponents();

            }

            public void IntroMess(){
                JOptionPane.showMessageDialog(null, "CIS 2235 \n John Acosta"
                + "\n 3 Card Brag \n\n"
                + "");
            }

            public void InitComponents(){
            panelA = new JPanel(new GridLayout(2,3,1,1));
            panelB = new JPanel(new GridLayout(2,1,10,10));
            panelC = new JPanel(new GridLayout(2,2,10,10));
            bgPanel = new JPanel(new GridLayout(1,3,1,1));

            Card1 = new JLabel();
            Card2 = new JLabel();
            Card3 = new JLabel();
            pCard1 = new JLabel();
            pCard2 = new JLabel();
            pCard3 = new JLabel();

            resetImages();

            p1TextArea = new JTextArea(playerName);
            p2TextArea = new JTextArea(player2Name);

            deal = new JButton(" Deal");
            winner = new JButton(" Show Winner");
            help = new JButton(" Help!");
            newGame = new JButton(" New Game");

            deal.addActionListener(new dealListener());
            winner.addActionListener(new winnerListener());
            help.addActionListener(new helpListener());
            newGame.addActionListener(new newGameListener());

            tempTitle = BorderFactory.createTitledBorder(blackline, "Cards");
            tempTitle.setTitleJustification(TitledBorder.CENTER);
            panelA.setBorder(tempTitle);

            tempTitle = BorderFactory.createTitledBorder(blackline, "Score");
            tempTitle.setTitleJustification(TitledBorder.CENTER);
            panelB.setBorder(tempTitle);

            tempTitle = BorderFactory.createTitledBorder(blackline, "");
            tempTitle.setTitleJustification(TitledBorder.CENTER);
            panelC.setBorder(tempTitle);

            panelA.setBackground(Color.CYAN);     //images panel is initially yellow
            bgPanel.setBackground(Color.BLUE);

            setLayout(new GridLayout());

            add(bgPanel);
            bgPanel.add(panelA);
            bgPanel.add(panelB);
            bgPanel.add(panelC);

            panelA.add(Card1);
            panelA.add(Card2);
            panelA.add(Card3);
            panelA.add(pCard1);
            panelA.add(pCard2);
            panelA.add(pCard3);

            panelB.add(p1TextArea);
            panelB.add(p2TextArea);

            panelC.add(deal);
            panelC.add(winner);
            panelC.add(help);
            panelC.add(newGame);
            }

            class dealListener implements ActionListener{
         public void actionPerformed( ActionEvent e ){
             for(int i=0; i &lt;3; i++)
             {
                c = deck.getNextCard();
                p1Hand[i] = c;
                p1.setCard(c);
                c = deck.getNextCard();
                p2Hand[i] = c;
                p2.setCard(c);
            }
            Card1.setIcon(p1Hand[0].getImage());
            Card2.setIcon(p1Hand[1].getImage());
            Card3.setIcon(p1Hand[2].getImage());
            pCard1.setIcon(p2Hand[0].getImage());
            pCard2.setIcon(p2Hand[1].getImage());
            pCard3.setIcon(p2Hand[2].getImage());
        }
    }

        class winnerListener implements ActionListener{
         public void actionPerformed( ActionEvent e ){
            updateScore();

        }
    }
            class helpListener implements ActionListener{
         public void actionPerformed( ActionEvent e ){
             JOptionPane.showMessageDialog(null, "Help Menu"
        + "\n You will Be Dealt 3 Cards  \n"
        + "\n The scoring system works like so: "
        + "\n Three 3's OR one or more Aces Rank at 3 pts. \n"
        + "\n 2 or 3 of the same card or suit Rank at 2 pts.\n"
        + "\n King, Queen, or Jack Rank at 1 pts.\n"
        + "\n Any other cards Rank at 0 pts.\n "
        + "\n Coloring System in Text Area"
        + "\n Bronze means you won with a Hand Rank of 1"
        + "\n Silver means you won with a Hand Rank of 2"
        + "\n Gold means you won with a Hand Rank of 3 ");
        }
    }

    class newGameListener implements ActionListener{
         public void actionPerformed( ActionEvent e ){
            deck.shuffle();
            resetImages();
        }
    }

    private void resetImages() {
        //reset counts
        player1 = 0;
        player2 = 0;

        //set labels
        Card1.setIcon(rd);
        Card2.setIcon(rd);
        Card3.setIcon(rd);
        pCard1.setIcon(rd);
        pCard2.setIcon(rd);
        pCard3.setIcon(rd);
    }
    private void updateScore() {
    //cardTableP is Players text area pOne
    //cardTableM is My text area pTwo
p1TextArea.setText(playerName);
p2TextArea.setText(player2Name);
        if( p1.getHandRank() &gt; p2.getHandRank() )
        {
            switch( p1.getHandRank() )
            {
                case 1:         //bronze
                                p1TextArea.setBackground(new Color(205, 127, 50));
                                break;
                case 2:         //silver
                                p1TextArea.setBackground(new Color(201, 192, 187));
                                break;
                case 3:         //Gold
                                p1TextArea.setBackground(new Color(212, 175, 55));
                                break;           
            }
        Font font = new Font("Comic Sans MS", Font.ITALIC, 24);
        p1TextArea.setFont(font);
            p1TextArea.setForeground(Color.GREEN);
            p1TextArea.append(" Winner!! "+"\n"+p1.getNameHandRankAndPoints());
            p2TextArea.setForeground(Color.RED);
            p2TextArea.append(" Loser!! " +"\n"+p2.getNameHandRankAndPoints());
        }
        else if( p2.getHandRank() &gt; p1.getHandRank() )
        {
            switch( p2.getHandRank() )
            {
                    case 1:     p2TextArea.setBackground(new Color(205, 127, 50));
                                break;
                    case 2:     p2TextArea.setBackground(new Color(201, 192, 187));
                                break;
                    case 3:     p2TextArea.setBackground(new Color(212, 175, 55));
                                break;           
            } 
            Font font = new Font("Comic Sans MS", Font.ITALIC, 24);
            p2TextArea.setFont(font);
            p2TextArea.setForeground(Color.GREEN);
            p2TextArea.append(" Winner!! "+"\n"+p2.getNameHandRankAndPoints());
            p1TextArea.setForeground(Color.RED);
            p1TextArea.append(" Loser!! "+"\n"+p1.getNameHandRankAndPoints());
        }
    else if( p2.getHandRank() == p1.getHandRank() )
        {
            p1TextArea.setBackground(Color.WHITE);
            p2TextArea.setBackground(Color.WHITE);
            Font font = new Font("Verdana", Font.BOLD, 12);
            p2TextArea.setFont(font);
            p2TextArea.setForeground(Color.blue);
            p2TextArea.append(" Draw "+"\n"+p2.getNameHandRankAndPoints());
            p1TextArea.setFont(font);
            p1TextArea.setForeground(Color.blue);
            p1TextArea.append(" Draw "+"\n"+p1.getNameHandRankAndPoints());
        }

    }

}
</code></pre>

<p>Player.java</p>

<pre><code>public class Player {

 private String name="";
 private int cardsInHandForGame; 
 private int cardsInHand = 0;    
 private int totalGamePoints;    
 private Card[] hand = new Card[3];

 private int handRank;   


 public Player() {
        totalGamePoints = 0;
        handRank = 0;

        for(int i = 0; i &lt; cardsInHandForGame; ++i) 
        {
            hand[i] = new Card();
        }
    }  

    //Name
    public void setName(String n)
    { 
    name = n;   
    }
    public String getName( )
    { 
    return name;   
    }

    public void setCardsInHandForGame(int numberOfCards) 
    {
        cardsInHandForGame = numberOfCards;
    }

    //Hand Rank
    public int getHandRank() 
    { 
    return handRank; 
    }

    //Total Game Points
    public int getTotalPoints() 
    { 
    return totalGamePoints; 
    }


    public String toString() {
        String s ="";
        ////write this===============================
        return s;
    }

//returns the player's name, current hand rank, and total pts
    public String getNameHandRankAndPoints() {

        String s = "your hand rank is: "+ handRank;

        return s;
    }

    //setCard Is called when a card is dealt to the player.
    //The Frame will call this, passing a Card c,
    //setting it into the player's hand (at the current
    //cardsInHand position), increment the number of cards
    //in the player's hand (cardsInHand++).
    //When the player has the maximum number of cards for the game,
    //the player determines the hand rank and assigns points to his score.

    public void setCard(Card c) {   //put the card in the player hand
        hand[cardsInHand] = c;      //increment hand index
        cardsInHand++;
        if(cardsInHand == (cardsInHandForGame)) {
            cardsInHand = 0;
            determineHandRank();
        }
    }

    //performs the logic needed to determine the winning hand level
    //assigns value (0,1,2,3) to handRank.
    //winning hand points are assigned to totalGamePoints  
    public int determineHandRank( ) {      
        //you write this, assign value to handRank, add to points
        //handRank;     holds the current hand's winning rank value
        //0 = not a winning hand
        //1 = lowest level, J, Q, K
        //2 = mid-level, 2 or 3 of any suit or rank
        //3 = high-level, 3-3's or an Ace
        //If 3 3's or an Ace in Hand[0], Hand[1], or Hand[2], then Rank = 3
        if((hand[0].getRank()== hand[1].getRank() &amp;&amp; hand[1].getRank() == hand[2].getRank()) || (hand[0].getRank()== 14 || hand[1].getRank() == 14 || hand[2].getRank() == 14 ))
        {
            handRank = 3;
        }
        else if((hand[0].getRank() == hand[1].getRank()) || (hand[0].getRank() == hand[2].getRank()) || (hand[1].getRank() == hand[2].getRank()))
    {
            handRank = 2;
        }
        else if((hand[0].getRank() &gt;10 &amp;&amp; hand[0].getRank() &lt; 14) || (hand[1].getRank() &gt;10 &amp;&amp; hand[1].getRank() &lt; 14 )|| (hand[2].getRank() &gt;10 &amp;&amp; hand[2].getRank() &lt; 14 ))
        {
            handRank = 1;
        }
        else{ handRank = 0;}
        return handRank;

        //If 2 or 3 and suit or rank in Hand[0], Hand[1], or Hand[2], then Rank = 2
        //If 1 Jack Queen or King in Hand[0], Hand[1], or Hand[2], then Rank = 1
        //else = 0
    }

    //If the player has won the hand, give'em an extra point
    //Note: this will be called from the Frame!
    public void creditGameWin( ) { totalGamePoints++; }

}
</code></pre>

<p>CardDeck.java </p>

<pre><code>//CardDeck.java
//description:
//The card deck class's job is to represent a deck 
//of playing cards

public class CardDeck{
public static final int NUMBER_OF_CARDS = 52;

private int[] deck = new int[NUMBER_OF_CARDS];
private static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
private int[] ranks = {14,2,3,4,5,6,7,8,9,10,11,12,13};
private Card[] cards = new Card[NUMBER_OF_CARDS];

private int topCard = 0;

//Constructor
public CardDeck(){
//fill the deck with sequential numbers
for(int i = 0; i &lt; deck.length; i++){
    deck[i] = i;
    }

    //fill the card array
    fillCardArray();

    //shuffle the deck
    shuffle();
}

public Card getNextCard(){
    //check for end of deck
        topCard++;
        if(topCard==53){
        topCard = 1;
        shuffle();
        }


    return cards[deck[topCard-1]];

}
private void fillCardArray(){

    for(int i = 0; i &lt; cards.length; i++){
    cards[i] = new Card();

    cards[i].setRank(ranks[i%13]);
    cards[i].setSuit(suits[i/13]);
    cards[i].setImage(".\\images\\" + (i+1) + ".png");
    //To set the images.

    }
}
public void shuffle(){
//shuffle the deck
 for (int i = 0; i &lt; deck.length; i++) {
  // Generate an index randomly
  int index = (int)(Math.random() * deck.length);
  int temp = deck[i];
  deck[i] = deck[index]; 
  deck[index] = temp;

}

  }
}
</code></pre>

<p>Card.java</p>

<pre><code>import javax.swing.ImageIcon;

public class Card{

public int rank;  //2-14 - 14:ace
private String suit; //Spades, Hearts, Diamonds, Clubs
private ImageIcon image; //icon is index + 1
private String[] rankString = 
{"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

public Card(){
}
public int getRank(){
    return rank;
}
public void setRank(int r){
    rank = r;  
}
public String getsuit(){
    return suit;
}
public void setSuit(String s){
    suit = s;
}
public ImageIcon getImage(){
    return image;
}
public void setImage(String filename){
    image = new ImageIcon(filename);
}
public  String toString(){

String s = ("I am the " + rankString[rank-2] + " of " + suit + "(rank): " + rank);

    return  s;

}
}
</code></pre>
