Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help making war card game

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help making war card game

    Alright, so I'm making the card game war in java and for some reason when b>d and b<d it's not adding and removing the elements from the arrays. I know I have 52 cards in each deck, I'll fix that.

    Also, any tips on a simpler way to do things when b==d? (tie between cards) Thanks in advance, this has been bugging me

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
    import java.util.Random;

    public class war

    {

    public static void printYourCard(ArrayList a, int b)
    {
    if(a.get(b).equals(11))
    System.out.println("Jack");
    else if(a.get(b).equals(12))
    System.out.println("Queen");
    else if(a.get(b).equals(13))
    System.out.println("King");
    else if(a.get(b).equals(14))
    System.out.println("Ace");
    else
    System.out.println(a.get(b));
    }
    public static void printOppCard(ArrayList a, int b)
    {
    if(a.get(b).equals(11))
    System.out.println("Jack");
    else if(a.get(b).equals(12))
    System.out.println("Queen");
    else if(a.get(b).equals(13))
    System.out.println("King");
    else if(a.get(b).equals(14))
    System.out.println("Ace");
    else
    System.out.println(a.get(b));
    }



    public static void main(String args[])
    {

    Scanner cards = null;

    try
    {
    // Create a scanner to read the file, file name is parameter
    cards = new Scanner (new File("C:\\Users\\Phil\\Desktop\\cards.txt"));
    }
    catch (FileNotFoundException e)
    {
    System.out.println ("File not found!");
    // Stop program if no file found
    System.exit (0);
    }

    Scanner cards2 = null;

    try
    {
    // Create a scanner to read the file, file name is parameter
    cards = new Scanner (new File("C:\\Users\\Phil\\Desktop\\cards2.txt"));
    }
    catch (FileNotFoundException e)
    {
    System.out.println ("File not found!");
    // Stop program if no file found
    System.exit (0);
    }





    ArrayList oppDeck = new ArrayList();
    ArrayList yourDeck = new ArrayList();
    int x = 0;

    while(cards.hasNext())
    {
    /*oppDeck[x] = cards.nextInt();
    yourDeck[x] = oppDeck[x];
    x++;*/


    oppDeck.add(cards.nextInt());
    yourDeck = oppDeck;
    }

    /*for(int y = 0; y<=51; y++)
    {
    System.out.print(yourDeck.get(y));
    }*/

    Random randomGenerator = new Random();
    Random randomGenerator2 = new Random();

    int randOpp = 0;
    int randYour = 0;

    Scanner askQ = new Scanner(System.in);
    String answer = "";

    while(oppDeck.size() != 0 || yourDeck.size() != 0)
    {

    System.out.print("Press 'd' to draw a card: ");
    answer = askQ.nextLine();

    randYour = randomGenerator.nextInt(yourDeck.size());
    System.out.print("You played the card: ");

    printYourCard(yourDeck, randYour);


    randOpp = randomGenerator.nextInt(oppDeck.size());
    System.out.print("Your opponent played the card: ");
    printOppCard(oppDeck, randOpp);
    System.out.println("");


    Integer a = new Integer(yourDeck.get(randYour).toString());
    int b = a.intValue();

    Integer c = new Integer(oppDeck.get(randOpp).toString());
    int d = c.intValue();


    if(b>d)
    {
    yourDeck.add(oppDeck.get(randOpp));
    oppDeck.remove(oppDeck.get(randOpp));
    oppDeck.trimToSize();
    yourDeck.trimToSize();
    }
    else if(b<d)
    {
    oppDeck.add(yourDeck.get(randYour));
    yourDeck.remove(yourDeck.get(randYour));
    yourDeck.trimToSize();
    }
    else if(b==d)
    {
    ArrayList keep = new ArrayList();

    keep.add(yourDeck.get(randYour));
    yourDeck.remove(yourDeck.get(randYour));
    keep.add(oppDeck.get(randOpp));
    oppDeck.remove(oppDeck.get(randOpp));


    //next drawn important card Your
    Random nextCard = new Random();
    int nCard = nextCard.nextInt(yourDeck.size());
    Integer e = new Integer(yourDeck.get(nCard).toString());
    int f = e.intValue();
    yourDeck.remove(yourDeck.get(nCard));

    //draw 3 and remove those
    Random warYou1 = new Random();
    int warY1 = warYou1.nextInt(yourDeck.size());
    keep.add(yourDeck.get(warY1));
    yourDeck.remove(yourDeck.get(warY1));

    Random warYou2 = new Random();
    int warY2 = warYou1.nextInt(yourDeck.size());
    keep.add(yourDeck.get(warY2));
    yourDeck.remove(yourDeck.get(warY2));

    Random warYou3 = new Random();
    int warY3 = warYou1.nextInt(yourDeck.size());
    keep.add(yourDeck.get(warY3));
    yourDeck.remove(yourDeck.get(warY3));


    //next drawn important card Opp
    Random oppNextCard = new Random();
    int oCard = oppNextCard.nextInt(oppDeck.size());
    Integer g = new Integer(oppDeck.get(oCard).toString());
    int h = g.intValue();
    oppDeck.remove(oppDeck.get(oCard));

    //draw 3 and remove those
    Random warOpp1 = new Random();
    int warO1 = warYou1.nextInt(yourDeck.size());
    keep.add(oppDeck.get(warO1));
    oppDeck.remove(oppDeck.get(warO1));

    Random warOpp2 = new Random();
    int warO2 = warYou1.nextInt(yourDeck.size());
    keep.add(oppDeck.get(warO2));
    oppDeck.remove(oppDeck.get(warO2));

    Random warOpp3 = new Random();
    int warO3 = warYou1.nextInt(yourDeck.size());
    keep.add(oppDeck.get(warO3));
    oppDeck.remove(oppDeck.get(warO3));


    if(f>h)
    {
    for(int i = 0; i<keep.size();i++)
    {
    yourDeck.add(keep.get(i));
    }
    }
    else if(f<h)
    {
    for(int i = 0; i<keep.size();i++)
    {
    oppDeck.add(keep.get(i));
    }
    }

    }//exit tie breaker
    System.out.println("--Your cards left: "+yourDeck.size());
    System.out.println("--Your opponents cards left: "+oppDeck.size());
    System.out.println();

    }










    }
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help making war card game

    Not sure about your stated problem, but I did find some things wrong in the code:

    1.)

    yourDeck = oppDeck;

    Are you sure you want to do that?

    I think that's making a shallow copy, to the best of my knowledge, not a deep copy.

    Hence whenever yourDeck is updated, oppDeck will be too with the same stuff. And vice versa.


    Instead of

    yourDeck = oppDeck;


    Have an int variable, perhaps called variable or whatever you like, and have it be initialized to -1 and be defined and initialized before the while loop.

    int variable = -1;

    ....
    ......

    oppDeck.add(cards.nextInt());
    variable++;
    yourDeck.add(oppDeck.get(variable));


    2.)

    while(oppDeck.size() != 0 || yourDeck.size() != 0)

    will go till BOTH decks are empty.

    while(oppDeck.size() != 0 && yourDeck.size() != 0)

    will go till AT LEAST ONE of the decks is empty.


    I'm guessing that one, or both, probably the error #1 for certain, of the errors might be contributing to your problem of it not adding or removing.

    If it was indeed making shallow copies, then basically every time it added to your Opponent's deck, it also added to yours and every time it removed from opponents deck, it also removed from yours.

    Hence it would appear to be doing nothing for b > d or b < d.

Similar Threads

  1. Help with Card Game
    By JSeol14 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2012, 10:46 PM
  2. A Card Game
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2012, 07:30 AM
  3. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM
  4. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM
  5. help on war card game project
    By sc0field1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 04:51 AM