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

2015/3/16

Strange problem with returning integer

ProfessionalNoob ProfessionalNoob

2015/3/16

#
I was wondering if anyone would help me understand this. I have a method called Generation() in my cards class that basically generates an image for the card and returns an integer. In my player class, I am trying to test what values it returns. So I did a simple showtext to check what number it returns. Code in my player class:
public void CardSelect()
    {
               CurrentMana ++;
               MaxMana++; 
               int number = 0;

               if (position == 6) return; 
               int value = Greenfoot.getRandomNumber(6-position);

               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;
               int ShowInt = card.Generation(); 
               String showInt = Integer.toString(ShowInt);
               getWorld().showText(showInt, 500, 400); 
              
                
 public int Generation()
   {

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

   if(getImage().equals(CardImages[0]))
    {
      cost = 0;
      attack = 1;
      health = 1;
      return 1;
    }
  if(getImage().equals(CardImages[1]))
  {
      cost = 7;
      attack = 9;
      health = 5;
      return 2;
    }
  if(getImage().equals(CardImages[2]))
  {
     cost = 6;
     attack = 6;
     health = 7;
     return 3;
  }
  if(getImage().equals(CardImages[3]))
  {
      cost = 2;
      attack = 3;
      health = 2;
      return 4;
  }
  if(getImage().equals(CardImages[4]))
  {
      cost = 1;
      attack = 2;
      health = 1;
      return 5;
  }
  if(getImage().equals(CardImages[5]))
  {
      cost = 8;
      attack = 4;
      health = 9;
      return 6;
  }
  return 0;
}
The method that generates a random integer. The problem is that it shows a different number for the exact same image when I generate my cards. This is really strange and I cannot understand why it would do that.
danpost danpost

2015/3/17

#
Line 8 in the player class is supposed to determine which of the 6 cards that is being chosen. The 'Generation' method should accept that value and use it to set up the proper field values for the card:
public void generate(int cardNum)
{
    setImage(CardImages[cardNum]);
    getImage().scale(NomalWidth/2, NormalHeight/2);
    switch(cardNum)
    {
        case 0:
            cost = 8;
            attack = 4;
            health = 9;
            break;
        case 1:
            cost = 0;
            attack = 1;
            health = 1;
            break;
        case 2:
            cost = 7;
            attack = 9;
            health = 5;
            break;
        case 3:
            cost = 6;
            attack = 6;
            health = 7;
            break;
        case 4:
            cost = 2:
            attack = 3;
            health = 2;
            break;
        case 5:
            cost = 1;
            attack = 2;
            health = 1;
            break;
    }
}
If you are actually showing a "rolling" dice, just tell it what number to land on instead of having it return a random value. I cannot be sure if the above will work or not as you did not post the entire player class code (it does not show what is done as far as the playerCards array being created and filled).
danpost danpost

2015/3/17

#
Easier would be to use static fields for the images and values and just have the Card class create each specific card as needed:
import greenfoot.*;

public class Card extends Actor
{
    public static final GreenfootImage[] IMAGES =
    {
        new GreenfootImage("imageOne.png"),
        new GreenfootImage("imageTwo.png"),
        new GreenfootImage("imageThree.png"),
        new GreenfootImage("imageFour.png"),
        new GreenfootImage("imageFive.png"),
        new GreenfootImage("imageSix.png")
    };
    public static final int[][] VALUES =
    {
        { 0, 1, 1}, { 8, 4, 9 }, { 0, 1, 1 }, { 7, 9, 5 }, { 6, 6, 7 }, { 2, 3, 2 }
    };
    public static final int NormalWidth = ???; // replace '???' with appropriate number
    public static final int NormalHeight = ???; // replace '???' with appropriate number

    private int cost;
    private int attack;
    private int health;

    public Card(int cardNum)
    {
        setImage(IMAGES[cardNum]);
        getImage().scale(NormalWidth/2, NormalHeight/2);
        cost = VALUES[cardNum][0];
        attack = VALUES[cardNum][1];
        health = VALUES[cardNum][2];
    }

    // place your other methods here
}
With the above beginnings of a Card class, you can easily fill your 'playerCards' array with this in the class of the player:
for (int n=0; n<6; n++) playerCards[n] = new Card(n);
Of course, the image filenames will need corrected.
You need to login to post a reply.