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 2 of 2 FirstFirst 12
Results 26 to 34 of 34

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

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    if there are three of a kind
    Where are the items that are being counted to see if there are three of a kind? Should there be three variables that the code tests the contents of to see if they match?
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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 are the items that are being counted to see if there are three of a kind? Should there be three variables that the code tests the contents of to see if they match?
    There is another class (class Player) which draws the cards and holds them. all it does is (riskCards.draw(Card)). So it holds the cards drawn.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    What does that have to do with coding enum usage?
    Post#24 shows a way to use enums.
    Rewrite the code in post#23 to use that technique and show what you are trying to do.
    Add some comments to document what the code is trying to do.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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
    What does that have to do with coding enum usage?
    Post#24 shows a way to use enums.
    Rewrite the code in post#23 to use that technique and show what you are trying to do.
    Add some comments to document what the code is trying to do.
    With the current coding, an arrayList is being created, all I am trying to say with this is if the same enum(Infantry, Artillery, or Calvalry) appear three times in this ArrayList, then the statement is true

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    if the same enum(Infantry, Artillery, or Calvalry) appear three times in this ArrayList
    That sounds like you need a loop to go through the contents of the ArrayList and count the number of times each item appears in the list.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    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 sounds like you need a loop to go through the contents of the ArrayList and count the number of times each item appears in the list.
    Well managed to get it to iterate through the list to get

    Just can't seem to figure out how to test if multiples of say ARTILLERY appears in the hand

      public boolean canTurnInCards(){
     
        for(int i = 0; i < deck.size(); i++){
          deck.get(i);
        }
      }

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Please post the current version of the code that shows what problem you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    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
    Please post the current version of the code that shows what problem you are having.
    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{
     
     private 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 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(){
     
        for(int i = 0; i < deck.size(); i++){
          deck.get(i);
        }
      }
     
      public boolean mustTurnInCards(){
        if(deck.size() > MAXCARDS)
          return true;
        else
          return false;
     
      }
     
      public ArrayList<Card> getHand(){
        return deck;
      }
     
    }

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    How do you test the code? I don't see a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

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