Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
Like Tree1Likes

Thread: How to write a method within a method?

  1. #21
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: How to write a method within a method?

    Thanks for the chip-in, Jim. I appreciate it. I agree there's room for improvement and simplification here. This was just the bare minimum. I created the Deck like that, because the final game will have much more complex cards and I needed a simple, easy to read example which can easily be extended by Kanada (OP).

    I am trying not to overload OP here. You can throw in a few Lambdas and streams too but at this stage that's not helping. Let's get this working before we move on to the next stage :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  2. #22
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default Re: How to write a method within a method?

    Right! And I fully agree with the lambda statement. They provide a whole new paradigm for writing
    code (with which I am still not fully fluent). And they also force one to really understand
    generics to properly apply them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default Re: How to write a method within a method?

    I was looking at the following method and noticed a slight problem.

    Java Code:
    public List<Card> getRandomCards(int number) { 
        List<Card>  result = new ArrayList<>();
         for (int i = 0 ; i < number && i < cards.size(); i++) {
             result.add(cards.remove(i));
         }
         return result;
    }
    Although you do an inline test in the for loop, it doesn't always return the number of cards you want when you get
    near the end (even if there are sufficient cards available). That is because when you do remove(i), the card list is updated.
    This can cause inconsistencies in the way the items are chosen. I think remove(0) would always remove the next item since
    each remove updates the list.

    Regards,
    Jim
    Last edited by jim829; 01-12-2018 at 06:47 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #24
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: How to write a method within a method?

    These are the sort of bugs that will popup once the game is 90% complete :)

    The first 90 percent of the code accounts for the first 90 percent of the development time. The remaining 10 percent of the code accounts for the other 90 percent of the development time.
    -  Tom Cargill, Bell Labs
    jim829 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #25
    Kanada is offline Member
    Join Date
    Mar 2017
    Posts
    14
    Rep Power
    0

    Default Re: How to write a method within a method?

    So I've gotten to the point where I can take in user input and where player 1 enters a number 1-4 to choose any of the 4 cards from their hand. So there's finally some user input interaction and the game works barebones. I gave each player their own deck objects so they're drawing from their own player1/2.deck as well.

    The next part that I think would probably be the simplest to implement to the game for now is doing the card types which I've given as fire/water/grass as a string into the Card class.
    My idea to incorporate into the game loop is kind of lousy though.
    I'm thinking of in the if's and elses's of the game loop it will be like where fire>grass>water>fire etc... :

    if(card1.getApc() > card2.getApc() && card1.type=fire && card2.type=grass){...have a super effective type interaction where card1.getApc() is increased with some random value}
    else if (card1.getApc() > card2.getApc() && card1.type=fire && card2.type=water){... card2.getApc() is increased with some random value...}
    else if (card1.getApc() > card2.getApc() && card1.type=fire && card2.type=fire){... normal battle conditions...}

    but if I do that for all the multiple conditions of combinations of card types, I've will be copying and pasting 3 times. It's not to awful with copying and pasting a few times but would there be a better method?

    There's some further developments that I might decide to add, like a new type like ice, psychic or even two types combined at the same time in the future(Pokemon inspiration...) but I probably want to think further in more depth for the game design for that first.

    So my code so far is:
    Game:
    Java Code:
    import java.util.List;
    import java.util.Scanner;
    
    public class Game16 {
    	private Player player1;
    	private Player player2;
    	
    	public static void main(String[] args){
    		Game16 game16 = new Game16();
    		game16.start();
    	} 
    	
    	public Game16(){
    		this.player1 = new Player("Ply1");
    		this.player2 = new Player("Ply2");
    	}
    	
    	private void start(){
    		//Removes 5 cards from deck and adds puts this into arrayList randomCardsForPlayer
    		//get 5 random cards from player1's deck arrayList
    		//.getHand() returns hand. Launches Hand constructer?
    		List<Card> randomCardsForPlayer1 = player1.getCardsFromDeck(4);
    		Hand handOfPlayer1 = player1.getHand();
    		//add all cards of the 5 cards gotten to hand arrayList
    		handOfPlayer1.addAllCards(randomCardsForPlayer1);
    		
    		List<Card> randomCardsForPlayer2 = player2.getCardsFromDeck(4);
            Hand handForPlayer2 = player2.getHand();
            handForPlayer2.addAllCards(randomCardsForPlayer2);
            
            player1.checkHandArraySize();
            startBattle();
    	}
    	
    	private void startBattle(){
    		while(!player1.isDead() && !player2.isDead()){
    			//Scanner for getting parameter from player 1. 1-5 is in array form 0-4
    			int ply1InputNumber;
    			
    			//Display Cards for player1, card0-4 from hand .getcardid
    			System.out.println("Choose a Card:");
    			System.out.println("Card ID = " + player1.getHand().getCard(0).getCardId() + " Card APC = " + player1.getHand().getCard(0).getApc());
    			System.out.println("Card ID = " + player1.getHand().getCard(1).getCardId() + " Card APC = " + player1.getHand().getCard(1).getApc());
    			System.out.println("Card ID = " + player1.getHand().getCard(2).getCardId() + " Card APC = " + player1.getHand().getCard(2).getApc());
    			System.out.println("Card ID = " + player1.getHand().getCard(3).getCardId() + " Card APC = " + player1.getHand().getCard(3).getApc() + "\n");
    			
    			Scanner Player1Scanner = new Scanner(System.in);
    			System.out.println("Enter Hand Card Number 1-4:" );
    			ply1InputNumber = Player1Scanner.nextInt() - 1; 
    			System.out.println("Player 1 chooses card " + ply1InputNumber + 1 + " from Hand"); 
    
    			//get random card from hand arrayList 
    			//this is removing the card from hand so at if/slse staement array size is 4.
    			//getHand returns player's hand and can use methods from hand class?
    			Card card1 = player1.getHandCard(ply1InputNumber);
                Card card2 = player2.getHand().getRandomCard();
                
    			player1.checkHandArraySize(); 
                
                System.out.println("Player " + player1.getName() + " drew " + card1.getCardId() + " apc= " + card1.getApc());
                System.out.println("Player " + player2.getName() + " drew " + card2.getCardId() + " apc= " + card2.getApc());
                
                if (card1.getApc() > card2.getApc()) {
                    System.out.println("Player " + player1.getName() + " wins with " + card1.getApc() + "APC over " + card2.getApc() + "APC");
                    player2.setLifePoints(player2.GetLifePoints() - card1.getApc());
                    System.out.println("Player " + player2.getName() + " has " + player2.GetLifePoints() + " LP left" + "\n");
                    //Problem with players not having their own decks. ie.need to create a this.deck in player class
                    //Need to add a a single deck card into hand again.
                    //add 1 card from deck to hand
                    //Need to discard played card from both players 
                    player1.getHand().removeCard(card1);
                    player2.getHand().removeCard(card2);
                    player1.addDeckCardToHandAt0Index();
                    player2.addDeckCardToHand();
                }
                else if (card2.getApc() > card1.getApc()) {
                    System.out.println("Player " + player2.getName() + " wins with " + card2.getApc() + "APC over " + card1.getApc() + "APC");
                    player1.setLifePoints(player1.GetLifePoints() - card2.getApc());
                    System.out.println("Player " + player1.getName() + " has " + player1.GetLifePoints() + " LP left" + "\n");
                    //add 1 card from player's respective deck to hand
                    player1.getHand().removeCard(card1);
                    player2.getHand().removeCard(card2);
                    player1.addDeckCardToHandAt0Index();
                    player2.addDeckCardToHand();
                }
                else {
                    System.out.println("It's a draw!");
                    //add 1 card from deck to hand, remove used card (1&2)
                    player1.getHand().removeCard(card1);
                    player2.getHand().removeCard(card2);
                    player1.addDeckCardToHandAt0Index();
                    player2.addDeckCardToHand();
                }
    		}
    	}
    }
    Player:
    Java Code:
    import java.util.List;
    
    public class Player {
    	private String name;
    	private Hand hand;
    	private Deck deck;
    	private int lifePoints;
    	
    	public Player(String name){
    		this.name = name;
    		this.hand = new Hand();
    		this.lifePoints = 300;
    		this.deck = new Deck();
    	}
    	
    	public String getName(){
    		return name;
    	}
    	
    	public Hand getHand(){
    		return hand;
    	}
    	
    	public int GetLifePoints(){
    		return lifePoints;
    	}
    	
    	public void setLifePoints(int lifePoints){
    		this.lifePoints = lifePoints;
    	}
    	
    	public boolean isDead(){
    		return lifePoints <= 0;
    	}
    	
    	public void checkHandArraySize(){
        	this.hand.CheckArraySize();
        }
    	
    	//get Deck arrayList for this player object
    	public List<Card> getCardsFromDeck(int a){
    		return this.deck.getRandomCards(a);
    	}
    	
    	public Card getRandDeckCard(){
        	return this.deck.getDeckRandomCard();
        }
    	
    	//add deck card to hand
    	public void addDeckCardToHand(){
    		Card a = this.getRandDeckCard();
    		this.hand.addCard(a);
    	}
    	
    	//add deck card to hand
    		public void addDeckCardToHandAt0Index(){
    			Card a = this.getRandDeckCard();
    			this.hand.addCardAt0Index(a);
    		}
    	
    	//A method to link to a get card from hand class.
    	public Card getHandCard(int b){
    		return this.hand.getCard(b);
    	}
    }
    Deck:
    import java.util.*;

    public class Deck {
    Java Code:
     private List<Card> cards;
     
        public Deck() {
            cards = new ArrayList<>();
            cards.add(new Card("c1", 20, 100, "fire"));		
            cards.add(new Card("c2",30, 80, "fire"));		
            cards.add(new Card("c3",10, 90, "water"));
            cards.add(new Card("c4",40, 100, "grass"));
            cards.add(new Card("c5",50, 80, "fire"));
            cards.add(new Card("c6",60, 90, "water"));
            cards.add(new Card("c7",20, 90, "fire"));
            cards.add(new Card("c8",30, 90, "fire"));
            cards.add(new Card("c9",10, 90, "water"));
            cards.add(new Card("c10",30, 90, "grass"));
            cards.add(new Card("c11",40, 100, "grass"));
            cards.add(new Card("c12",50, 80, "fire"));
            cards.add(new Card("c13",60, 90, "water"));
            cards.add(new Card("c14",20, 90, "fire"));
            cards.add(new Card("c15",30, 90, "fire"));
            cards.add(new Card("c16",60, 90, "water"));
            cards.add(new Card("c17",20, 90, "fire"));
            cards.add(new Card("c18",30, 90, "fire"));
            cards.add(new Card("c19",10, 90, "water"));
            cards.add(new Card("c20",30, 90, "grass"));
            cards.add(new Card("c21",40, 100, "grass"));
            cards.add(new Card("c22",50, 80, "fire"));
            cards.add(new Card("c23",60, 90, "water"));
            cards.add(new Card("c24",20, 90, "fire"));
            cards.add(new Card("c25",30, 90, "fire"));
            cards.add(new Card("c26",60, 90, "water"));
            cards.add(new Card("c27",20, 90, "fire"));
            cards.add(new Card("c28",30, 90, "fire"));
            cards.add(new Card("c29",10, 90, "water"));
            cards.add(new Card("c30",30, 90, "grass"));
            cards.add(new Card("c31",40, 100, "grass"));
            cards.add(new Card("c32",50, 80, "fire"));
            cards.add(new Card("c33",60, 90, "water"));
            cards.add(new Card("c34",20, 90, "fire"));
            cards.add(new Card("c35",30, 90, "fire"));
            
            //shuffle cards
            Collections.shuffle(cards);
        }
        
        //get card, put number of cards of parameter into results.
        //Adds the cards that are being removed into result.
        public List<Card> getRandomCards(int number) {
            List<Card>  result = new ArrayList<>();
            for (int i = 0 ; i < number && i < cards.size(); i++) {
                result.add(cards.remove(i));
            }
            return result;
        }
        
        public Card getDeckRandomCard(){
        	int randomIndex = new Random().nextInt(this.cards.size()); //List called cards
            return cards.get(randomIndex);    						   //Random().nextInt gets a random element
        }
    }
    Hand:
    Java Code:
    import java.util.*;
    public class Hand {
    	private List<Card> cards;
    	
    	public Hand(){
    		cards = new ArrayList<>();
    	}
    	
    	public void addCard(Card card){
    		cards.add(card);
    	}
    	
    	public void addCardAt0Index(Card card){
    		cards.add(0, card);
    	}
    	
    	//Takes in all cards of an arrayList and adds card objects to hand's arrayList 
    	public void addAllCards(List<Card> cards){
    		this.cards.addAll(cards);
    	}
    	
    	//get int of random card from deck arrayList and remove it
    	public Card getRandomCard() {
    		int rand = new Random().nextInt(cards.size());
            return this.cards.remove(rand);
    	}
    	
    	public void CheckArraySize(){
    		System.out.println("Cards in hand= " + this.cards.size()); 
    	}
    	
    	public Card getCard(int b){
    		return this.cards.get(b);
    	}
    	
    	public void removeCard(Card card){
    		cards.remove(card);
    	}
    }
    Card:
    Java Code:
    public class Card {
    	private String cardId;
        private int apc;
        private int hp;
        private String type;
     
        public Card(String cardId, int apc, int hp, String type) {
        	this.cardId = cardId;
            this.apc = apc;
            this.hp = hp;
            this.type = type;
        }
        
        public void setCardId(String cardId) {
            this.cardId = cardId;
        }
        
        public String getCardId() {
        	return cardId;
        }
     
        public int getApc() {
            return apc;
        }
     
        public void setApc(int apc) {
            this.apc = apc;
        }
     
        public int getHp() {
            return hp;
        }
     
        public void setHp(int hp) {
            this.hp = hp;
        }
        
        public String getType(){
        	return type;
        	} 
        
        public void setType(String type){
        	this.type = type;
        	} 
    }
    So I have a few other questions as well...
    for the getHand in Player you return hand. I'm a bit confused on what the hand is returning. Is it using the constructor in public Hand so basically returning whatever is being constructed there AKA the hands.card array or is it much more than this?

    In deck class there's result.add(cards.remove(i));
    So this is basically adding the cards that are being removed simultaneously? So it adds the card first to cards arrayList and then removed them?

    Also Jim identified that problem with the array, I don't understand it?
    I tried something in the game with Players life points set to 300 LP and given only 8 cards in a deck. And when it runs out of options it repeats randomly from the start again? This is actually desirable for me because I was thinking that when a player runs out of cards in deck to draw, the deck is reshuffled and so the player draws from the start again. I don't really understand what's happening though. Is it because of the Collections.shuffle(cards);? Originally I was thinking that the game would just crash if the player runs out of cards in the deck since it has result.add(cards.remove(i));

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2013, 06:31 AM
  2. How to Write to a File Using a Method
    By Anagha in forum New To Java
    Replies: 7
    Last Post: 04-06-2011, 07:50 AM
  3. Writing a method to write a method?
    By balla in forum New To Java
    Replies: 5
    Last Post: 04-01-2011, 01:05 PM
  4. how to write method syntax ?
    By Bader in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 06:58 PM
  5. Trying to write toString method
    By curious in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 03:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •