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?import java.util.List; List <Card> cardsOnSlot = getOneObjectAtOffset(getX(), getY(), Card.class);
import java.util.List; import java.util.ArrayList; ... List<Card> cardsOnSlot = new ArrrayList<Card>();
// 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);
/** * 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 { } }
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); }
/** * 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; } }
if (cardsOnSlot.contains(card))
int listSize = cardsOnSlot.size();
if (cardsOnSlot.size() < 3) cardsOnSlot.add(card);
int position = cardsOnSlot.indexOf(card); // moving selector right (or wrap) position = (position+1)%listSize;
position = (position+listSize-1)%listSize;
/** * 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 { } }
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); }
/** * 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(); } }
/** * 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 }
position = (position+1)%listSize;