This site requires JavaScript, please enable it in your browser!
Greenfoot back
ProfessionalNoob
ProfessionalNoob wrote ...

2015/3/29

How to set a value for an object?

ProfessionalNoob ProfessionalNoob

2015/3/29

#
Hello. I was wondering how to assign a value to a card so that when the player selects a card, he can only do something with it if it's their OWN card. This is my cards class:
import greenfoot.*;

/**
 * Write a description of class Ogre here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cards extends Actor
{
    
    private static Cards attacker;
    SomeDice dice = new SomeDice();
    int health;
    int cost;
    int attack;
    int number;
    boolean click;
    boolean Selected;
    public GreenfootSound[] CardSounds = { new GreenfootSound("KittyPlaySound.mp3"),
                                                         new GreenfootSound("HoundPlaySound.mp3"),
                                                         new GreenfootSound("OgrePlaySound.mp3"),
                                                         new GreenfootSound("RaptorPlaySound.mp3"),
                                                         new GreenfootSound("MurlocPlaySound.mp3"),
                                                         new GreenfootSound("GrommashPlaySound.mp3")};
    public static final GreenfootImage[] CardImages = { new GreenfootImage("Kitty.png"),
                                                         new GreenfootImage("Hound.png"),
                                                         new GreenfootImage("Ogre.png"),
                                                         new GreenfootImage("Raptor.png"),
                                                         new GreenfootImage("Murloc.png"),
                                                         new GreenfootImage("Grommash.png")};
                                                         
    private boolean visible;
    public GreenfootImage BackCard = new GreenfootImage("CardBack.png");
    boolean Grab = false;
    boolean MouseHover = false;
    boolean Deployment = false; 
    boolean OnBoard = false;
    boolean Dep = false;
    boolean val = false;
    boolean battleposition;
    boolean initialized = false;
    boolean attacked;
    boolean Grabbed;
    int NormalHeight = CardImages[number].getHeight();
    int NormalWidth = CardImages[number].getWidth(); 
    int position;
    int limit;
    int Cardvalue;
    
    public void act() 
    {
   
    if (click == false)
    { 
        Generation(); 
        click = true;
    }
        mouseDragging();  
        Deployment();
    if(!Dep && Deployment)
    {
        Position();
        position++; 
        Dep = true; 
    }
    
    /*if(Deployment && Dep)
    {
        if(Greenfoot.mouseClicked(this))
        {
            initialized = true; 
        }
        if(initialized && Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            Actor mouseActor = mouse.getActor(); 
            if(mouseActor!= null && mouseActor instanceof Cards)
            {
                setLocation(50,50); 
            }
        }
        
    }*/
    
    if(Deployment && Greenfoot.mouseClicked(this))
    {
        if (attacker == null) attacker = this;
        else if(attacker != this)
        {
         loseHealth(attacker.attack);
         if (health <= 0)
         {
             getWorld().removeObject(this); 
         }
            attacker = null;
        }
        else attacker = null;
    }
  
}
    public void CardImage()
    {
        if(getY() > 400)
        {
            for (Object PlayerCards: getObjectsInRange(100, Cards.class))
            {
                Cards a = (Cards) PlayerCards;
                a.setImage(BackCard);
            }
        }
        
        if(getY() < 400)
        {
            for (Object PlayerCards: getObjectsInRange(100, Cards.class))
           {
               Cards a = (Cards) PlayerCards;
               a.setImage(BackCard);
           }
           
        }
    }
   public int Generation()
   {

    number = dice.Roll(number);
    CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
    setImage(CardImages[number]);

    switch(number)
    {
        case 0:
            cost = 8;
            attack = 4;
            health = 9;
            return 8;
        case 1:
            cost = 0;
            attack = 1;
            health = 1;
            return 0;
        case 2:
            cost = 7;
            attack = 9;
            health = 5;
            return 7;
        case 3:
            cost = 6;
            attack = 6;
            health = 7;
            return 6;
        case 4:
            cost = 2;
            attack = 3;
            health = 2;
            return 2;
        case 5:
            cost = 1;
            attack = 2;
            health = 1;
            return 1;
    }
    return 10;
}
public void setVisible(boolean state)
{
    visible = state; 
    if(!Deployment)
    {
    if(visible == true)
    {
        setImage(CardImages[number]);
    }
    else if (visible == false)
    {
        setImage(BackCard);
    }
}
}
public void show()
{
    setVisible(true);
}
public void hide()
{
    setVisible(false);
}
public boolean isVisible()
{
    return visible;
}
public void mouseDragging()
{
    if (Greenfoot.mousePressed(this) && !Grab)
    {
        Grab = true;
        return;
    }
    if ((Greenfoot.mouseDragged(this)) && Grab)
    {
        MouseInfo Mouse = Greenfoot.getMouseInfo();
        setLocation(Mouse.getX(), Mouse.getY());
        return;
    }
    if (Greenfoot.mouseDragEnded(this) && Grab)
    {
        Grab = false;
        Grabbed = true; 
        return;
    }
}
public void Deployment()
{
    if(Deployment) return;
    if (!MouseHover && Greenfoot.mouseMoved(this))
        {
            CardImages[number].scale(NormalWidth, NormalHeight); 
            MouseHover = true;
        }
    if (MouseHover && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))
        {
            CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
            MouseHover = false;
        }
    if(MouseHover && Greenfoot.mouseMoved(null) && Greenfoot.mouseMoved(this) && getY() < 400 && Grabbed)
    {
        MouseHover = false;
        CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
        Deployment = true; 
    }

}
public boolean DeploymentBoolean()
{
    return Deployment;
}
public void Position()
{

     if(limit < 8)
    {
       setLocation(130, 300);
       while (isTouching(Cards.class)) move(50);
       CardSounds[number].play();
       limit++;
    }
    
}
public void loseHealth(int damage)
{
    health -= damage;
}
}
This is my player class:
import greenfoot.*;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    boolean cardPossessed[] = new boolean[2];
    //int[] Cards = new int[2];
    private boolean click = false;
    int health = 30;
    SomeDice playerdice = new SomeDice();
    int count = 0;
    int number;
    boolean button;
    int position;
    int CurrentMana;
    int MaxMana;
    int CardValue;
    GreenfootImage[] playerImages ;
    Cards[] playerCards = {new Cards(),new Cards(),new Cards(),new Cards(),new Cards(),new Cards()};

    boolean dep;

    public Player()
    {
         int CurrentMana = 0; 
         int MaxMana = 0;
    }
    
     
   public void CardSelect()
   {
            
               CurrentMana ++;
               MaxMana++; 
               int number = 0;
               int value = Greenfoot.getRandomNumber(6-position);
               if (position == 6) return; 


               Cards card = playerCards[value];
               if(getY() > 400)
               {
               getWorld().addObject(card, 40 + (50*(position+1)^2), getY()+60);
            }
            if(getY() < 400)
            {
                getWorld().addObject(card, 40 + (50*(position+1)^2), getY() -40);
            }
               position++;
               playerCards[value] = playerCards[6-position];
               playerCards[6-position] = card;
               
    }
              
   public void act() 
   {  
       String manaDisplay = Integer.toString(CurrentMana);
       String mana = Integer.toString(MaxMana); 
       getWorld().showText(manaDisplay, 600, 550);
       getWorld().showText("/" + mana, 620, 550);     
       
   }
    
    public void hideCards()
    {
        for (Cards Card : playerCards) Card.hide(); 
    }
 
    public void showCards()
    {
        for (Cards Card : playerCards) Card.show();
    }
  
    }
  

 
        
How do I make it so that a player can only select their own card once the Deployed variable is true?
danpost danpost

2015/3/29

#
Have each player keep a list of their cards.
ProfessionalNoob ProfessionalNoob

2015/4/13

#
Like this?
 public static ArrayList<Cards> PlayerArray = new ArrayList<Cards>();
    public static ArrayList<Cards> Player2Array = new ArrayList<Cards>();
 if(getY() > 285)                             //Determines player position for card positioning.
            {
               getWorld().addObject(card, 40 + (50*(position+1)^2), getY()+60);
               PlayerArray.add(card);
            }
            
            if(getY() < 285)
            {
                getWorld().addObject(card, 40 + (50*(position+1)^2), getY() -40);
                Player2Array.add(card); 
            }
I obviously have to use an arraylist contain statement, but how can I make it so that it does not do anything at all if the card is not in the arraylist? Really sorry for the late response.
danpost danpost

2015/4/13

#
The arraylist should be non-static (one will be assigned to each player, then -- any you will not need to declare the field twice). You can just ask:
if (playerArray.contains(card))
ProfessionalNoob ProfessionalNoob

2015/4/13

#
The cardselect method is in the World class. Now it is true that I can add those cards into the array like so:
  if(getY() > 285)                             //Determines player position for card positioning.
            {
               getWorld().addObject(card, 40 + (50*(position+1)^2), getY()+60);
               PlayerArray.add(card);
            }
            
            if(getY() < 285)
            {
                getWorld().addObject(card, 40 + (50*(position+1)^2), getY() -40);
                PlayerArray.add(card);
            }
But I don't know how to make it so that the other player cannot manipulate the other card while it is in the other's arraylist. This is what's in the world method:
  
    public void act()
    {
        int value = turn%playerCount;
        
        if(Greenfoot.mouseClicked(Player[value]))
        {
            Card.setImage(BackCard);        //Making sure that players cannot see their opponent's hand. 
            Player[turn%playerCount].CardSelect();  //Running the cardselect method.
            Player[turn%2].showCards();         //Shows the card for the current player.
            Player[(turn+1)%2].hideCards();  
            turn++;
            Cards.array.clear();
        }

       
    if(Greenfoot.mouseClicked(Spells[0]))
    {
        Player[0].gainArmor(2); 
    }
    
   
   }
The playerarray.add is in the CardSelect method.
   public void CardSelect()
   {
              
               CurrentMana ++;          //Increasing current and max mana
               MaxMana++; 
              if (position == 6) return;                   //Cannot have more than six cards.
               int value = Greenfoot.getRandomNumber(6-position);        //Value used for cards indexing.

               Cards card = playerCards[value];
            if(getY() > 285)                             //Determines player position for card positioning.
            {
               getWorld().addObject(card, 40 + (50*(position+1)^2), getY()+60);
               PlayerArray.add(card);
            }
            
            if(getY() < 285)
            {
                getWorld().addObject(card, 40 + (50*(position+1)^2), getY() -40);
                PlayerArray.add(card);
            }
            
            position++;                          //Increases value of position.
            playerCards[value] = playerCards[6-position];        //changes value based on position.
            playerCards[6-position] = card;              //Turns array value into a single variable. 
            
          
            if(card.Deployment && Greenfoot.mouseClicked(this))
            {
                loseHealth(card.attack);         //Looses health if card attacks plaer.
            }
                

    }
Really thankful for the help.
You need to login to post a reply.