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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Trying to make a Risk game, but I got stuck on the Hand class.

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to make a Risk game, but I got stuck on the Hand class.

    package RiskProject;
     
    public class Card{
      //set for the Type of Countries
      public enum Country{ALASKA, ALBERTA, CENTRAL_AMERICA, EASTERN_UNITED_STATES, GREENLAND, NORTHWEST_TERRITORY,
      ONTARIO, QUEBEC, WESTERN_UNITED_STATES, ARGENTINA, BRAZIL, VENEZUELA, GREAT_BRITAIN, ICELAND, NORTHERN_EUROPE,
      SCANDINAVIA, SOUTHER_EUROPE, UKRAINE, WESTERN_EUROPE, CONGO, EAST_AFRICA, EGYPT, MADAGASCAR, NORTH_AFRICA,
      SOUTH_AFRICA, AFGHANISTAN, CHINA, INDIA, IRKUTSK, JAPAN, KAMCHATKA, MIDDLE_EAST, MONGOLIA, SIAM, SIBERIA, URAL,
      YAKUTSK, EASTERN_AUSTRALIA, INDONESIA, LOTR, NEW_GUINEA, WESTERN_AUSTRALIA};
      //set for the Type of cards
      public enum Arms{INFANTRY, ARTILLERY, CALVALRY};
     
      private final Country country;
      private final Arms arms;
     
      public Card(Country country, Arms arms){
     
        this.country = country;
        this.arms = arms;
     
      }
     
      public @Override String toString(){
        return country + "," + arms;
      }
     
      public Arms getArms(){
     
        return arms;
     
      }
     
      public Country getCountry(){
     
        return country;
     
      }
     
    }

    package RiskProject;
     
    import java.util.ArrayList;
    import java.util.Collections;
     
    public class Deck{
     
     public ArrayList<Card> deck;
     
     Card.Country[] countries = Card.Country.values();
     Card.Arms[] arm = Card.Arms.values(); 
     
     public Deck(){
       deck = new ArrayList<Card>();  
       }
     
     
     
     public Card draw(){
     
       return deck.remove(0);
     
     }
     
     public void add(){
     
       for(Card.Arms arms : arm){
         for(Card.Country country : countries){
           deck.add(new Card(country, arms));
         }
       }
     }
     
     public void shuffle(){
     
       Collections.shuffle(deck);
     
     }
     
     
     
    }

    package RiskProject;
     
    import java.util.ArrayList;
     
    public class Hand{
     
      private final ArrayList<Card> deck;
     
      public Hand(){
        deck = new ArrayList<Card>();
      }
     
      public Card add(Card card){
     
        deck.add(card);
        return card;
     
      }
     
      public Card removeCardsFromHand(int index1, int index2, int index3){
     
        deck.remove(index1, index2, index3);//The method remove(int) in the type java.util.ArrayList<RiskProject.Card> is not applicable for the arguments(int, int, int)
        return deck;
     
      }
     
      public boolean canTurnInCards(){
     
      }
     
      public boolean mustTurnInCards(){
     
      }
     
      public ArrayList<Card> getHand(){
     
      }
     
    }
    Last edited by geeksutopia; April 23rd, 2014 at 11:45 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Please describe why you're stuck and ask a specific question. Post errors, undesired results, etc.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by GregBrannon View Post
    Please describe why you're stuck and ask a specific question. Post errors, undesired results, etc.
    The error is posted in the Hand class, I am stuck on the the remove cards from hand method. Just can't seem to figure out what to do here

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    The method remove(int) in the type java.util.ArrayList<RiskProject.Card> is not applicable for the arguments(int, int, int)
    The ArrayList class's remove() method is defined to take one int as its argument. The code is trying to call that method with 3 int values.
    Can the code be changed to call the remove() method with a single int as arg? Perhaps call it three times, once with each int.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    The ArrayList class's remove() method is defined to take one int as its argument. The code is trying to call that method with 3 int values.
    Can the code be changed to call the remove() method with a single int as arg? Perhaps call it three times, once with each int.
    Just implemented that and it compiles. Now the return statement is giving me problems.

    public Card removeCardsFromHand(int index1, int index2, int index3){
                  deck.remove(index1);
                  deck.remove(index2);
                  deck.remove(index3);
                  return deck;//Type mismatch cannot convert from java.util.ArrayList<RiskProject.Card> to RiskProject.Card
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    public Card removeCardsFromHand(
    The method says that it is going to return an instance of the Card class.
    Is that what it is supposed to return?

    The code is trying to return an ArrayList. The method does not create any objects, so it should not be returning anything. It updates an existing object: deck that is globally available to any other methods in the class.

    What should happen to the 3 Card objects that were removed from the deck?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    public Card removeCardsFromHand(
    The method says that it is going to return an instance of the Card class.
    Is that what it is supposed to return?

    The code is trying to return an ArrayList. The method does not create any objects, so it should not be returning anything. It updates an existing object: deck that is globally available to any other methods in the class.

    What should happen to the 3 Card objects that were removed from the deck?
    It's supposed to return an instance of the Card class, sort of what is in your hand now, The three cards that get removed are gone completely.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    It's supposed to return an instance of the Card class, sort of what is in your hand now
    If there are more than one card in the hand after removing 3 cards from deck, which card is it supposed to return?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    public Card removeCardsFromHand(
    The method says that it is going to return an instance of the Card class.
    Is that what it is supposed to return?

    The code is trying to return an ArrayList. The method does not create any objects, so it should not be returning anything. It updates an existing object: deck that is globally available to any other methods in the class.

    What should happen to the 3 Card objects that were removed from the deck?
    found out the answer to the return statement which is "return deck.get(index1);" which compiles correctly.

    Working on the canTurnInCards statement which I am using
    Card.Arms[]arm = Card.Arms.values();

    And seeing how that this:

    public boolean canTurnInCards(){
      if(INFANTRY == 3){
          return true;
      }
    }

    It says that the use of CALVALRY cannot be resolved to a variable
    Same with INFANTRY or ARTILLERY

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Try adding on the name of the enum.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    Try adding on the name of the enum.
    The name of the enum is Arms, which I instantiated with
    Card.Arms[] arm = Card.Arms.values();

    And tried putting it into practice in the boolean using

    if(arm.values(INFANTRY) == 3){
      return true;
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Did you try: Arms.INFANTRY
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    Did you try: Arms.INFANTRY
    Tried it, and nothing. Got an Arms cannot be resolved to a variable

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Can you post a small, simple, complete program that shows the problem?
    Something that will compile when the problem is fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    Can you post a small, simple, complete program that shows the problem?
    Something that will compile when the problem is fixed.
    package RiskProject;
     
    import java.util.ArrayList;
     
    public class Hand{
     
      private final ArrayList<Card> deck;
      public int MAXCARDS = 4;
      Card.Arms[] arm = Card.Arms.values();
     
      public Hand(){
        deck = new ArrayList<Card>();
      }
     
      public Card add(Card card){
     
        deck.add(card);
        return card;
     
      }
     
      public Card removeCardsFromHand(int index1, int index2, int index3){
     
        deck.remove(index1);
        deck.remove(index2);
        deck.remove(index3);
        return deck.get(index1);
      }
     
      public boolean canTurnInCards(){
     
        if(Arms.INFANTRY == 3){
          return true;
        }
        else if(ARTILLERY == 3){
          return true;
        }
        else if(CALVALRY == 3){
          return true;
        }
        else if(ARTILLERY == 1 && Infantry == 1 && CALVALRY == 1){
          return true;
        }
     
      }
     
      public boolean mustTurnInCards(){
        if(deck.size() > MAXCARDS)
          return true;
        else
          return false;
     
      }
     
      public ArrayList<Card> getHand(){
        return deck;
      }
     
    }

    This mixed in with the other two classes in the post should compile

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Sorry, I'd prefer ONE file for testing, not two or three or whatever is needed.
    It shouldn't take over 20 lines of code.

    Also post the full text of the compiler's error message when you try to compile it.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    Sorry, I'd prefer ONE file for testing, not two or three or whatever is needed.
    It shouldn't take over 20 lines of code.

    Also post the full text of the compiler's error message when you try to compile it.
    6 errors found:
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 32]
    Error: Arms cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 35]
    Error: ARTILLERY cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 38]
    Error: CALVALRY cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 41]
    Error: ARTILLERY cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 41]
    Error: Infantry cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 41]
    Error: CALVALRY cannot be resolved to a variable

    This is the error I get when I try to compile, the only file that is wrong is the Hand class, the other two run perfectly

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Where is the code that generates those error messages?

    Can you post a small, simple, complete program that shows the problem?
    Something that will compile when the problem is fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    Where is the code that generates those error messages?

    Can you post a small, simple, complete program that shows the problem?
    Something that will compile when the problem is fixed.
    package RiskProject;
     
    import java.util.ArrayList;
     
    public class HandTest{
     
     
     
      Card.Arms[] arm = Card.Arms.values();
     
      public HandTest(){
     
      }
     
      public boolean canTurnInCards(){
     
        if(arm.INFANTRY == 3){
          return true;
        }
      }
    }

    This should compile when fixed. The error for it is

    INFANTRY cannot be resolved or is not a field.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    That code doesn't compile. These are its errors:
    Running: D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint HandTest.java

    HandTest.java:8: error: package Card does not exist
    Card.Arms[] arm = Card.Arms.values();
    ^
    HandTest.java:8: error: package Card does not exist
    Card.Arms[] arm = Card.Arms.values();
    ^
    2 errors

    2 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    That code doesn't compile. These are its errors:
    The reason for those errors is cause the class Card and the class HandTest are in the same package.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    I don't have any packages for testing forum code. That's why I'd like all the code in one file for testing.

        if(arm.INFANTRY == 3){
    arm is an array and does not have a field named: INFANTRY
    To access the contents of an array use array notation: []
    arm[0] would be the first element in the array.

    I have no idea what that if statement could be testing for.
    With most enum usage you don't compare the enum's value to an int. That would conflict with the purpose of using enums: Use names as values for a type. For example: INFANTRY is a value for type Arms
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    I don't have any packages for testing forum code. That's why I'd like all the code in one file for testing.

        if(arm.INFANTRY == 3){
    arm is an array and does not have a field named: INFANTRY
    To access the contents of an array use array notation: []
    arm[0] would be the first element in the array.

    I have no idea what that if statement could be testing for.
    With most enum usage you don't compare the enum's value to an int. That would conflict with the purpose of using enums: Use names as values for a type. For example: INFANTRY is a value for type Arms
     
     
    import java.util.ArrayList;
     
    public class HandTest{
     
      public enum Arms{INFANTRY, ARTILLERY, CALVALRY};
     
      private final Arms arms;
     
      public HandTest(Arms arms){
        this.arms = arms;
      }
      public @Override String toString(){
        return "Card" + arms;
      }
      public Arms getArms(){
        return arms;
      }
     
      public boolean canTurnInCards(){
        if(arms.INFANTRY == 3){
          return true;
        }
        else if(arms.ARTILLERY == 3){
          return true;
        }
        else if(arms.CALVALRY == 3){
          return true;
        }
        else if(arms.INFANTRY == 1 && arms.ARTILLERY == 1 && arms.CALVALRY == 1){
          return true;
        }
        else
          return false;
      }
    }
    }

    12 errors and 6 warnings found:
    --------------
    *** Errors ***
    --------------
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 32]
    Error: Arms cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 35]
    Error: ARTILLERY cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 38]
    Error: CALVALRY cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 41]
    Error: ARTILLERY cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 41]
    Error: Infantry cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\Hand. java [line: 41]
    Error: CALVALRY cannot be resolved to a variable
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 22]
    Error: Incompatible operand types HandTest.Arms and int
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 25]
    Error: Incompatible operand types HandTest.Arms and int
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 28]
    Error: Incompatible operand types HandTest.Arms and int
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 31]
    Error: Incompatible operand types HandTest.Arms and int
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 31]
    Error: Incompatible operand types HandTest.Arms and int
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 31]
    Error: Incompatible operand types HandTest.Arms and int
    --------------
    ** Warnings **
    --------------
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 22]
    Warning: The static field HandTest.Arms.INFANTRY should be accessed in a static way
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 25]
    Warning: The static field HandTest.Arms.ARTILLERY should be accessed in a static way
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 28]
    Warning: The static field HandTest.Arms.CALVALRY should be accessed in a static way
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 31]
    Warning: The static field HandTest.Arms.INFANTRY should be accessed in a static way
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 31]
    Warning: The static field HandTest.Arms.ARTILLERY should be accessed in a static way
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\RiskProject\HandT est.java [line: 31]
    Warning: The static field HandTest.Arms.CALVALRY should be accessed in a static way


    What I am trying to do is that if there are multiples of the same arms (like three cards of the same type) I can trade them in

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    The enum is spelled: with A not a- Arms not arms.

    Why does the code try to compare an enum to an int?

    This code compiles:
        if(Arms.INFANTRY == arms){
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Risk game, but I got stuck on the Hand class.

    Quote Originally Posted by Norm View Post
    The enum is spelled: with A not a- Arms not arms.

    Why does the code try to compare an enum to an int?

    This code compiles:
        if(Arms.INFANTRY == arms){
    but wait shouldn't it be if there are three of a kind, the statement is true.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: March 17th, 2014, 06:45 PM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  3. I'm stuck on my Camelot game
    By Stagnit in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 11th, 2012, 03:57 PM
  4. I need a hand in editing this game
    By FEU_4thyear in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 6th, 2012, 08:14 AM
  5. typing game stuck up
    By chronoz13 in forum AWT / Java Swing
    Replies: 6
    Last Post: April 29th, 2011, 08:09 AM

Tags for this Thread