We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,031 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Unable to award double points in War Card game

I need to fix the code. Once a war event takes place, no points are assigned however in the next deal whoever wins (be it the player or computer) should get double points. One point for the war and the second point for the next deal that was won as well.

import java.util.Scanner;

public class Game {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        War war = new War();
        while(count == 1){
            System.out.println("Score: "+war.playerScore+"-"+war.compScore);
            war.playerCard();
            Card playerCard = new Card(war.playerNum, war.playerFace);
            war.compCard();
            Card compCard = new Card(war.compNum, war.compFace);
            System.out.println("Player: "+playerCard.toString());
            System.out.println("Computer: "+compCard.toString());
            System.out.println(war.compare());
            System.out.println("Play next round? 1 - yes, 0 - no");
            count=sc.nextInt();
        }
    }
}
public class Card {

    private String _suit;
    private int _value;
    private String _face;

    public Card(int value, int suit) {
        _value = value;

        switch(value) {
            case 14:
                _face = "Ace";
                break;
            case 11:
                _face = "Jack";
                break;
            case 12:
                _face = "Queen";
                break;
            case 13:
                _face = "King";
                break;
            default:
                _face = String.valueOf(value);
        }

        switch(suit){
            case 0:
                _suit="spades";break;
            case 1:
                _suit="hearts";break;
            case 2:
                _suit="diamonds";break;
            case 3:
                _suit="clubs";break;
        }
    }

    public String getSuit() {
        return _suit;
    }

    public int getValue() {
        return _value;
    }

    public String getFace() {
        return _face;
    }

    public String toString(){
        return this._face+" of "+this._suit;
    }

} // Card

public class Deck2 {

    private final int DECKSIZE = 52;

    private Card[] deck = new Card[DECKSIZE];
    private int _index;

    public Deck2() {

    } // constructor

    public Card dealCard() {
        return deck[_index++];
    }

    public boolean isEmpty() {
        return _index == DECKSIZE;
    }

    public int getDeckSize() {
        return DECKSIZE;
    }

    public void shuffle() {
        for (int i = 0; i < 10000; i++) {
            int card1 = (int) (Math.random() * DECKSIZE);
            int card2 = (int) (Math.random() * DECKSIZE);
            Card temp;
            temp = deck[card1];
            deck[card1] = deck[card2];
            deck[card2] = temp;

        } // for
    } // shuffle
} // Deck

import java.util.Random;

public class War
{
    public final int END_LIMIT = 8;
    public static int playerScore = 0;
    public static int compScore = 0;
    public int playerNum = 0;
    public int playerFace = 0;
    public int compNum = 0;
    public int compFace = 0;
    private Deck2 warDeck;
    private Random r = new Random();

    public String compare()
    {
        if(playerNum > compNum){
            playerScore++;
        }else if(compNum > playerNum){
            compScore++;
        }else{
            return "war! and no points awarded so far";
        }
        return "Score: "+this.playerScore+" - "+this.compScore;
    }

   public void playerCard(){
       playerNum=r.nextInt(12)+1;
       if(playerNum==1){
           playerNum=14;
       }
       playerFace=r.nextInt(3);
   }

   public void compCard(){
       compNum=r.nextInt(13)+1;
       if(compNum==1){
           compNum=14;
       }
       compFace=r.nextInt(4);
   }
}
3
Contributors
23
Replies
18 Hours
Discussion Span
7 Months Ago
Last Updated
24
Views
Question
Answered
popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

a war event takes place, no points are assigned

Where are the events detected? Would that be the place to assign points?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This what is happening:-
Play next round? 1 - yes, 0 - no
1
Score: 7-6
Player: 7 of spades
Computer: 6 of hearts
Score: 8 - 6
Play next round? 1 - yes, 0 - no
1
Score: 8-6
Player: Jack of diamonds
Computer: Jack of clubs
war! and no points awarded so far
Play next round? 1 - yes, 0 - no
1
Score: 8-6
Player: Queen of diamonds
Computer: 3 of diamonds
Score: 9 - 6
Play next round? 1 - yes, 0 - no

As you see the player won the next event after the war however only one point was awarded when two should be. I am unable to figure it out.

popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

As you see the player

Sorry, I don't see where the output is wrong.

Can you explain where the problem is shown in the print out you posted?
And post an example of what the output should be.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Score: 7-6
Player: 7 of spades
Computer: 6 of hearts
Score: 8 - 6
Play next round? 1 - yes, 0 - no
1
Score: 8-6
Player: Jack of diamonds
Computer: Jack of clubs
war! and no points awarded so far
Play next round? 1 - yes, 0 - no
1
Score: 8-6
Player: Queen of diamonds
Computer: 3 of diamonds
Score: 10 - 6
Play next round? 1 - yes, 0 - no
1
Score: 10-6
Player: Ace of spades
Computer: 8 of clubs
Score: 11 - 6
Play next round? 1 - yes, 0 - no

Actually the program is correct, but what i need is what i am not able to figure out where the change should be. I feel there need to be a modification in the "war" class that to in the if loop section where probably i need to put a condition. Do you understand what i want to achieve?

popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

If you notice in the above output, where is shows 10-6 as the score while in the first output log that i pasted, it showed 9-6

popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Actually the program is correct, but what i need is what i am not able to figure out where the change should be.

That doesn't make sense to me. If the program is correct, why does it need to be changed?

Do you understand what i want to achieve?

No, can you add comments to the current output showing what it should be?
For example, current output on left, desired output to right of <<<<<:
Score: 10 - 6 <<<<<<<<<<< This should be 22-12

Where in the code does it detect the event so it can change the score as you want?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

from code line 130 to 140

popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

When you detect a war (line 137) you could set a boolean to mean "previous game resulted in war". Then next time through you can check that boolean to whether to score double points (and reset the boolean)

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

What values of what varibles tells the program that an event has happened?

When an event happens, where else in the code should the fact that there was an event change what the code does? Does there need to be a way to remember the event happened so that other code can execute differently? Some where the value of a variable needs to be set to remember the event.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

okay so this is what i try to do but i get return missing error

public int compare(){
    if (playerNum > compNum){
    playerScore += 1 + warCards;
    warCards = 0;
}
else if (compNum > playerNum){
    compScore += 1 + warCards;
    warCards = 0;
}
else{
    return warCards++;
        }
    }    
popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Where are the return statements for the case where the first or second if statement is true?
There is ony one return statement for when neither of the if statements are true.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Look at yor logic... unless both if tests are false you never execute that return. Keep it simple and just set your variables in the if/else tests and have a single return as the last line of the method.
ps I don't see why you are returning warCards anyway - why not just treat that variable like playerScore or compScore?

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

Thank you sir. I was really stupid. I saw my friends program but not completely so my mind was all the time working on what i saw half and was not thinking at all. Thank you for making to think.....but i belive, it could be shorter. I will repaste it.

popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
import java.util.Random;

public class War
{
    public final int END_LIMIT = 8;
    public static int playerScore = 0;
    public static int compScore = 0;
    public int playerNum = 0;
    public int playerFace = 0;
    public int compNum = 0;
    public int compFace = 0;
    private Deck2 warDeck;
    private Random r = new Random();

    public String compare()
    {
        if(playerNum > compNum){
            playerScore++;
        }else if(compNum > playerNum){
            compScore++;
        }else
        if(playerNum==compNum){
            return "war!";
        }
        if(playerNum > compNum){
            ++playerScore;
        }else if(compNum > playerNum){
            ++compScore;
        }        
        return "Score: "+this.playerScore+" - "+this.compScore;
    }
   public void playerCard(){
       playerNum=r.nextInt(12)+1;
       if(playerNum==1){
           playerNum=14;
       }
       playerFace=r.nextInt(3);
   }

   public void compCard(){
       compNum=r.nextInt(13)+1;
       if(compNum==1){
           compNum=14;
       }
       compFace=r.nextInt(4);
   }
}
popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Latest version has completely lost all the stuff about remembering that the previous game as a war.
(I'm finishing now for this evening, someone else will probably step in...)

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

Opps, i did not go beyound the war, it is now doubling everything depending on who wins

popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

In the compare() method there seems to be two sets of identical if statements. Is that intended?
That wouldn't be a normal way to write a method.

How is the program working with the new code?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

You are going and am stuck.....please help me finish the homework. I know where the problem is..in the if loop in the war class but not sure how to go about it. Solve it for me James

popeyemissy
Newbie Poster
12 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Can you explain what the "war event" is and what determines it?
When a "war event" is detected you need a way to remember it so it can be used later in the program to change the way scores are changed.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1313 seconds using 2.86MB