📄 chessrules.java
字号:
package client;
public class ChessRules
{
public ChessRules()
{
System.out.println("ChessRules");
}
//Judge whether the movement of the chess is legal
public static boolean isLegal(int Chessman, int Row, int Line, int Row2, int Line2)
{
AvailablePlace teap = new AvailablePlace(Chessman,ChessBoard.moment);
java.util.Vector<java.awt.Point> temp = ChessBoard.theap;
if(ChessBoard.theap == null)
{
System.err.println("isLegal(): theap == null");
System.out.println("isLegal(): available place are");
for(int i = 0; i < temp.size(); i++)
{
System.out.println("("+temp.get(i)+" , "+temp.get(i)+")");
}
System.out.println("isLegal(): tempap");
for(int i = 0; i < (teap.ap).size(); i++)
{
System.out.println("("+temp.get(i)+" , "+temp.get(i)+")");
}
return false;
}
System.out.println("isLegal(): available place are");
for(int i = 0; i < temp.size(); i++)
{
System.out.println("chess["+Chessman+"]"+ChessBoard.chess[Chessman].name+temp.get(i));
}
for(int i = 0; i < temp.size(); i++ )
{
//System.out.println("def point ("+temp.get(i).x+" , "+temp.get(i).y+")");
if(temp.get(i).x == Row2 && temp.get(i).y == Line2)
return true;
}
return false;
}
/*
if(Chessman < 1 | Chessman > 32 | Row < 0 | Row > 8 | Row2 < 0 | Row2 > 8 | Line < 0 | Line > 9 | Line2 < 0 | Line2 > 9)
{
System.err.println("error in isLegal("+Chessman+", "+Row+" , "+Line+" , "+Row2+" , "+Line2+")");
return false;
}
if(Row == Row2 & Line == Line2)
{
System.err.println("error isLegal isLegal because Row == Row2 & Line == Line2");
return false;
}
int maxRow, minRow, Rowminus, maxLine, minLine, Lineminus, chessBetweenNumber=0;
maxRow = ((Row> Row2) ? Row: Row2); //which row is big
minRow = ((Row < Row2 )? Row: Row2);
maxLine = ((Line> Line2) ? Line: Line2);
minLine = ((Line< Line2 )? Line : Line2);
Rowminus = maxRow - minRow;
Lineminus = maxLine - minLine;
if(Row==Row2)
{
for(int i=minLine+1; i< maxLine; i++)
if(ChessBoard.moment[Row][i]!=0)
chessBetweenNumber++;
}
else if(Line==Line2)
{
for(int i=minRow+1; i< maxRow; i++)
if(ChessBoard.moment[i][Line]!=0)
chessBetweenNumber++;
}
switch(Chessman)
{
//Che's Rule
case 1:
case 9:
case 17:
case 25: if(Row== Row2 || Line== Line2)
if(chessBetweenNumber!=0)
return(true);
break;
//Ma's Rule
case 2:
case 8:
case 18:
case 24:
if(Rowminus == 1 && Lineminus==2)
{
if(maxRow==Row2 && maxLine==Line2)
{
if(ChessBoard.moment[Row][Line+1] == 0)
return(true);
break;
}
else if(maxRow==Row2 &&maxLine==Line)
{
if(ChessBoard.moment[Row][Line-1]==0)
return(true);
break;
}
else if(maxRow==Row && maxLine==Line2)
{
if(ChessBoard.moment[Row][Line+1]==0)
return(true);
break;
}
else if(maxRow==Row && maxLine== Line)
{
if(ChessBoard.moment[Row][Line-1]==0)
return(true);
break;
}
} //if(Rowminus==1) ends
else if(Rowminus==2 && Lineminus==1)
{
if(maxRow==Row2 && maxLine==Line2)
{
if(ChessBoard.moment[Row+1][Line] == 0)
return(true);
break;
}
if(maxRow==Row2 &&maxLine==Line)
{
if(ChessBoard.moment[Row+1][Line]==0)
return(true);
break;
}
if(maxRow==Row && maxLine==Line2)
{
if(ChessBoard.moment[Row-1][Line]==0)
return(true);
break;
}
if(maxRow==Row && maxLine== Line)
{
if(ChessBoard.moment[Row-1][Line]==0)
return(true);
break;
}
return(false);
}//if(Rowminus==2)
break;
//blue Xiang's Rule
case 3:
case 7:if(maxLine< 5)
{
if(Rowminus==2 && Lineminus==2 && ChessBoard.moment[(Row + Row2)/2][(Line+Line2)/2]==0)
return(true);
}
break;
//Red Xiang's rule
case 19:
case 23:if(maxLine> 4)
{
if(Rowminus==2 && Lineminus==2 && ChessBoard.moment[(Row + Row2)/2][(Line+Line2)/2]==0)
return(true);
}
break;
//blue Shi's rule
case 4:
case 6:if(maxLine< 3 && maxRow< 6 && minRow> 2)
if(Rowminus ==1 && Lineminus==1)
return(true);
break;
//red Shi's rule
case 20:
case 22:if(minLine> 6 && maxRow< 6 && minRow> 2)
if(Rowminus ==1 && Lineminus==1)
return(true);
break;
//blue Jiang's rule
case 5: if(maxLine< 3 && maxRow< 5 && minRow> 2)
if((Rowminus==1 && Lineminus==0)||(Rowminus==0 && Lineminus==1))
return(true);
break;
//red Shuai's rule
case 21:if(minLine> 6 && maxRow< 5 && minRow> 2)
if((Rowminus==1 && Lineminus==0)||(Rowminus==0 && Lineminus==1))
return(true);
break;
//Pao's rule
case 10:
case 11:
case 26:
case 27:if(Row== Row2 || Line == Line2)
{
if(chessBetweenNumber==1)
{
if(ChessBoard.moment[Row2][Line2] != 0)
return(true);
}
else //if(chessBetweenNumber==0)
{
if(ChessBoard.moment[Row2][Line2] == 0)
return(true);
}
}
break;
//blue Zui's rule
case 12:
case 13:
case 14:
case 15:
case 16:if(Line2 < 5)
if((Line2-Line)==1)
return(true);
if(Line2> 4)
if(((Line2-Line)==1&&Rowminus==0)||((Lineminus==0)&&(Lineminus==1)))
return(true);
break;
//red Bing's rule
case 28:
case 29:
case 30:
case 31:
case 32:if(Line2 > 4)
if((Line-Line2)==1)
return(true);
if(Line2< 5)
if(((Line-Line2)==1&&Rowminus==0)||((Lineminus==0)&&(Lineminus==1)))
return(true);
break;
//default: break;
}//switch ends
return(false);
}//public static boolean isLegal
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -