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);
   }
}

Recommended Answers

All 23 Replies

a war event takes place, no points are assigned

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

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.

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.

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?

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

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?

from code line 130 to 140

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)

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.

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++;
        }
    }    

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.

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?

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.

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);
   }
}

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...)

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

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?

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

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.

okay this is what i did.
I created int roundPoints = 1; whihc will store count of points for one round
then roundPoints++; will increase count of points to keep track

My program works. thanks but no thanks I had put in so much to do it on my own. As i was not able to solve it for so long, you could have solved it. anyways.

Glad you got it working. That was what we hoped you would do and were trying to help you to do.
We're here to help you solve the problems, not to solve them for you.

Please mark this thread as solved.

where is your solution button. please help..in the same way so i take those real long hours.....help me solve it, lets see agin how i do it :-). I dont see the button, where is it?

thanks but no thanks I had put in so much to do it on my own. As i was not able to solve it for so long, you could have solved it. anyways.

Maybe you have misunderstood what we do here at DaniWeb. We don't do people's homework. We try to help people to do it themselves. We try to advise people on how to think about their programs, and which information they need to learn next. If you solved your own problem that that's exactly what we hoped for. Yes, it took you a long time, but that was time spent learning, not time wasted. You are a better Java programmer now than you were 24 hours ago.

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.