📄 rule.java
字号:
package xq;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Rule {
ChessBoard board = null;
ChessPiece piece = null;
ChessPoint point[][];
int startI, startJ, endI, endJ;
String name = null;
public Rule(ChessBoard board, ChessPoint point[][]) {
// TODO 自动生成构造函数存根
this.board = board;
this.point = point;
}
public boolean movePieceRule(ChessPiece piece, int startI, int startJ, int endI, int endJ, String name){
this.piece = piece;
this.startI = startI;
this.startJ = startJ;
this.endI = endI;
this.endJ = endJ;
this.name = name;
int minI = Math.min(startI, endI);
int maxI = Math.max(startI, endI);
int minJ = Math.min(startJ, endJ);
int maxJ = Math.max(startJ, endJ);
boolean 可否走棋 = false;
if(piece.getName().equals("车")){
if(startI == endI){//符合车沿纵向行走的规则
int j = 0;
for(j=minJ+1; j<=maxJ-1; j++){//如果起点与终点之间的棋点上有棋子
if(point[startI][j].isPiece()){
可否走棋 = false;
break;
}
}
if(j == maxJ){
可否走棋 = true;
}
}
else if(startJ == endJ){
int i=0;
for(i=minI+1; i<=maxI-1; i++){
if(point[i][startJ].isPiece()){
可否走棋 = false;
break;
}
}
if(i == maxI){
可否走棋 = true;
}
}
else{
可否走棋 = false;
}
}
else if(piece.getName().equals("马")){
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
//假如横向走两个棋点,纵向走一个棋点
if(xAxle == 2 && yAxle == 1){
if(endI>startI){
if(point[startI+1][startJ].isPiece()){
可否走棋 = false;
}
else{
可否走棋 = true;
}
}
if(endI < startI){
if(point[startI-1][startJ].isPiece()){
可否走棋 = false;
}
else{
可否走棋 = true;
}
}
}
//假如纵向走两个棋点,横向走一个棋点
else if(xAxle == 1 && yAxle ==2){
if(endJ>startJ){
if(point[startI][startJ+1].isPiece()){
可否走棋 = false;
}
else{
可否走棋 = true;
}
}
if(endJ < startJ){
if(point[startI][startJ-1].isPiece()){
可否走棋 = false;
}
else{
可否走棋 = true;
}
}
}
else{
可否走棋 = false;
}
}
else if(piece.getName().equals("象")){
int centerI = (startI + endI)/2;
int centerJ = (startJ + endJ)/2;
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if(xAxle == 2 && yAxle == 2 && endJ <= 5){//象走田,不可以过河
if(point[centerI][centerJ].isPiece()){
可否走棋 = false;
}
else{
可否走棋 = true;
}
}
else{
可否走棋 = false;
}
}
else if(piece.getName().equals("相")){
int centerI = (startI + endI)/2;
int centerJ = (startJ + endJ)/2;
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if(xAxle == 2 && yAxle == 2 && endJ >= 6){//象走田,不可以过河
if(point[centerI][centerJ].isPiece()){
可否走棋 = false;
}
else{
可否走棋 = true;
}
}
else{
可否走棋 = false;
}
}
else if(piece.getName().equals("炮")){
int number = 0;
if(startI == endI){
int j = 0;
for(j=minJ+1; j<=maxJ-1; j++){//如果炮沿纵向走
//计算起点和终点之间的棋子的数目.
if(point[startI][j].isPiece()){
number++;
}
}
if(number > 1){
可否走棋 = false;
}
else if(number == 1){
if(point[endI][endJ].isPiece()) {
可否走棋 = true;
}
}
else if(number == 0 && !point[endI][endJ].isPiece()){
可否走棋 = true;
}
}
else if(startJ == endJ){//如果炮沿横向走
int i = 0;
for(i=minI+1; i<=maxI-1; i++){//如果炮沿纵向走
//计算起点和终点之间的棋子的数目.
if(point[i][startJ].isPiece()){
number++;
}
}
if(number > 1){
可否走棋 = false;
}
else if(number == 1){
if(point[endI][endJ].isPiece()) {
可否走棋 = true;
}
}
else if(number == 0 && !point[endI][endJ].isPiece()){
可否走棋 = true;
}
}
else{
可否走棋 = false;
}
}
else if(piece.getName().equals("兵")){
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
//兵没有过河前不可横走(任何情况下都不可以后退).
if(endJ >= 6){
if(startJ - endJ == 1 && xAxle == 0){
可否走棋 = true;
}
else{
可否走棋 = false;
}
}
else if(endJ <= 5){//过河之后
if((startJ - endJ == 1)&&(xAxle == 0)){//向前一步
可否走棋 = true;
}
else if((endJ - startJ == 0)&&(xAxle == 1)){//向左或右一步
可否走棋 = true;
}
else{
可否走棋 = false;
}
}
}
else if(piece.getName().equals("卒")){
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
//卒没有过河前不可横走(任何情况下都不可以后退).
if(endJ <= 5){
if(endJ - startJ == 1 && xAxle == 0){
可否走棋 = true;
}
else{
可否走棋 = false;
}
}
else if(endJ >= 5){//过河之后
if((endJ - startJ == 1)&&(xAxle == 0)){//向前一步
可否走棋 = true;
}
else if((endJ - startJ == 0)&&(xAxle == 1)){//向左或右一步
可否走棋 = true;
}
else{
可否走棋 = false;
}
}
}
else if(piece.getName().equals("士")){
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if(endI <= 6 && endI >=4 && (endJ >=8 || endJ <=3) && xAxle == 1 && yAxle == 1){
可否走棋 = true;
}
else{
可否走棋 = false;
}
}
else if((piece.getName().equals("帅")) || (piece.getName().equals("将"))){
//判断是否符合帅与行走方法
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if(endI <= 6 && endI >=4 &&( endJ >=8 || endJ <= 3) ){
if((xAxle == 1 && yAxle == 0) || (xAxle == 0 && yAxle == 1)){
可否走棋 = true;
}
else if((xAxle == 0 && yAxle >= 5) && (name.equals("帅") || name.equals("将"))){
int number = 0;
for(int j=minJ+1; j<=maxJ-1; j++){//如果炮沿纵向走
//计算起点和终点之间的棋子的数目.
if(point[startI][j].isPiece()){
number++;
}
}
if(number == 0 &&
(point[endI][endJ].getPiece().getName().equals("帅") ||
point[endI][endJ].getPiece().getName().equals("将"))){
可否走棋 = true;
}
else{
可否走棋 = false;
}
}
else{
可否走棋 = false;
}
}
else{
可否走棋 = false;
}
}
return 可否走棋;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -