Hi, I am writing a game. Three card brag. The rules of the game are
1.Each player is dealt three cards.
2.The hand is then assigned a rank based on the cards recieved
3. The hand Ranks are 0,1,2,3
4. the hand ranks are then compared and the highest hand rank from that game is the winner, other wise it's a draw.

I'm almost finished with this game, but im having trouble figuring out how to tally each players total game points. the game points are just the values from the hand ranks added to each other from each game. the score should automatically update once the user clicks on the button to start a new game.another trick to it is that the winner of the game automatically gets a point just for winning.

so an example of what im trying to do.
first game:
player 1 has a hand rank of 3//winner
player 1 Total Score:0
player 2 has a hand rank of 2
player 2 Total score: 0

second game:
player 1 has a hand rank of 2//winner
player 1 Total Score:7
player 2 has a hand rank of 1
player 2 Total Score :2

how would i go about doing this, i have to catch the value of the handRank and add it to the last value of the handRank everytime but how would i do this? through an array?? with a for loop running? I don't know, I'm lost. Any suggestions would be appreciated. here is my player.java code for you guys. if you need to see the rest of my code. let me know.

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 < 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
                /*+"your total score is: " + totalGamePoints*/;
                //this is where i should output the score!!
        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() && 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() >10 && hand[0].getRank() < 14) || (hand[1].getRank() >10 && hand[1].getRank() < 14 )|| (hand[2].getRank() >10 && hand[2].getRank() < 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++; 
    }
}

have to catch the value of the handRank and add it to the last value of the handRank

Are you asking how to accumulate the values of handRank across several games?
Define a variable to hold the total and add the current value to it at the end of each game.

yes that's exactly what im asking! I have totalGamePoints defined already as my variable. I have no problem in getting the current value in hand rank! that's easy, the problem i guess im having is storing the last number so i can then sum it to the current value and so on and so forth. how exactly would i go on about doing that?

problem i guess im having is storing the last number

Can you explain why saving a value in a variable is a problem?
lastValue = theValToSave; // save the last number
Is it a question of when to do it?

I'm sorry i misworded that there is no problem in saving a value in a variable, i was just referring to the whole problem i'm dealing with in figuring out the sum. i just can't seem to get it to work correctly:

so what your telling me to do is this? and correct me if im wrong(which im pretty sure i am).

private int lastValue;//declaring lastValue

then store the value that i want saved which is the Hand Rank

lastValue = handRank;

then to output:

> public String getNameHandRankAndPoints() {
> String s = "your hand rank is: "+ handRank
>            +"\n your total score is "+(lastValue+handRank);///???????????
> //this is where i should output the score!!
> return s;
> }

it is a question of when to do it and where exactly to do it.

it is a question of when to do it and where exactly to do it.

Yes, it usually is.

hahaha thanks dude.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.