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?

1 2 | import java.util.List; List <Card> cardsOnSlot = getOneObjectAtOffset(getX(), getY(), Card. class ); |
1 2 3 4 | import java.util.List; import java.util.ArrayList; ... List<Card> cardsOnSlot = new ArrrayList<Card>(); |
1 2 3 4 5 6 | // 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); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** * 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 { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /** * 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 ; } } |
1 | if (cardsOnSlot.contains(card)) |
1 | int listSize = cardsOnSlot.size(); |
1 | if (cardsOnSlot.size() < 3 ) cardsOnSlot.add(card); |
1 2 3 | int position = cardsOnSlot.indexOf(card); // moving selector right (or wrap) position = (position+ 1 )%listSize; |
1 | position = (position+listSize- 1 )%listSize; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /** * 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 { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * 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 } |
1 | position = (position+ 1 )%listSize; |