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 with my code please Java war card game

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my code please Java war card game

    I have a code but I want it to run the actual game but it only runs a portion of the code. My code is the following:




    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.Random;
     
    public class warcardGame {
     
      public static void main(String[] args) {
     
     
        String line = "Welcome to my game of war. " + 
          "The rules of the game are simple " + 
          "Each player turns up a card at the same time and the player with the higher card takes both cards and puts them in their " +
          "respective decks"+
          " If the car1ds are the same rank , then another draw happens"+
          "The game is won when someone has ALL of the cards" + 
          "!!Aces are 14!!";
     
     
        System.out.println(line);
     
          warGamecards();
     
      }
     
     
      public static void randNum(){
        Random generator = new Random();
        ArrayList<Integer> numbers = new ArrayList<Integer>();   
        Random randomGenerator = new Random();
        while (numbers.size() < 52) {
     
          int random = randomGenerator .nextInt(52);
          if (!numbers.contains(random)) {
            numbers.add(random);
          }
        }
      }
     
     
      public static void deckCards(){
        ArrayList<String> deck = new ArrayList<String>();
        deck.add("Ace Of Hearts");
        deck.add("Ace Of Clubs");
        deck.add("Ace Of Spades");
        deck.add("Ace Of Diamonds");
        deck.add("2 of Hearts");
        deck.add("3 of Hearts");
        deck.add("4 of Hearts");
        deck.add("5 of Hearts");
        deck.add("6 of Hearts");
        deck.add("7 of Hearts");
        deck.add("8 of Hearts");
        deck.add("9 of Hearts");
        deck.add("10 of Hearts");
        deck.add("Jack of Hearts");
        deck.add("Queen of Hearts");
        deck.add("King of Hearts");
        deck.add("2 of Diamonds");
        deck.add("3 of Diamonds");
        deck.add("4 of Diamonds");
        deck.add("5 of Diamonds");
        deck.add("6 ofDiamonds");
        deck.add("7 ofDiamonds");
        deck.add("8 ofDiamonds");
        deck.add("9 ofDiamonds");
        deck.add("10 ofDiamonds");
        deck.add("Jack ofDiamonds");
        deck.add("Queen ofDiamonds");
        deck.add("King ofDiamonds");
        deck.add("2 of Clubs");
        deck.add("3 of Clubs");
        deck.add("4 of Clubs");
        deck.add("5 of Clubs");
        deck.add("6 of Clubs");
        deck.add("7 of Clubs");
        deck.add("8 of Clubs");
        deck.add("9 of Clubs");
        deck.add("10 of Clubs"); 
        deck.add("Jack of Clubs");
        deck.add("Queen of Clubs");
        deck.add("King of Clubs");
        deck.add("2 of Spades");
        deck.add("3 of Spades");
        deck.add("4 of Spades");
        deck.add("5 of Spades");
        deck.add("6 of Spades");
        deck.add("7 of Spades");
        deck.add("8 of Spades");
        deck.add("9 of Spades");
        deck.add("10 of Spades");
        deck.add("Jack of Spades");
        deck.add("Queen of Spades");
        deck.add("King of Spades");
     
     
      }
     
     
      public static void dealCards(){
     
        ArrayList<String> userhand = new ArrayList<String>();
        ArrayList<String> deck = new ArrayList<String>();
        for(int a = 0; a<deck.size(); a++){
     
          String checkword = deck.get(a);
          ArrayList<String> computerhand = new ArrayList<String>();
          if(userhand.contains(checkword) == false)
            userhand.add(checkword);
        }
      }
     
      public static void dealCardscpu(){
        ArrayList<String> userhand = new ArrayList<String>();
        ArrayList<String> deck = new ArrayList<String>();
        for(int a = 0; a<deck.size(); a++){
     
          String checkword = deck.get(a);
          ArrayList<String> computerhand = new ArrayList<String>();
          if(userhand.contains(checkword) == false)
            computerhand.add(checkword);
        }
      }
     
      public static void warGamecards(){
        Scanner reader = new Scanner(System.in);
        ArrayList<String> userhand = new ArrayList<String>();
        Random generator = new Random();
        ArrayList<String> computerhand = new ArrayList<String>();
        while(userhand.size()>0 && computerhand.size()>0){
          //ArrayList<String> userhand = new ArrayList<String>();
          int l = generator.nextInt(userhand.size()-0);
          int p = generator.nextInt(computerhand.size()-0);
          String cardone = userhand.get(l);
          String cardtwo = computerhand.get(p);
     
          System.out.println("----------------------------------------------------------------------------------------------------------");
          System.out.println("Your : " + cardone);
          System.out.println("Computer Card: " + cardtwo);
          System.out.println("");
          System.out.println("You have " + userhand.size() + " cards.");
          System.out.println("The computer has " + computerhand.size() + " cards.");
          System.out.println("");
     
          System.out.println("If your card is bigger please enter 1.");
          System.out.println("If the computer card is bigger please enter 2.");
          System.out.println("If there is a tie enter 3.");
     
          System.out.print("Enter winner: ");
          int winner = reader.nextInt();
     
          if(winner == 1){
            userhand.add(cardtwo);
            computerhand.remove(p);
          }
     
          if(winner == 2){
            computerhand.add(cardone);
            userhand.remove(l);
          }
     
        }
     
        if(userhand.size() == 0){
          System.out.println("The computer has won the game.");
          System.out.println("THANKS FOR PLAYING");
     
        }
     
        {
          if(computerhand.size() == 0){
            System.out.println("You have won the game!!!! :)");
            System.out.println("THANKS FOR PLAYING");
     
     
          }
     
     
     
        }
     
     
      }
    }


    It only outputs the following :
    The rules of the game are simple 
      Each player turns up a card at the same time and the player with the higher card takes both cards and puts them in their 
         respective decks
           If the car1ds are the same rank , then another draw happens
          The game is won when someone has ALL of the cards
          !!Aces are 14!!
     
     
    "The computer has won the game."
    "THANKS FOR PLAYING"
     
    "You have won the game!!!! :)"
    "THANKS FOR PLAYING"

    Please help, again I want the code to run the actual game.
    Your help is greatly appreciated
    Thanks

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Help with my code please Java war card game

    There are two ArrayLists in the warGamecards() method.
    When are any objects stored in them? The while statement requires that they have something in them.

    Note: It is usually a bad idea to define variables inside of a method are are going to be used by other methods. The variables should be defined at the class level so that any method that needs access to the variables can access them.

    Any variable defined inside of a method will disappear when execution leaves the method. If you want variables to persist between method calls, define the variables at the class level.

Similar Threads

  1. Help with Arrays (Java card game)
    By tonynsx in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 8th, 2013, 04:49 PM
  2. help making war card game
    By thephipster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2012, 01:39 PM
  3. [SOLVED] Card Program"War" Class help
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 16th, 2012, 08:43 PM
  4. code for 3 card brag game
    By jay_tee92 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 20th, 2011, 11:51 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