设计思路
1.Cell 类:
这个类代表棋盘上的一个格子,也就是棋盘上的一个位置。每个格子可以包含一个棋子,用于表示在该位置上是否有棋子存在。
2.ChessboardPoint 类:
这个类代表棋盘上的一个点,使用行和列来标识棋盘上的位置。这在表示棋子的位置以及在棋盘上点击的位置时很有用。
3.ChessPiece 类:
这个类表示棋子,包含了棋子的所有属性。每个棋子有一个所有者(蓝色或红色玩家)、名称(象、鼠等)以及等级(表示棋子的力量)。这个类还包含了一个方法 canCapture,用于判断一个棋子是否可以吃掉另一个棋子。
4.PlayerColor 枚举类:
这个枚举类定义了玩家的颜色,即蓝色和红色。每个颜色都与一个Java的Color对象关联,用于在界面上显示不同玩家的棋子。
5.Constant 枚举类:
这个枚举类定义了一些常量,包括棋盘的行数和列数。这些常量可以在代码中被引用,使得代码更加可读和易于维护。
通过上述代码片段,可以看出这个斗兽棋游戏的实现是基于面向对象的设计。不同的类代表了游戏中的不同实体和概念,如棋盘、棋子、玩家颜色等。棋子之间的互动规则在 ChessPiece 类中被定义,包括了棋子的移动和吃掉其他棋子的规则。PlayerColor 枚举类用于表示玩家的颜色,这在界面上用不同的颜色显示不同玩家的棋子。
运行图片
相关代码
package boardForJunjun;
import java.awt.*;
/**
* This class store the real chess information.
* The Chessboard has 9*7 cells, and each cell has a position for chess
*/
public class Chessboard {
private Cell[][] grid;
public Chessboard() {
this.grid = new Cell[Constant.CHESSBOARD_ROW_SIZE.getNum()][Constant.CHESSBOARD_COL_SIZE.getNum()];//19X19
initGrid();
initPieces();
}
private void initGrid() {
for (int i = 0; i < Constant.CHESSBOARD_ROW_SIZE.getNum(); i++) {
for (int j = 0; j < Constant.CHESSBOARD_COL_SIZE.getNum(); j++) {
grid[i][j] = new Cell();
}
}
}
public void initPieces() {
grid[2][6].setPiece(new ChessPiece(PlayerColor.BLUE, "Elephant", 8));
grid[0][0].setPiece(new ChessPiece(PlayerColor.BLUE, "Lion", 7));
grid[0][6].setPiece(new ChessPiece(PlayerColor.BLUE, "Tiger", 6));
grid[2][2].setPiece(new ChessPiece(PlayerColor.BLUE, "Leopard", 5));
grid[2][4].setPiece(new ChessPiece(PlayerColor.BLUE, "Wolf", 4));
grid[1][1].setPiece(new ChessPiece(PlayerColor.BLUE, "Dog", 3));
grid[1][5].setPiece(new ChessPiece(PlayerColor.BLUE, "Cat", 2));
grid[2][0].setPiece(new ChessPiece(PlayerColor.BLUE, "Rat", 1));
grid[6][0].setPiece(new ChessPiece(PlayerColor.RED, "Elephant", 8));
grid[8][6].setPiece(new ChessPiece(PlayerColor.RED, "Lion", 7));
grid[8][0].setPiece(new ChessPiece(PlayerColor.RED, "Tiger", 6));
grid[6][4].setPiece(new ChessPiece(PlayerColor.RED, "Leopard", 5));
grid[6][2].setPiece(new ChessPiece(PlayerColor.RED, "Wolf", 4));
grid[7][5].setPiece(new ChessPiece(PlayerColor.RED, "Dog", 3));
grid[7][1].setPiece(new ChessPiece(PlayerColor.RED, "Cat", 2));
grid[6][6].setPiece(new ChessPiece(PlayerColor.RED, "Rat", 1));
}
private ChessPiece getChessPieceAt(ChessboardPoint point) {
return getGridAt(point).getPiece();
}
public Cell getGridAt(ChessboardPoint point) {
return grid[point.getRow()][point.getCol()];
}
//下面这个方法可以不看,是计算两点之间距离用的
private int calculateDistance(ChessboardPoint src, ChessboardPoint dest) {
return Math.abs(src.getRow() - dest.getRow()) + Math.abs(src.getCol() - dest.getCol());
}
private ChessPiece removeChessPiece(ChessboardPoint point) {
ChessPiece chessPiece = getChessPieceAt(point);
getGridAt(point).removePiece();//point包含了行和列//这个removePiece是cell里面的一个方法,是令这里的piece变成null
return chessPiece;
}
private void setChessPiece(ChessboardPoint point, ChessPiece chessPiece) {
getGridAt(point).setPiece(chessPiece);
}
public void moveChessPiece(ChessboardPoint point1, ChessboardPoint point2) {//我把这里的src改成了point1,方便理解这是一个point
if (!isValidMove(point1, point2)) {//判断是不是有效的移动
throw new IllegalArgumentException("Illegal chess move!");
}
setChessPiece(point2, removeChessPiece(point1));//如果是的,就把Point1移走,然后把原本的棋子放到Point2的位置
//在setChessPiece这个方法里,point2的意思是放上的棋子的位置,
// removeChessPiece后面的point1是指把该位置的棋子移走,但是它的返回值是一个chessPiece
}
public void captureChessPiece(ChessboardPoint point1, ChessboardPoint point2) {
removeChessPiece(point2);
setChessPiece(point2, removeChessPiece(point1));
// if (isValidCapture(point1, point2)) {
// throw new IllegalArgumentException("Illegal chess capture!");
// } else {
// removeChessPiece(point1);
// removeChessPiece(point2);
// setChessPiece(point2, getChessPieceAt(point1));
// }
// TODO: Finish the method.
}
//chessboard里面的类
public void removeAllPieces () {
for (int i = 0; i < Constant.CHESSBOARD_ROW_SIZE.getNum(); i++) {
for (int j = 0; j < Constant.CHESSBOARD_COL_SIZE.getNum(); j++) {
if (grid[i][j].getPiece() != null){
grid[i][j].setPiece(null);
}
}
}
}
public Cell[][] getGrid() {
return grid;
}
public PlayerColor getChessPieceOwner(ChessboardPoint point) {
return getGridAt(point).getPiece().getOwner();
}
public boolean isValidMove(ChessboardPoint point1, ChessboardPoint point2) {
if (getChessPieceAt(point1) == null || getChessPieceAt(point2) != null) {
return false;
}
if ((getGridAt(point2)==grid[3][1]||getGridAt(point2)==grid[3][2]||getGridAt(point2)==grid[3][4]||getGridAt(point2)==grid[3][5]||getGridAt(point2)==grid[4][1]||getGridAt(point2)==grid[4][2]||getGridAt(point2)==grid[4][4]||getGridAt(point2)==grid[4][5]||getGridAt(point2)==grid[5][1]||getGridAt(point2)==grid[5][2]||getGridAt(point2)==grid[5][4]||getGridAt(point2)==grid[5][5])) {
if (getChessPieceAt(point1).getName() != "Rat") {
return false;
} else {
return calculateDistance(point1, point2) == 1;
}
}//鼠可以进河
if (getGridAt(point2)==grid[8][3] && getChessPieceAt(point1).getOwner() ==PlayerColor.RED ) {
return false;
}
if (getGridAt(point2)==grid[0][3] && getChessPieceAt(point1).getOwner() ==PlayerColor.BLUE) {
return false;
}
if (calculateDistance(point1, point2) > 1
&& (getChessPieceAt(point1).getName() == "Lion" || getChessPieceAt(point1).getName() == "Tiger")) {
if (point1.getCol() == 0 && point1.getRow() == 3 && point2.getRow() == 3 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(3, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getCol() == 0 && point1.getRow() == 4 && point2.getRow() == 4 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getCol() == 0 && point1.getRow() == 5 && point2.getRow() == 5 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(5, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getCol() == 3 && point1.getRow() == 3 && point2.getRow() == 3 && point2.getCol() == 0) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(3, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getCol() == 3 && point1.getRow() == 4 && point2.getRow() == 4 && point2.getCol() == 0) {
if (getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getCol() == 3 && point1.getRow() == 5 && point2.getRow() == 5 && point2.getCol() == 0) {
if (getChessPieceAt(new ChessboardPoint(5, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 3 && point1.getCol() == 3 && point2.getRow() == 3 && point2.getCol() == 6) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(3, 5)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 4 && point1.getCol() == 3 && point2.getRow() == 4 && point2.getCol() == 6) {
if (getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 5 && point1.getCol() == 3 && point2.getRow() == 5 && point2.getCol() == 6) {
if (getChessPieceAt(new ChessboardPoint(5, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 3 && point1.getCol() == 6 && point2.getRow() == 3 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(3, 5)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 4 && point1.getCol() == 6 && point2.getRow() == 4 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 5 && point1.getCol() == 6 && point2.getRow() == 5 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(5, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 1 && point2.getRow() == 6 && point2.getCol() == 1) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 1)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 1 && point2.getRow() == 2 && point2.getCol() == 1) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 1)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 2 && point2.getRow() == 6 && point2.getCol() == 2) {
if (getChessPieceAt(new ChessboardPoint(3, 2)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 2 && point2.getRow() == 2 && point2.getCol() == 2) {
if (getChessPieceAt(new ChessboardPoint(3, 2)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 4 && point2.getRow() == 6 && point2.getCol() == 4) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 4)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 4 && point2.getRow() == 2 && point2.getCol() == 4) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 4)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 5 && point2.getRow() == 6 && point2.getCol() == 5) {
if (getChessPieceAt(new ChessboardPoint(3, 5)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return true;
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 5 && point2.getRow() == 2 && point2.getCol() == 5) {
if (getChessPieceAt(new ChessboardPoint(3, 5)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return true;
} else {
return false;
}
} else {
return false;
}
}//狮虎可以跨河
return calculateDistance(point1, point2) == 1;//在本class中的一个方法,用于计算两点之间的距离,如果距离为一就是true,否则为false
}
public boolean isValidCapture(ChessboardPoint point1, ChessboardPoint point2) {
if (getChessPieceAt(point1) == null || getChessPieceAt(point2) == null) {
return false;
}//有一个为空
if (getChessPieceAt(point1).getOwner() == getChessPieceOwner(point2)) {
return false;
}//两个都是同一方
if((getGridAt(point1)==grid[0][2]||getGridAt(point1)==grid[0][4]||getGridAt(point1)==grid[1][3])&&getChessPieceAt(point2).getOwner() ==PlayerColor.BLUE&&getChessPieceAt(point1).getOwner() ==PlayerColor.RED){
return false;
}
if((getGridAt(point1)==grid[8][2]||getGridAt(point1)==grid[8][4]||getGridAt(point1)==grid[7][3])&&getChessPieceAt(point2).getOwner() ==PlayerColor.RED&&getChessPieceAt(point1).getOwner() ==PlayerColor.BLUE){
return false;
}
if ((getGridAt(point1)==grid[3][1]||getGridAt(point1)==grid[3][2]||getGridAt(point1)==grid[3][4]||getGridAt(point1)==grid[3][5]||getGridAt(point1)==grid[4][1]||getGridAt(point1)==grid[4][2]||getGridAt(point1)==grid[4][4]||getGridAt(point1)==grid[4][5]||getGridAt(point1)==grid[5][1]||getGridAt(point1)==grid[5][2]||getGridAt(point1)==grid[5][4]||getGridAt(point1)==grid[5][5])&&(getGridAt(point2)!=grid[3][1]&&getGridAt(point2)!=grid[3][2]&&getGridAt(point2)!=grid[3][4]&&getGridAt(point2)!=grid[3][5]&&getGridAt(point2)!=grid[4][1]&&getGridAt(point2)!=grid[4][2]&&getGridAt(point2)!=grid[4][4]&&getGridAt(point2)!=grid[4][5]&&getGridAt(point2)!=grid[5][1]&&getGridAt(point2)!=grid[5][2]&&getGridAt(point2)!=grid[5][4]&&getGridAt(point2)!=grid[5][5])) {
return false;
}
if ((getGridAt(point2)==grid[3][1]||getGridAt(point2)==grid[3][2]||getGridAt(point2)==grid[3][4]||getGridAt(point2)==grid[3][5]||getGridAt(point2)==grid[4][1]||getGridAt(point2)==grid[4][2]||getGridAt(point2)==grid[4][4]||getGridAt(point2)==grid[4][5]||getGridAt(point2)==grid[5][1]||getGridAt(point2)==grid[5][2]||getGridAt(point2)==grid[5][4]||getGridAt(point2)==grid[5][5])&&(getGridAt(point1)!=grid[3][1]&&getGridAt(point1)!=grid[3][2]&&getGridAt(point1)!=grid[3][4]&&getGridAt(point1)!=grid[3][5]&&getGridAt(point1)!=grid[4][1]&&getGridAt(point1)!=grid[4][2]&&getGridAt(point1)!=grid[4][4]&&getGridAt(point1)!=grid[4][5]&&getGridAt(point1)!=grid[5][1]&&getGridAt(point1)!=grid[5][2]&&getGridAt(point1)!=grid[5][4]&&getGridAt(point1)!=grid[5][5])) {
return false;
}
if((getGridAt(point2)==grid[0][2]||getGridAt(point2)==grid[0][4]||getGridAt(point2)==grid[1][3])&&getChessPieceAt(point1).getOwner() ==PlayerColor.BLUE&&getChessPieceAt(point2).getOwner() ==PlayerColor.RED){
return true;
}
if((getGridAt(point2)==grid[8][2]||getGridAt(point2)==grid[8][4]||getGridAt(point2)==grid[7][3])&&getChessPieceAt(point1).getOwner() ==PlayerColor.RED&&getChessPieceAt(point2).getOwner() ==PlayerColor.BLUE){
return true;
}
if (calculateDistance(point1, point2) > 1
&& (getChessPieceAt(point1).getName() == "Lion" || getChessPieceAt(point1).getName() == "Tiger")) {
if (point1.getCol() == 0 && point1.getRow() == 3 && point2.getRow() == 3 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(3, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getCol() == 0 && point1.getRow() == 4 && point2.getRow() == 4 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getCol() == 0 && point1.getRow() == 5 && point2.getRow() == 5 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(5, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getCol() == 3 && point1.getRow() == 3 && point2.getRow() == 3 && point2.getCol() == 0) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(3, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getCol() == 3 && point1.getRow() == 4 && point2.getRow() == 4 && point2.getCol() == 0) {
if (getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getCol() == 3 && point1.getRow() == 5 && point2.getRow() == 5 && point2.getCol() == 0) {
if (getChessPieceAt(new ChessboardPoint(5, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 3 && point1.getCol() == 3 && point2.getRow() == 3 && point2.getCol() == 6) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(3, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 4 && point1.getCol() == 3 && point2.getRow() == 4 && point2.getCol() == 6) {
if (getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 5 && point1.getCol() == 3 && point2.getRow() == 5 && point2.getCol() == 6) {
if (getChessPieceAt(new ChessboardPoint(5, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 3 && point1.getCol() == 6 && point2.getRow() == 3 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(3, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 4 && point1.getCol() == 6 && point2.getRow() == 4 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 5 && point1.getCol() == 6 && point2.getRow() == 5 && point2.getCol() == 3) {
if (getChessPieceAt(new ChessboardPoint(5, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 1 && point2.getRow() == 6 && point2.getCol() == 1) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 1)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 1 && point2.getRow() == 2 && point2.getCol() == 1) {
if (getChessPieceAt(new ChessboardPoint(3, 1)) == null && getChessPieceAt(new ChessboardPoint(4, 1)) == null && getChessPieceAt(new ChessboardPoint(5, 1)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 2 && point2.getRow() == 6 && point2.getCol() == 2) {
if (getChessPieceAt(new ChessboardPoint(3, 2)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 2 && point2.getRow() == 2 && point2.getCol() == 2) {
if (getChessPieceAt(new ChessboardPoint(3, 2)) == null && getChessPieceAt(new ChessboardPoint(4, 2)) == null && getChessPieceAt(new ChessboardPoint(5, 2)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 4 && point2.getRow() == 6 && point2.getCol() == 4) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 4)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 4 && point2.getRow() == 2 && point2.getCol() == 4) {
if (getChessPieceAt(new ChessboardPoint(3, 4)) == null && getChessPieceAt(new ChessboardPoint(4, 4)) == null && getChessPieceAt(new ChessboardPoint(5, 4)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 2 && point1.getCol() == 5 && point2.getRow() == 6 && point2.getCol() == 5) {
if (getChessPieceAt(new ChessboardPoint(3, 5)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
}
if (point1.getRow() == 6 && point1.getCol() == 5 && point2.getRow() == 2 && point2.getCol() == 5) {
if (getChessPieceAt(new ChessboardPoint(3, 5)) == null && getChessPieceAt(new ChessboardPoint(4, 5)) == null && getChessPieceAt(new ChessboardPoint(5, 5)) == null) {
return getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
} else {
return false;
}
} else {
return false;
}
}
return calculateDistance(point1, point2) == 1&&getChessPieceAt(point1).canCapture(getChessPieceAt(point2));
}
public boolean checknest(ChessboardPoint dest){
if (getGridAt(dest)==grid[0][3]||getGridAt(dest)==grid[8][3]) {
return true;
}
return false;
}//进对方巢穴赢
public boolean checkBlueNest(ChessboardPoint dest){
if (getGridAt(dest)==grid[0][3]) {
return true;
}
return false;
}
public boolean checkRedNest(ChessboardPoint dest){
if (getGridAt(dest)==grid[8][3]) {
return true;
}
return false;
}
// public boolean unablemove(PlayerColor currentPlayer){
// int count=0;
// for(int i=0;i<9;i++){
// for(int j=0;j<7;j++){
// if (getChessPieceAt(new ChessboardPoint(i, j)) == null || getChessPieceAt(new ChessboardPoint(i, j)).getOwner() != currentPlayer){
// count+=0;
// }else {
// count++;
// }
// }
// }if (count==0) {
// return true;
// }else {
// return false;
// }
// }
public boolean UnableMove(PlayerColor currentPlayer){
int count = 0;
for(int i = 0; i < 9; i++){
for(int j = 0; j < 7; j++){
ChessPiece chessPiece = getChessPieceAt(new ChessboardPoint(i,j));
if(chessPiece != null && currentPlayer.getColor().equals(chessPiece.getOwner().getColor())){
count++;
}
}
}
if(count == 0)return true;
else return false;
}
public boolean BlueUnableMove(PlayerColor currentPlayer){
int count=0;
for(int i=0;i<9;i++){
for(int j=0;j<7;j++){
if (getChessPieceAt(new ChessboardPoint(i, j)) == null && currentPlayer.getColor().equals(Color.RED)){
count+=0;
}else {
count++;
}
}
}
System.out.println(count);
if (count==0) {
return true;
}else {
return false;
}
}
public boolean RedUnableMove(PlayerColor currentPlayer){
int count=0;
for(int i=0;i<9;i++){
for(int j=0;j<7;j++){
if (getChessPieceAt(new ChessboardPoint(i, j)) == null && currentPlayer.getColor().equals(Color.BLUE)){
count+=0;
}else {
count++;
}
}
}if (count==0) {
return true;
}else {
return false;
}
}
public void setGrid(Cell[][] grid) {
this.grid = grid;
}
}
package boardForJunjun;
public class ChessPiece {
// the owner of the chess
private PlayerColor owner;
// Elephant? Cat? Dog? ...
private String name;
private int rank;
public ChessPiece(PlayerColor owner, String name, int rank) {
this.owner = owner;
this.name = name;
this.rank = rank;
}
/**
* 在这里也有一个捕食需要我们写
* @param target
* @return
*/
public boolean canCapture(ChessPiece target) {
// TODO: Finish this method!
if (target.getOwner() == this.owner){
return false;
}
if (this.name.equals("Elephant") && target.name.equals("Rat")){
return false;
}
if (this.name.equals("Rat") && target.name.equals("Elephant")){
return true;
}
if (this.rank >= target.rank) {
return true;
}
return false;
}
/**
* 以下是getter和setter方法
*/
public String getName() {
return name;
}
public PlayerColor getOwner() {
return owner;
}
public void setOwner(PlayerColor owner) {
this.owner = owner;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}
运行视频
8月29日