Andriod应用与游戏开发纸牌小游戏实验报告Word文件下载.docx
《Andriod应用与游戏开发纸牌小游戏实验报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《Andriod应用与游戏开发纸牌小游戏实验报告Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。
publicbooleancanmove=false;
//判断当前卡片是否可以拖拽
publicCard(CardMainm){
this.m=m;
this.setSize(71,96);
this.setVisible(true);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
publicCard(CardMainm,Stringname,Stringflag){
this.name=name;
if(flag=="
front"
)
this.turnFront();
else{
this.turnRear();
}
publicCardgetNext(Pointpoint){
Cardcard=null;
point.y+=m.min;
card=(Card)m.table.get(point);
if(card!
=null)
returncard;
point.y+=m.max-m.min;
returncard;
publicvoidmoveto(Pointfrom,Pointto){
Pointto2=newPoint();
to2=to;
Cardcard=this.getNext(from);
m.table.remove(from);
this.setLocation(to2);
m.table.put(to2,this);
card.moveto(card.getLocation(),newPoint(to2.x,to2.y+m.max));
//递归
publicvoidmoving(intx,inty)//拖拽事件
{
for(Cardcard:
m.dragList)
{
Pointp=card.getLocation();
m.table.remove(p);
p.x+=x;
p.y+=y;
card.setLocation(p);
m.container.setComponentZOrder(card,0);
m.table.put(p,card);
//Cardcard=this.getNext(this.getLocation());
//解决某个bug
//if(this.getCol()==currentCol)
//System.out.println(this.getCol()+"
:
"
+currentCol);
//Pointp=this.getLocation();
/*m.table.remove(p);
p.x+=x;
p.y+=y;
this.setLocation(p);
m.container.setComponentZOrder(this,0);
m.table.put(p,this);
card.moving(x,y);
//递归*/
publicvoidturnFront(){
this.setIcon(newImageIcon("
images/"
+name+"
.gif"
));
this.up="
publicvoidturnRear(){
images/rear.gif"
//错误地方回滚
publicvoidrollback(MouseEvente){
Cardcard=this;
Pointlocal=card.getLocation();
this.moveto(local,oldPoint);
//System.out.println(this.getLocation().toString());
//获取当前卡片是第几列(1-7)
publicintgetCol(){
PointnowPoint=this.getLocation();
intxx=nowPoint.x,col=0;
for(inti=1;
i<
=7;
i++){
if(Math.abs(xx-50-100*(i-1))<
80){
col=i;
break;
}
returncol;
publicCardgetLastCard(){
Cardcard=null,lastcard=null;
intcol=this.getCol();
//当前列
intx=50+100*(col-1),y=180;
//获取第一个
card=m.table.get(newPoint(x,y));
lastcard=card;
while(card!
=null){
lastcard=card;
card=card.getNext(card.getLocation());
returnlastcard;
publicbooleanCheck(){
//检查范围
CardlastCard=this.getLastCard();
intx=-1,y=-1;
if(lastCard!
x=Math.abs(this.getLocation().x-lastCard.getLocation().x);
y=Math.abs(this.getLocation().y-lastCard.getLocation().y);
if(!
((x>
=0)&
&
(x<
40)&
(y>
(y<
40))){
System.out.println("
范围出错"
);
returnfalse;
//检查花色
//黑1红2梅3方4
Stringcur=this.name,last=lastCard.name;
intcolor1=Integer.valueOf(cur.substring(0,1));
intcolor2=Integer.valueOf(last.substring(0,1));
//System.out.println("
color:
+color1+"
"
+color2);
if(Math.abs(color1-color2)%2==0){
colorwrong"
//检查大小
intnum1=Integer.valueOf(cur.substring(2,cur.length()));
intnum2=Integer.valueOf(last.substring(2,last.length()));
num:
+num1+"
+num2);
if(num2-num1!
=1){
num1-num2!
=1"
//檢查牌是否翻開
if(lastCard.up=="
){
lastcardup==rear"
returntrue;
//判断当前是不是待发牌堆
publicbooleanisCards(){
if(this.getLocation().x==600&
this.getLocation().y==30){
returntrue;
}else
//判断当前是不是在4个框框内
publicbooleanisFour(){
Pointpoint=this.getLocation();
if(this.getNext(this.getLocation())!
x=Math.abs(point.x-(50+100*(this.getCol()-1)));
y=Math.abs(point.y-30);
if((x>
=0&
x<
=40)&
y<
=40)){
System.out.println(x+"
+y);
}else{
//放入框框
publicbooleanlocateFour(){
//框框是空的时候只能放A
Pointpoint=newPoint(50+100*(this.getCol()-1),30);
card=m.table.get(point);
if(card==null){
Strings=this.name.substring(2,this.name.length());
if(!
s.equals("
1"
)){
System.out.println(s+"
不是A"
returnfalse;
//不是A
m.table.remove(this.getLocation());
this.setLocation(point);
System.out.println(point.toString());
m.table.put(point,this);
//说明//框框不是空的时候由小到大,相同花色
Strings1=this.name.substring(0,1);
Strings2=card.name.substring(0,1);
intnum1=Integer.valueOf(this.name.substring(2,
this.name.length()));
intnum2=Integer.valueOf(card.name.substring(2,
card.name.length()));
((s1.equals(s2))&
(num1-num2==1)))
{
System.out.println("
花色不同"
else{
m.table.remove(this.getLocation());
this.setLocation(point);
System.out.println(point.toString());
m.table.put(point,this);
returntrue;
publicbooleanisK(){
Pointpoint=this.getLocation();
y=Math.abs(point.y-180);
if(this.getLastCard()!
=null)
当前K下面不为空"
=40))){
区域错误"
Stringstring=this.name.substring(2,this.name.length());
if(!
string.equals("
13"
))
不是K"
Pointpoint2=newPoint((50+100*(this.getCol()-1)),180);
this.moveto(this.getLocation(),point2);
@Override
publicvoidmouseDragged(MouseEvente){
//TODOAuto-generatedmethodstub
if(this.canmove){
Cardcard=this;
Pointnow=e.getPoint();
intx,y;
x=now.x-point.x;
y=now.y-point.y;
moving(x,y);
publicvoidmousePressed(MouseEvente){
point=e.getPoint();
oldPoint=this.getLocation();
currentCol=this.getCol();
m.dragList=newArrayList<
Card>
();
if(this.canmove)
m.dragList.add(this);
Cardcard=this.getNext(this.getLocation());
while(card!
list:
+card.name);
if(card.canmove)
m.dragList.add(card);
card=card.getNext(card.getLocation());
publicvoidmouseReleased(MouseEvente){
=null&
lastCard.canmove==false){
if(lastCard.up=="
&
lastCard==this){
lastCard.canmove=true;
lastCard.turnFront();
}else{
rollback(e);
if(this.canmove)//如何能移动
//当移动最上面4个框框时
if(this.isFour()&
this.locateFour())//将放入四个框框
if(oldPoint.x==700&
oldPoint.y==30){
m.list.remove(this);
//从牌堆抽出
}
}elseif(this.isK())//当白框框时,且当前为K
elseif(Check())//检查合法性
this.moveto(
this.getLocation(),
newPoint(lastCard.getLocation().x,lastCard
.getLocation().y+m.max));
}else{
this.rollback(e);
//恢复
if(this.isCards())//当前是牌堆
if(this.name==null)//说明点到框框了
for(Cardcard:
m.list){
card.setLocation(newPoint(600,30));
card.turnRear();
card.canmove=false;
this.setLocation(newPoint(700,30));
m.container.setComponentZOrder(this,0);
this.turnFront();
this.canmove=true;
以上是规则代码,以及一些重要步骤的解析。