solitaire with greenfoot #4

Download Solitaire with Greenfoot #4

If you can't read please download the document

Upload: imacat-

Post on 16-Apr-2017

822 views

Category:

Technology


1 download

TRANSCRIPT

1

Greenfoot

[email protected]/6/29

CC Attribution-ShareAlike 3.0 Unported

solitaire-4

Cardact()Pileact()

flipNextCard()

public class Card extends Actor

{

public void act()

{

//

if (!isFaceUp) {

if (Greenfoot.mouseClicked(this) && this == pile.getTopCard()) {

if (pile instanceof UnflippedPile) {

((UnflippedPile) pile).flipNextCard();

} else if (pile instanceof WorkingPile) {

((WorkingPile) pile).flipTopCard();

}

}

}

}

}

instanceof

WorkingPile x instanceof WorkingPile

WorkingPile x instanceof Pile

Card y instanceof Pile?

((WorkingPile) pile).flip()

pilePilePileflipTopCard()WorkingPileflipTopCard()

pileWorkingPileinstanceof

(WorkingPile) pile

flipTopCard()

public class UnflippedPile extends Pile

{

public void act()

{

//

if (Greenfoot.mouseClicked(this))

{

Table table = (Table) getWorld();

table.getFlippedPile().returnAllCards();

}

}

}

moveToWorking()

subclassMovingPile

act()

MovingPile

public class MovingPile extends Pile

{

/**

*

*/

public void act()

{

MouseInfo mouse = Greenfoot.getMouseInfo();

setLocation(mouse.getX(), mouse.getY());

}

}

MovingPile
Greenfoot 2.1.1

public class MovingPile extends Pile

{

/**

*

*/

public void act()

{

MouseInfo mouse = Greenfoot.getMouseInfo();

If (mouse != null) {

setLocation(mouse.getX(), mouse.getY());

}

}

}

MouseInfo

MouseInfo

Greenfoot APIMouseInfo

MouseInfo

getX()getY()

MovingPile

startMoving()Pile


public class Card extends Actor

{

private boolean isDragging = false;

public void act()

{

//

if (isFaceUp) {

if (!isDragging && Greenfoot.mouseDragged(this)) {

isDragging = true;

pile.startMoving(this);

}

//

} else {

if (Greenfoot.mouseClicked(this) && this == pile.getTopCard()) {

}

}

}

}

public class Pile extends Actor

{

public void startMoving(Card starCard)

{

int index = -1;

for (int i = 0; i < getSize(); i++) {

if (getCard(i) == starCard) {

index = i;

break;

}

}

if (index == -1) {

return;

}

MovingPile target = new MovingPile(this);

MouseInfo mouse = Greenfoot.getMouseInfo();

getWorld().addObject(target, mouse.getX(), mouse.getY());

while (index < getSize()) {

Card card = takeCard(index);

target.addCard(card);

}

}

}

public class MovingPile extends Pile

{

/** */

Pile from = null;

/**

*

*

* @param from

*/

public MovingPile(Pile from)

{

this.from = from;

}

}

public class MovingPile extends Pile

{

/**

*

*/

public void act()

{

MouseInfo mouse = Greenfoot.getMouseInfo();

setLocation(mouse.getX(), mouse.getY());

redrawCards();

}

}

public class MovingPile extends Pile

{

/**

*

*

*/

public void redrawCards()

{

for (int i = 0; i < getSize(); i++)

{

getCard(i).setLocation(getX(), getY() + 3 + 12 * i);

}

}

}

public class Card extends Actor {

public void act() {

if (isFaceUp) {

if (!isDragging && Greenfoot.mouseDragged(this)) {

} else if (isDragging && Greenfoot.mouseDragEnded(this)) {

isDragging = false;

MovingPile movingPile = (MovingPile) pile;

Pile target = movingPile.getNowOnPile();

if (target == null) {

movingPile.returnCards();

} else if (!target.isAcceptCard(this)) {

movingPile.returnCards();

} else {

movingPile.moveTo(target);

}

}

}

}

}

import java.util.List;

public class MovingPile extends Pile

{

/**

*

*

* @return null

*/

public Pile getNowOnPile()

{

MouseInfo mouse = Greenfoot.getMouseInfo();

List piles = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), Pile.class);

piles.remove(this); // MovingPile

if (piles.size() == 0) {

return null;

}

return piles.get(0);

}

}

isAcceptCard()

WorkingPileResultPileisAcceptCard()

PileisAcceptCard()WorkingPileResultPileoverrideisAcceptCard()

isAcceptCard()

public class Pile extends Actor

{

/**

*

*

* @param card

* @return

*/

public boolean isAcceptCard(Card card)

{

return false;

}

}

returnCards()moveTo()

MovingPile

takeBottomCard()

public class MovingPile extends Pile

{

/**

* null

*

* @return

*/

public Card takeBottomCard()

{

if (getSize() == 0)

{

return null;

}

return takeCard(0);

}

}

returnCards()moveTo()

returnCards()

public class MovingPile extends Pile

{

/**

*

*

*/

public void returnCards()

{

while (getSize() > 0)

{

Card card = takeBottomCard();

from.addCard(card);

}

getWorld().removeObject(this);

}

}

moveTo()

public class MovingPile extends Pile

{

/**

*

*

* @param target

*/

public void moveTo(Pile target)

{

while (getSize() > 0)

{

Card card = takeBottomCard();

target.addCard(card);

}

getWorld().removeObject(this);

}

}

solitaire-5