15. 翻牌的條件規則
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();
}
}
}
}
}
45. 拖拉開始時,開始移動
(這裏蓋掉之前寫的前兩行)
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()) {
…
}
}
}
}
46. 拖拉開始時,開始移動
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);
}
}
}
47. 拖拉開始時,開始移動
public class MovingPile extends Pile
{
/** 撲克牌是由哪一疊移動的。 */
Pile from = null;
/**
* 建立一疊新的移動牌疊。
*
* @param from 撲克牌是由哪一疊移動的。
*/
public MovingPile(Pile from)
{
this.from = from;
}
}