基于java开发的蜘蛛纸牌程序设计(含源文件) 下载本文

this.pane.setComponentZOrder(cards[c + i], 1);

Point point = new Point(lastPoint);

if (cards[c + i].getCardValue() == 1){

int n = cards[c + i].whichColumnAvailable(point); point.y -= 240;

PKCard card = (PKCard) this.table.get(point); if (card != null && card.isCardCanMove()){

this.haveFinish(n);\\\\判断纸牌是否可以回收以及回收的总次数

} }

x += 101; }

c += 10; }

4.3.4纸牌的移动以及放置

代码实现如下: 用鼠标拖动纸牌

public void mouseDragged(MouseEvent arg0){ if (canMove){

21

int x = 0; int y = 0;

Point p = arg0.getPoint(); x = p.x - point.x; y = p.y - point.y; this.moving(x, y); } }

方法:放置纸牌

public void setNextCardLocation(Point point){ PKCard card = main.getNextCard(this); if (card != null){

if (point == null){

card.setNextCardLocation(null);

main.table.remove(card.getLocation()); card.setLocation(card.initPoint); main.table.put(card.initPoint, card); } else{

point = new Point(point); point.y += 20;

card.setNextCardLocation(point); point.y -= 20;

main.table.remove(card.getLocation()); card.setLocation(point);

main.table.put(card.getLocation(), card); card.initPoint = card.getLocation(); } } }

4.3.5显示当前纸牌可行的操作: 代码实现如下: 返回值:void

方法:显示可移动的操作

public void showEnableOperator(){ int x = 0;

out: while (true){

Point point = null; PKCard card = null; do{

if (point != null){ n++;

22

}

point = this.getLastCardLocation(n); while (point == null){

point = this.getLastCardLocation(++n); if (n == 10) n = 0; x++;

if (x == 10) break out; }

card = (PKCard) this.table.get(point); }

while (!card.isCardCanMove());

while (this.getPreviousCard(card) != null

&& this.getPreviousCard(card).isCardCanMove()){ card = this.getPreviousCard(card); }

if (a == 10){ a = 0; }

for (; a < 10; a++){ if (a != n){

Point p = null; PKCard c = null; do{

if (p != null){ a++; }

p = this.getLastCardLocation(a); int z = 0;

while (p == null){

p = this.getLastCardLocation(++a); if (a == 10) a = 0; if (a == n) a++; z++;

if (z == 10) break out; }

c = (PKCard) this.table.get(p); }

while (!c.isCardCanMove());

if (c.getCardValue() == card.getCardValue() + 1){ card.flashCard(card); try{

Thread.sleep(800); }

catch (InterruptedException e){

23

e.printStackTrace(); }

c.flashCard(c); a++;

if (a == 10){ n++; }

break out; } } } n++;

if (n == 10){ n = 0; }

x++;

if (x == 10){ break out; } } }

4.3.6回收纸牌:

代码实现如下: 返回值:void

方法:判断纸牌的摆放是否完成

public void haveFinish(int column){

Point point = this.getLastCardLocation(column); PKCard card = (PKCard) this.table.get(point); do{

this.table.remove(point);

card.moveto(new Point(20 + finish * 10, 580)); //将组件移动到容器中指定的顺序索引。 pane.setComponentZOrder(card, 1); //将纸牌新的相关信息存入Hashtable

this.table.put(card.getLocation(), card);

24