import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Chess implements java.io.Serializable
{
private int width,height; //棋盘的高度
private int [][] chessarray; //棋盘每个点的状态
private int []whitex; //白棋横坐标数组
private int []whitey; //白棋纵坐标数组
private int []blackx; //黑棋横坐标数组
private int []blacky; //黑棋纵坐标数组
private int whitextop; //白棋横坐标栈顶
private int whiteytop; //白棋纵坐标栈顶
private int blackxtop; //黑棋横坐标栈顶
private int blackytop; //黑棋纵坐标栈顶
private boolean chesscolor;
public Chess(int width,int height)
{
this.width=width;
this.height=height;
this.chessarray=new int[width+3][height+3];
this.whitex=new int[width*height/2+1];
this.whitey=new int[height*height/2+1];
this.blackx=new int[width*height/2+1];
this.blacky=new int[height*height/2+1];
this.whitextop=-1;
this.whiteytop=-1;
this.blackxtop=-1;
this.blackytop=-1;
this.chesscolor=true;
}
public void setarray(int x,int y,int value) //设置状态
{
this.chessarray[x][y]=value;
}
public int [][]getarray() //获得状态
{
return chessarray;
}
public int getheight()
{
return height;
}
public int getwidth()
{
return width;
}
public boolean isplay(int x,int y) //是否可以下棋
{
if(x<0||y<0)
return false;
if(chessarray[x][y]==0)
{
return true;
}
else
return false;
}
public void play(int x,int y) //下棋
{
int state=(chesscolor==true?2:1);
if(this.isplay(x, y))
{
this.setarray(x,y,state);
if(state==1)
{
whitex[++whitextop]=x;
whitey[++whiteytop]=y; //堆栈
}
if(state==2)
{
blackx[++blackxtop]=x;
blacky[++blackytop]=y;
}
chesscolor=!chesscolor;
}
}
public boolean display() //悔棋
{
boolean disappear=false;
for(int i=0;i<2;i++)
{
if((blackxtop>whitextop)&&(blackxtop>=0||whitextop>=0))
{
int x=blackx[blackxtop--];
int y=blacky[blackytop--];
this.setarray(x, y,0);
disappear=true;
}
else
if((blackxtop==whitextop)&&(blackxtop>=0||whitextop>=0))
{
int x=whitex[whitextop--];
int y=whitey[whiteytop--];
this.setarray(x,y,0);
disappear=true;
}
}
return disappear;
}
public boolean getchesscolor() //获得当前棋子的颜色
{
return this.chesscolor;
}
public boolean iscanplay(int x,int y,boolean chessColor) //判断是否禁手
{
int number=0;
int count=0;
if(iscanplayone(x,y,chessColor,3))
number++;
if(iscanplayone(x,y,chessColor,4))
count++;
if(iscanplaytwo(x,y,chessColor,3))
number++;
if(iscanplaytwo(x,y,chessColor,4))
count++;
if(number==2||count==2)
return true;
if(iscanplaythree(x,y,chessColor,3))
number++;
if(iscanplaythree(x,y,chessColor,4))
count++;
if(number==2||count==2)
return true;
if(iscanplayfour(x,y,chessColor,3))
number++;
if(iscanplayfour(x,y,chessColor,4))
count++;
if(number==2||count==2)
return true;
return false||issix(x,y,chessColor);
}
public boolean iscanplayone(int x,int y,boolean chessColor,int count) //上方和下方禁手
{
if(chessColor)
{
int tempx=x;
int tempy=y;
int number=1;
for(int i=1;i<count+1;i++)
{
tempy-=1;
if(tempy<0)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
tempy=y;
for(int i=1;i<count+1;i++)
{
tempy+=1;
if(tempy>this.height)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
if(number==count)
return true;
}
return false;
}
public boolean iscanplaytwo(int x,int y,boolean chessColor,int count) //左上方和右下方禁手
{
if(chessColor)
{
int tempx=x;
int tempy=y;
int number=1;
for(int i=1;i<count+1;i++)
{
tempx-=1;
tempy-=1;
if(tempy<0||tempx<0)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
tempx=x;
tempy=y;
for(int i=1;i<count+1;i++)
{
tempx+=1;
tempy+=1;
if(tempy>this.height||tempx>this.width)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
if(number==count)
return true;
}
return false;
}
public boolean iscanplaythree(int x,int y,boolean chessColor,int count) //左方和右方禁手
{
if(chessColor)
{
int tempx=x;
int tempy=y;
int number=1;
for(int i=1;i<count+1;i++)
{
tempx-=1;
if(tempx<0)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
tempx=x;
for(int i=1;i<count+1;i++)
{
tempx+=1;
if(tempx>this.width)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
if(number==count)
return true;
}
return false;
}
public boolean iscanplayfour(int x,int y,boolean chessColor,int count) //左下方和右上方禁手
{
if(chessColor)
{
int tempx=x;
int tempy=y;
int number=1;
for(int i=1;i<count+1;i++)
{
tempx-=1;
tempy+=1;
if(tempy>this.height||tempx<0)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
tempx=x;
tempy=y;
for(int i=1;i<count+1;i++)
{
tempx+=1;
tempy-=1;
if(tempy<0||tempx>this.width)
break;
if(this.chessarray[tempx][tempy]==2)
number++;
else
break;
}
if(number==count)
return true;
}
return false;
}
public boolean isSuccess(int x,int y,boolean chessColor) //判断是否胜利
{
int number=1;
int chessvalue;
int tempx=x;
int tempy=y;
if(chessColor)
chessvalue=2;
else
chessvalue=1;
for(int i=1;i<6;i++) //判断左方和右方
{
tempx+=1;
if(tempx>this.width)
break;
if(this.chessarray[tempx][tempy]==chessvalue)
number++;
else
break;
}
if(number==5)
{
return true;
}
tempx=x;
for(int i=1;i<6;i++)
{
tempx-=1;
if(tempx<0)
break;
if(this.chessarray[tempx][tempy]==chessvalue)
number++;
else
break;
}
if(number==5)
{
return true;
}
number=1;
tempx=x;
for(int i=1;i<6;i++) //判断上方和下方
{
tempy+=1;
if(tempy>this.height)
break;
if(this.chessarray[tempx][tempy]==chessvalue)
number++;
else
break;
}
if(number==5)
{
return true;
}
tempy=y;
for(int i=1;i<6;i++)
{
tempy-=1;
if(tempy<0)
break;
if(this.chessarray[tempx][tempy]==chessvalue)
number++;
else
break;
}
if(number==5)
{
return true;
}
number=1;
tempy=y;
for(int i=1;i<6;i++) //判断左上方和右下方
{
tempy-=1;
tempx-=1;
if(tempy<0||tempx<0)
break;
if(this.chessarray[tempx][tempy]==chessvalue)
number++;
else
break;
}
if(number==5)
{
return true;
}
tempx=x;
tempy=y;
for(int i=1;i<6;i++)
{
tempy+=1;
tempx+=1;
if(tempy<0||tempx>width)
break;
if(this.chessarray[tempx][tempy]==chessvalue)
number++;
else
break;
}
if(number==5)
{
return true;
}
number=1;
tempx=x;
tempy=y;
for(int i=1;i<6;i++) //判断左下方和右上方
{
tempy+=1;
tempx-=1;
if(tempy>height||tempx<0)
break;
if(this.chessarray[tempx][tempy]==chessvalue)
number++;
else
break;
}
if(number==5)
{
return true;
}
tempx=x;
tempy=y;
for(int i=1;i<6;i++)
{
tempy-=1;
tempx+=1;
if(tempy<0||te