Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6852577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:23:17+00:00 2026-05-27T01:23:17+00:00

So I am writing a class that plays the card game war. I have

  • 0

So I am writing a class that plays the card game war. I have already made separate classes for the hand, the deck, and the different methods that go along with these types such as shuffling, dealing, and dropping the cards.

/*Using the Card, Deck, and Hand classes from the previous lab, write a main program          that will play games of war.  The program should

1) Inatantiate a Deck and two Hands

2) Read in n, the number of games of war to play

3) n times, 
  a) shuffle and deal the deck 
  b) play a game of war, counting the number of turns, the number of wars, and the   number of double wars
4) After all n games have been played, print the average number of turns, average number of wars, and the average number of double wars.

The rules for the game can be found here:
 http://www.pagat.com/war/war.html

If a player runs out of cards during a war, use option 1 on that site.

*/



import java.util.Scanner;

public class War {
public static void main (String args [])
{

    //Instantiate Deck and two hands
    deck Pile = new deck();
    hand Player1 = new hand();
    hand Player2 = new hand();
    Scanner kb = new Scanner (System.in);
    int n, turns = 0, wars = 0, dubwars = 0;
    System.out.println("Please enter the number of times you would like the game   'war' to be played:");
    n = kb.nextInt();
    for (int i = 0; i<n; i++)
    {
        //Game playing code goes here
        Pile.shuffle();
        Pile.deal(Player1, Player2);

        while(Player1.size() != 0 && Player2.size() !=0)
        {

            Card Card1 = Player1.DropCard();
            Card Card2 = Player2.DropCard();
            Card P1WarCard = Player1.WarCard(1);//Cards Used in the   First War
            Card P2WarCard = Player2.WarCard(1);
            //if statement saying if the player does not have two cards then exit the program
            //Cards Used in the Second War
            Card P1DubWarCard = Player1.WarCard(2);  
            Card P2DubWarCard = Player2.WarCard(2);



        //If player 1 has a higher rank, then...
        if (Card1.rank > Card2.rank)
        {
            //Player 1 takes both cards
            Player1.add(Card1);
            Player1.add(Card2);
            turns ++;
        }
        else if (Card1.rank < Card2.rank)
        {
            //Player 2 takes both cards
            Player2.add(Card2);
            Player2.add(Card1);
            turns++;
        }
        else 
        {
            if (Player1.size() <3 || Player2.size() <3)
            {
                System.exit(0);
            }
            //War
            wars ++;
            turns++;
        //  while (Player1.size() >=2 && Player2.size() >=2)
            {
            if (P1WarCard.rank > P2WarCard.rank)
            {
                //Player1.add(Card1);
                Player1.add(Card2);
                //Player1.add(P1WarCard);
                Player1.add(P2WarCard);
                //Player 1 takes 4 cards
            }
            else if (P1WarCard.rank < P2WarCard.rank)
            {
                //Player2.add(Card2);
                Player2.add(Card1);
                Player2.add(P1WarCard);
                //Player2.add(P2WarCard);
                //Player 2 takes 4 cards
            }
            else
            {
                if (Player1.size() <3 || Player2.size() <3)
                {
                    System.exit(0);
                }
                //Dubwar
                dubwars++;
                turns++;
                //while (Player1.size() >=3 && Player2.size() >=3)
                {


                if (P1DubWarCard.rank > P2DubWarCard.rank)
                {
                    //Player1.add(Card1);
                    Player1.add(Card2);
                    //Player1.add(P1WarCard);
                    Player1.add(P2WarCard);
                    //Player1.add(P1DubWarCard);
                    Player1.add(P2DubWarCard);
                    //Player 1 takes 6 cards
                }
                else if (P1DubWarCard.rank < P2DubWarCard.rank)
                {
                    //Player2.add(Card2);
                    Player2.add(Card1);
                    Player2.add(P1WarCard);
                    //Player2.add(P2WarCard);
                    Player2.add(P1DubWarCard);
                    //Player2.add(P2DubWarCard);
                    //Player 2 takes 6 cards
                }
                else
                {
                    System.out.println("NUCLEAR WAR");
                    break;
                }
            }

        }
    }
}
        }
    }



System.out.println("The average number of turns was " +turns/n);
System.out.println("The average number of wars was " +wars/n);
System.out.println("The average number of double wars was " +dubwars/n);

}



}

My question is how can I prevent entry into a double war if there are not enough cards to complete the double war? There must be at least three cards in the deck at that time.

Also the war card method simply returns the card at that position in the deck.

This is my first time posting so I apologize if I did something wrong.

Thanks

Chris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:23:18+00:00Added an answer on May 27, 2026 at 1:23 am

    You can always use a boolean to determine whether the game is running or not, and then check at the start of your while loop as to whether it should still be set to true, for example:

    boolean isPlaying = true;
    
    while (isPlaying) {
      if (numberOfCardsLeft <= cardsNeededToContinue) {
         isPlaying = false;
      }
      //rest of logic here
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing several class templates that contain nested iterator classes, for which
I have a Runnable class that I'm writing. Inside of it, I have two
I am writing a card game in an attempt to learn Silverlight. I have
I'm writing an AS3/Flex4 game engine, and I want to have a base class
I am writing a class that I know that needs a lock, because the
I'm writing a class that returns both a DataTable and a IEnumerable. I cannot
I'm writing a class that extends a UIScrollView to display a large tiled image,
I am writing a class that inherits from IHttpHandler for script and css combining.
I am writing a class that inherits from IHttpHandler for script and css combining.
I apologize if this is a duplicate. I am writing a class that needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.