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

2019/4/17

Selecting a card that is on the slot

1
2
3
danpost danpost

2019/4/19

#
dkalfpqpfjq wrote...
Okay. Is there any way that I can put in and out some value from array? I've never used array in that way. So could you provide an example code please?
Sorry, I meant list, not array. Using a List type object, you have methods to add to and remove from them as needed. Also, since they are Collection type objects, you have method to shuffle and sort them.
dkalfpqpfjq dkalfpqpfjq

2019/4/19

#
Okay. I am sorry that I don't know much about List, so I kept avoid to use it. I tried this in the Slot class:
import java.util.List;
List <Card> cardsOnSlot = getOneObjectAtOffset(getX(), getY(), Card.class);
I recognized that getX() and getY() is not a proper thing to put in there. Does the name inside <> has to be same as the class that is holding this line? I think I cannot use getOneIntersectingObject, because when the mouse is on the card that is not selected yet, it will move up a little, and the card would touch the slot. Could you help me set up the list?
danpost danpost

2019/4/19

#
dkalfpqpfjq wrote...
I tried this in the Slot class: << Code Omitted >> I recognized that getX() and getY() is not a proper thing to put in there. Does the name inside <> has to be same as the class that is holding this line? Could you help me set up the list?
Inside <> should be the type (class name) of those objects the list is to hold. You could use <Object> (since ALL objects are of that generic/root type), but since your list is only to hold Card objects, <Card> would be preferable. Your slot list should start empty and should be modified as you add and remove cards from the slot. You can create an empty list like this:
import java.util.List;
import java.util.ArrayList;
...
List<Card> cardsOnSlot = new ArrrayList<Card>();
A specific Card instance, card, can be:
// added to the list
cardsOnSlot.add(card);
// removed from the list
cardsOnSlot.remove(card);
// or indexed (-1 : not in list; 0 : first in list; 1 : second in list; etc.)
int positionInList = cardsOnSlot.indexOf(card);
dkalfpqpfjq dkalfpqpfjq

2019/4/19

#
Thank you so much for your help! I will try those lines of code in my script.
dkalfpqpfjq dkalfpqpfjq

2019/4/20

#
Sorry, I have another question. In Card class
/**
     * Put the selected card into the empty slot if it is player's turn
     */
    public void select()
    {
        Slot slot = (Slot)getWorld().getObjects(Slot.class).get(0);
        if (slotEmpty1) //go to slot1
        {
            this.setLocation(slotX[0],slotY[1]);
            slot.addCardInList(this); 
            getWorld().addObject(new HealthBar(health, currentHealth), slotX[0], slotY[1]-90);
            //slotEmpty1 = false;
        }
        else if (!slotEmpty1 && slotEmpty2)//!mySlotEmpty1 && mySlotEmpty2) //go to slot2
        {
            this.setLocation(slotX[1],slotY[1]);
            slot.addCardInList(this);
            getWorld().addObject(new HealthBar(health, currentHealth), slotX[1], slotY[1]-90);
            //slotEmpty2 = false;
        }
        else if (!slotEmpty1 && !slotEmpty2 && slotEmpty3)//!mySlotEmpty1 && !mySlotEmpty2 && mySlotEmpty3) //go to slot3
        {
            this.setLocation(slotX[2],slotY[1]);
            slot.addCardInList(this); 
            getWorld().addObject(new HealthBar(health, currentHealth), slotX[2], slotY[1]-90);
            //slotEmpty3 = false; 
        }
        else if (!slotEmpty1 && !slotEmpty2 && !slotEmpty3) //if all full, don't do anything
        {
           
        }
    }
In Slot class
public int positionInList;
List <Card> cardsOnSlot = new ArrayList<Card>();
...
/**
     * Add instance of Card when it is placed on an empty slot
     */
    public void addCardInList(Card card)
    {
        cardsOnSlot.add(card); 
        positionInList = cardsOnSlot.indexOf(card);
    }
    
    /**
     * Remove instance of Card when it dies
     */
    public void removeCardInList(Card card)
    {
        cardsOnSlot.remove(card); 
    }
In Player class
/**
     * Player choose a card on one of the slots to attack bot
     * When the mouse is on the card, it will create yellow border
     */
    public void chooseToAttack()
    {
        if (!keyPressed && Greenfoot.isKeyDown("right"))
        {
            keyPressed = true;
            if (getWorld().getObjects(SelectRectangle.class).isEmpty())
            {
                getWorld().addObject(new SelectRectangle(255,202,24), slotX[0], slotY[1]);
            }
            else
            { 
                SelectRectangle selectRectangle = (SelectRectangle)getWorld().getObjects(SelectRectangle.class).get(0);
                selectRectangle.playerAttack();
            }
        }
        if (keyPressed && !Greenfoot.isKeyDown("right"))
        {
            keyPressed = false; 
        }
        if (!keyPressed && Greenfoot.isKeyDown("up"))
        {
            keyPressed = true;  
            SelectRectangle selectRectangle = (SelectRectangle)getWorld().getObjects(SelectRectangle.class).get(0);
            selectRectangle.playerCardSelected();
            //card is selected;
        }
        if (keyPressed && !Greenfoot.isKeyDown("up"))
        {
            keyPressed = false;
        }
    }
I hope it is what you meant. I wonder if the maximum length of this list will always be 2 (as game goes through, and some cards are removed and some are placed again)?! Also, will it possible to get the health or attack value of that specific card by using the list and index? Currently, the rectangle can only go to the slot that has card on it as I wanted. But I want the rectangle to appear back to slot 1(slotX) when it is at left-end-most slot and the right key is pressed. Thanks for your time and effort.
danpost danpost

2019/4/20

#
In the Slot class code, you do not need lines 1 and 10 (the ones dealing with positionInList). I really do not see any reason why you might need to know where in the list a specific card is. The thing you will at some point need is to see if card is contained within a list or not. You can use the following at such time as you would need it:
if (cardsOnSlot.contains(card))
There is no limit to the size of a List object. You can get its current size with this:
int listSize = cardsOnSlot.size();
If you want to limit it to 3, you could do something like this, for example:
if (cardsOnSlot.size() < 3) cardsOnSlot.add(card);
The list will keep references to the cards. All card states (health, attack, etc.) are kept by the cards themselves. For wrapping the select rectangle, let's see. If you maintain the order of cards on the screen to match the order they occur in the lists, then you could use the modulus operator:
int position = cardsOnSlot.indexOf(card);
// moving selector right (or wrap)
position = (position+1)%listSize;
For wrapping left:
position = (position+listSize-1)%listSize;
dkalfpqpfjq dkalfpqpfjq

2019/4/20

#
I wanted to know where in the list a specific card is. Because I am not sure if, for example, when all the slots are full, and a card on second slot is being removed (die), will the card on the third slot stay in the third in the list and leave second in the list empty? Or will the third in list become second? If that (the former) is the case, I see that I do not need to know the specific location of card in the list. Thank you for the reply.
danpost danpost

2019/4/20

#
dkalfpqpfjq wrote...
I wanted to know where in the list a specific card is. Because I am not sure if, for example, when all the slots are full, and a card on second slot is being removed (die), will the card on the third slot stay in the third in the list and leave second in the list empty? Or will the third in list become second?
When an element is removed from the list, the size of the list goes down. If the list had 3 elements and one is removed, the list will end up with no third element, regardless of which is removed. However, I would think it more important which slot is open, not which element was null (which none would be unless you initialize the list with 3 null elements and use set instead of add. That would just complicate things, though. Two day ago, I threw something together to simulate what I thought you were going for. It takes a shuffled deck and gives 12 to each player. They are added to the world along top and bottom. Single- and double-clicks control the play for both players (I did not do an AI for bot). During an attack, I gave random chance of a card being removed. I can upload it so you can test its play, however I cannot find it in me to just give you the complete source. Just say so, and it will be put up temporarily and if you have questions about how I did something, I will try to explain (possibly with some of the code to help in the explanation).
dkalfpqpfjq dkalfpqpfjq

2019/4/20

#
Ah, okay. I would appreciate if you could let me test your game. Now, for some reason, cardsOnSlot list's size stays 0 at all time. The following codes in Card class and Slot class are used to place card in slot and put it in the list. Card class:
    /**
     * Choose a card to place in the empty slot if it is player's turn.
     */
    private void choose()
    {
        Actor card = getOneObjectAtOffset (getX(), 750, Card.class);
        if (Greenfoot.mouseMoved(card))
        {
            onThis = Greenfoot.mouseMoved(this);
        }
        if (onThis && !selected)
        {
            setLocation(this.getX(),633);
            // SelectRectangle sr = (SelectRectangle)getWorld().getObjects(SelectRectangle.class);
            // if (sr != null)
            // {
                // getWorld().removeObject(sr);
            // }
        }
        else if (!onThis && !selected)
        {
            setLocation(this.getX(),750);
        }
        if (Greenfoot.mouseClicked(this))
        {
            select();
            Slot slot = (Slot)getWorld().getObjects(Slot.class).get(0);
            slot.addCardInList(this); 
            myTurn = false;
            selected = true;
        }
    } 
    
    /**
     * Put the selected card into the empty slot if it is player's turn
     */
    public void select()
    {
        if (slotEmpty1) //go to slot1
        {
            this.setLocation(slotX[0],slotY[1]); 
            getWorld().addObject(new HealthBar(health, currentHealth), slotX[0], slotY[1]-90);
        }
        else if (!slotEmpty1 && slotEmpty2)//!mySlotEmpty1 && mySlotEmpty2) //go to slot2
        {
            this.setLocation(slotX[1],slotY[1]);
            getWorld().addObject(new HealthBar(health, currentHealth), slotX[1], slotY[1]-90);
        }
        else if (!slotEmpty1 && !slotEmpty2 && slotEmpty3)//!mySlotEmpty1 && !mySlotEmpty2 && mySlotEmpty3) //go to slot3
        {
            this.setLocation(slotX[2],slotY[1]);
            getWorld().addObject(new HealthBar(health, currentHealth), slotX[2], slotY[1]-90);
        }
        else if (!slotEmpty1 && !slotEmpty2 && !slotEmpty3) //if all full, don't do anything
        {
           
        }
    }
Slot class:
List <Card> cardsOnSlot = new ArrayList<Card>();
    public int cardListSize = cardsOnSlot.size();
    public int cardPosition;
    List <Bot> botsOnSlot = new ArrayList<Bot>();
    public int botListSize = botsOnSlot.size();
    public int botPosition;
...
/**
     * Add instance of Card when it is placed on an empty slot
     */
    public void addCardInList(Card card)
    {
        cardsOnSlot.add(card); 
        cardPosition = cardsOnSlot.indexOf(card);
    }
    
    /**
     * Remove instance of Card when it dies
     */
    public void removeCardInList(Card card)
    {
        cardsOnSlot.remove(card); 
    }
    
    /**
     * Add instance of Bot when it is placed on an empty slot
     */
    public void addBotInList(Bot bot)
    {
        botsOnSlot.add(bot); 
        botPosition = botsOnSlot.indexOf(bot); 
        //botPositionInList = botsOnSlot.indexOf(bot);
    }
    
    /**
     * Remove instance of Card when it dies
     */
    public void removebotInList(Bot bot)
    {
        botsOnSlot.remove(bot); 
    }
The following codes in Player class and SelectRectangle class are used to move select rectangle while player is choosing card to attack. Player class:
/**
     * Player choose a card on one of the slots to attack bot
     * When the mouse is on the card, it will create yellow border
     */
    public void chooseToAttack()
    {
        if (!keyPressed && Greenfoot.isKeyDown("right"))
        {
            keyPressed = true;
            if (getWorld().getObjects(SelectRectangle.class).isEmpty())
            {
                getWorld().addObject(new SelectRectangle(255,202,24), slotX[0], slotY[1]);
            }
            else
            { 
                SelectRectangle selectRectangle = (SelectRectangle)getWorld().getObjects(SelectRectangle.class).get(0);
                selectRectangle.playerAttack();
            }
        }
        if (keyPressed && !Greenfoot.isKeyDown("right"))
        {
            keyPressed = false; 
        }
        if (!keyPressed && Greenfoot.isKeyDown("up"))
        {
            keyPressed = true;  
            SelectRectangle selectRectangle = (SelectRectangle)getWorld().getObjects(SelectRectangle.class).get(0);
            selectRectangle.playerCardSelected();
            //card is selected;
        }
        if (keyPressed && !Greenfoot.isKeyDown("up"))
        {
            keyPressed = false;
            chooseBotToAttack();
        }
    }
SelectRectangle class
/**
     * Move to right slot when selecting a card.
     */
    public void playerAttack()
    {
        // int i =0;  
        // this.setLocation(slotX[i],slotY[1]);
        // while (getObjectsInRange(0, Card.class).isEmpty())
        // {
             // this.setLocation(slotX[i+1], slotY[1]);
        // }
        // if (i > 2)
        // {
            // this.setLocation(slotX[0], slotY[1]);
        // }
        // i++; 
        Slot slot = (Slot)getWorld().getObjects(Slot.class).get(0);
        int position = slot.cardPosition;
        int listSize = slot.cardListSize;
        if (position > -1)
        {
            position = (position+1)%listSize;
            this.setLocation(slotX[position],slotY[1]);
        }
    }
    
   /**
     * Tell player card that it is selected to attack
     * Haven't worked on it yet
     */
    public void playerCardSelected()
    {
        int x = this.getX();//when 'up' pressed, rectangle location is the card's location
        int y = this.getY();
        Actor card =getOneObjectAtOffset(x,y,Card.class);
        //need to reference to this selected card's attack points
    }
And when I press right arrow key, it will soon throw me this error message. java.lang.ArithmeticException: / by zero at SelectRectangle.playerAttack(SelectRectangle.java:64) at Player.chooseToAttack(Player.java:75) at Player.checkTurn(Player.java:51) at Player.act(Player.java:42)
position = (position+1)%listSize;
I think the error is because listSize is 0.
danpost danpost

2019/4/20

#
dkalfpqpfjq wrote...
Ah, okay. I would appreciate if you could let me test your game.
Okay -- it is here.
danpost danpost

2019/4/20

#
dkalfpqpfjq wrote...
Now, for some reason, cardsOnSlot list's size stays 0 at all time.
Lines 2 and 5 in the Slot class should be local variables (inside of methods without the public). Uhh, wait ... why is there is list of Bot objects in the Slot class? Here is some of the basics of what I did in my demo. (1) Bot and Man both extend Player, which extends Actor; (2) MyWorld contains an 2-element array of Player objects -- one Man and one Bot object; (3) Player contains 2 List objects -- one for cards not on slot and one for cards on slot; (4) Card controls hover-highlight and MyWorld controls both selected-highlight and Player images highlighting slot area;
dkalfpqpfjq dkalfpqpfjq

2019/4/20

#
I like how your game can be played with mouse! Thanks for uploading it! I think my game coding would be easier if I can control it with mouse only. The reason why I have a list of Bot objects in the Slot class is to keep track of which card bot has place in the slot. So I can reference its health and attack points.
dkalfpqpfjq dkalfpqpfjq

2019/4/20

#
danpost wrote...
Here is some of the basics of what I did in my demo. (1) Bot and Man both extend Player, which extends Actor; (2) MyWorld contains an 2-element array of Player objects -- one Man and one Bot object; (3) Player contains 2 List objects -- one for cards not on slot and one for cards on slot; (4) Card controls hover-highlight and MyWorld controls both selected-highlight and Player images highlighting slot area;
I will make a new scenario quickly and try these. (since my code is very messy anyway) Thank you so much!
danpost danpost

2019/4/21

#
I just added health and damage to my demo and now remove cards properly.
dkalfpqpfjq dkalfpqpfjq

2019/4/21

#
danpost wrote...
I just added health and damage to my demo and now remove cards properly.
I really like your game! It is basically what I am trying to do except the player fights against the computer. Would you like to see what I've got so far? I only got place card in empty slot and hover-highlight done so far. I think it would be easier for you to look through.
There are more replies on the next page.
1
2
3