?? 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 自動生成構造函數(shù)存根
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){//符合車沿縱向行走的規(guī)則
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++){//如果炮沿縱向走
//計算起點和終點之間的棋子的數(shù)目.
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++){//如果炮沿縱向走
//計算起點和終點之間的棋子的數(shù)目.
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++){//如果炮沿縱向走
//計算起點和終點之間的棋子的數(shù)目.
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 + -